util 工具类(更新中............)

来源:互联网 发布:软件需求 三方面 编辑:程序博客网 时间:2024/05/10 15:55

/****************************************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);
    }
  }

原创粉丝点击