c++获取年月日时间(window+linux)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <stdbool.h>

#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>

#include <fstream>
#include <iostream>
using namespace std;

void GetLocalTimeString(string& strLocalTime, bool bDateOnly)
{
	static char chTime[15];

#ifdef _PLATFORM_WINDOWS_
	time_t lTime; time(&lTime);
	tm sTM; localtime_s(&sTM, &lTime);
#else
	struct timeval timeVal; gettimeofday(&timeVal, NULL);
	struct tm sTM; localtime_r(&timeVal.tv_sec, &sTM);
#endif    // lhf modify 2009.10.23

	if(bDateOnly)
		sprintf(chTime, "%04d%02d%02d", (sTM.tm_year+1900), sTM.tm_mon+1, sTM.tm_mday);
	else
		sprintf(chTime, "%04d%02d%02d%02d%02d%02d", (sTM.tm_year+1900), sTM.tm_mon+1, sTM.tm_mday, sTM.tm_hour, sTM.tm_min, sTM.tm_sec);
	strLocalTime = chTime;
}
int main()
{
   string str123;
  GetLocalTimeString(str123,false);
   cout<<str123<<endl;
   cout<<"2021,新年快乐"<<endl;
   return 0;
}

  

posted on 2021-01-01 02:50  lydstory  阅读(364)  评论(0)    收藏  举报

导航