/****************************************cutil.h******************************************/
#ifndef _CPP_SOCKET_C_UTIL_
#define _CPP_SOCKET_C_UTIL_
#include <time.h>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;
namespace CPPSocket
{
namespace T
{
string getTime();//YYYYMMDDHHmmSS
string getTime1(); //YYYY-mm-DD HH:MM:SS
template <typename R>
string toString(R t)
{
stringstream s;
s<<t;
return s.str();
}
}
}
#endif
/****************************cutil.cpp*****************************************/
#include "cutil.h"
namespace CPPSocket
{
namespace T
{
string getTime()//YYYYMMDDHHmmSS
{
char tmbuf[64];
time_t now = time(NULL);
struct tm _ct;
localtime_r(&now, &_ct);
memset(tmbuf,0,sizeof(tmbuf));
snprintf(tmbuf,sizeof(tmbuf),"%04d%02d%02d%02d%02d%02d",(int)(_ct.tm_year+1900), (int)(_ct.tm_mon+1), (int)(_ct.tm_mday),(int)_ct.tm_hour,(int)_ct.tm_min,(int)_ct.tm_sec);
return string(tmbuf);
}
string getTime1() //YYYY-mm-DD HH:MM:SS
{
char tmbuf[64];
time_t now = time(NULL);
struct tm _ct;
localtime_r(&now, &_ct);
memset(tmbuf,0,sizeof(tmbuf));
snprintf(tmbuf,sizeof(tmbuf),"[%04d-%02d-%02d %02d:%02d:%02d]",(int)(_ct.tm_year+1900), (int)(_ct.tm_mon+1), (int)(_ct.tm_mday),(int)_ct.tm_hour,(int)_ct.tm_min,(int)_ct.tm_sec);
return string(tmbuf);
}
}
}