- int GetTimeString( string& strDate , const unsigned int cStringFormat )
- {
- char sTime[40];
- time_t t = time(NULL);
- struct tm tm1;
- localtime_r(&t, &tm1);
- switch(cStringFormat)
- {
- case 0: // today is 2008-01-10. return 20080110174233
- sprintf( sTime, "%.4d%.2d%.2d%.2d%.2d%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday, tm1.tm_hour, tm1.tm_min, tm1.tm_sec );
- strDate = sTime;
- break;
- case 1: // today is 2008-01-10. return 17:42:33 01/10
- sprintf( sTime, "%.2d:%.2d:%.2d %.2d/%.2d", tm1.tm_hour, tm1.tm_min, tm1.tm_sec, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 2: // today is 2008-01-10. return 20070110
- sprintf( sTime, "%.4d%.2d%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 3: // today is 2008-01-10. return 2008-01-10
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 4: // today is 2008-01-10. return 2008-01-11
- t = time(NULL) + 86400;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 5: // today is 2008-01-10. return 2008-01-17
- t = time(NULL) + 86400*7;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 6: // today is 2008-01-10. return 2008-01-03
- t = time(NULL) - 86400*7;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 7: // today is 2008-01-10. return 2008-02-01
- if((tm1.tm_mon+1)<12)
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+2, 1 );
- else
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year+1, 1, 1 );
- strDate = sTime;
- break;
- case 8: // today is 2008-01-10. return 2008-01-01
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, 1 );
- strDate = sTime;
- break;
- case 9: // today is 2008-01-10. return month value: 01
- sprintf( sTime, "%.2d", tm1.tm_mon+1 );
- strDate = sTime;
- break;
- case 10: // today is 2008-01-10. return 20080109
- t = time(NULL) - 86400;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d%.2d%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 11: // today is 2008-01-10. return day of week, 0 is sunday, 6 is saturday
- sprintf( sTime, "%d", tm1.tm_wday );
- strDate = sTime;
- break;
- case 12: // today is 2008-01-10. return 10
- sprintf( sTime, "%.2d", tm1.tm_mday );
- strDate = sTime;
- break;
- case 13: // today is 2008-01-10. return 2008-01-09
- t = time(NULL) - 86400;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 14: // today is 2008-01-10. return 2008-01-09
- t = time(NULL) - 86400;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-01", 1900+tm1.tm_year, tm1.tm_mon+1 );
- strDate = sTime;
- break;
- case 15: // today is 2008-01-10. return month value: 01
- t = time(NULL) - 86400;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.2d", tm1.tm_mon+1 );
- strDate = sTime;
- break;
- case 16: // today is 2008-01-10. return 2008-01-03 00:00:00
- t = time(NULL) - 86400*7;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d 00:00:00", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 17: // today is 2008-01-10. return 2008-01-03 00:00:00
- t = time(NULL) - 86400;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d 23:59:59", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 18: // today is 2008-01-10. return 2008-01-09
- t = time(NULL) - 86400*2;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 19: // today is 2008-01-10. return 2008-01-02
- t = time(NULL) - 86400*8;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 20: // today is 2008-01-15. return 2008-01-01
- t = time(NULL) - 86400*14;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-%.2d", 1900+tm1.tm_year, tm1.tm_mon+1, tm1.tm_mday );
- strDate = sTime;
- break;
- case 21: // today is 2008-07-01. return 2008-05-01 这里可能有错, 需要考虑闰年的情况,实际使用时要考虑一下
- t = time(NULL) - 86400*64;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.4d-%.2d-01", 1900+tm1.tm_year, tm1.tm_mon+1 );
- strDate = sTime;
- break;
- case 22: // today is 2008-01-10. return 01; if today is 2008-01-01 will return 12
- t = time(NULL) - 86400;
- localtime_r(&t, &tm1);
- sprintf( sTime, "%.2d", tm1.tm_mon+1);
- strDate = sTime;
- break;
- default:
- return -1;
- }
- return 0;
- }
获取本地时间的多种格式函数
最新推荐文章于 2022-12-25 13:54:50 发布