#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 <iostream> using namespace std; static const size_t MAX_TIME_STRING_LEN = 128 ; // get current YYYYMMDD bool getLocalDate(string& strDate) { struct tm newTM; time_t tmpTimeSec ; struct timeval timeVal ; gettimeofday(&timeVal, NULL) ; tmpTimeSec = timeVal.tv_sec ; localtime_r( &(tmpTimeSec), &newTM); char chTmp[10]; memset(chTmp, 0, 10); snprintf(chTmp, MAX_TIME_STRING_LEN, "%04d%02d%02d", (newTM.tm_year+1900), newTM.tm_mon+1, newTM.tm_mday ) ; strDate = chTmp; return true ; } int main() { string str123; getLocalDate(str123); cout<<str123<<endl; cout<<"2021,新年快樂"<<endl; return 0; }