📄 timerep.cpp
字号:
UTCTimeRep::UTCTimeRep(const char* pStr){ m_szTime[0] = '\0'; fromString(pStr);}UTCTimeRep::UTCTimeRep(time_t t, BOOL bUTC /* = TRUE */){ m_szTime[0] = '\0'; bUTC ? fromUTCTime(t) : fromTime(t);}UTCTimeRep::~UTCTimeRep(void){ // Empty}const char*UTCTimeRep::asRFC1123String(void){ m_szTime[0] = '\0'; struct tm* ptm = NULL; // If our time is valid, fetch a struct tm in GMT.#ifdef _USE_THREADSAFE_TIME_FUNCS struct tm tmgmt; if (m_tTime != INVALID_TIME_T) { ptm = gmtime_r(&m_tTime, &tmgmt); }#else if (m_tTime != INVALID_TIME_T) { ptm = gmtime(&m_tTime); }#endif // If the struct tm is good, create the string. if (ptm != NULL) {#if defined(_WIN16) || defined(WIN32_PLATFORM_PSPC) if (strftime16(m_szTime, MAX_UTC_TIME_LEN, "%a, %d %b %Y %H:%M:%S GMT", ptm) == 0)#else if (strftime(m_szTime, MAX_UTC_TIME_LEN, "%a, %d %b %Y %H:%M:%S GMT", ptm) == 0)#endif { // If there is an error, the string is undefined so empty it. m_szTime[0] = '\0'; } } return m_szTime;}const char*UTCTimeRep::asRFC850String(void){ m_szTime[0] = '\0'; struct tm* ptm = NULL; // If our time is valid, fetch a struct tm in GMT.#ifdef _USE_THREADSAFE_TIME_FUNCS struct tm tmgmt; if (m_tTime != INVALID_TIME_T) { ptm = gmtime_r(&m_tTime, &tmgmt); }#else if (m_tTime != INVALID_TIME_T) { ptm = gmtime(&m_tTime); }#endif // If the struct tm is good, create the string. if (ptm != NULL) {#if defined(_WIN16) || defined(WIN32_PLATFORM_PSPC) if (strftime16(m_szTime, MAX_UTC_TIME_LEN, "%A, %d-%b-%y %H:%M:%S GMT", ptm) == 0)#else if (strftime(m_szTime, MAX_UTC_TIME_LEN, "%A, %d-%b-%y %H:%M:%S GMT", ptm) == 0)#endif { m_szTime[0] = '\0'; } } return m_szTime;}const char*UTCTimeRep::asUTCString(void){ m_szTime[0] = '\0'; struct tm* ptm = NULL; // If our time is valid, fetch a struct tm in GMT.#ifdef _USE_THREADSAFE_TIME_FUNCS struct tm tmgmt; if (m_tTime != INVALID_TIME_T) { ptm = gmtime_r(&m_tTime, &tmgmt); }#else if (m_tTime != INVALID_TIME_T) { ptm = gmtime(&m_tTime); }#endif // If the struct tm is good, create the string. if (ptm != NULL) {#if defined(_WIN16) || defined(WIN32_PLATFORM_PSPC) if (strftime16(m_szTime, MAX_UTC_TIME_LEN, "%Y%m%dT%H%M%SZ", ptm) == 0)#else if (strftime(m_szTime, MAX_UTC_TIME_LEN, "%Y%m%dT%H%M%SZ", ptm) == 0)#endif { m_szTime[0] = '\0'; } } return m_szTime;}time_tUTCTimeRep::asUTCTimeT(void){ return m_tTime;}void UTCTimeRep::SetLocalTime(time_t t){ fromTime(t);}void UTCTimeRep::SetUTCTime(time_t t){ fromUTCTime(t);}void UTCTimeRep::SetTime(const char* pStr){ fromString(pStr);}/* * Convert time_t value from localtime to gmt. First we call gmtime() to get * a struct tm in localtime. This works because gmtime() does no timezone * conversions -- the result is in the same timezone as the input. Then we * call mktime() which converts a struct tm in localtime to a time_t in GMT. * * Win32 always assumes GMT for mktime(), so we use LocalFileTimeToFileTime() * and a pair of conversion functions. */intUTCTimeRep::fromTime(time_t t){#ifdef _WIN32 FILETIME lft, ft; TimetToFileTime(t, &lft); LocalFileTimeToFileTime(&lft, &ft); m_tTime = FileTimeToTimet(&ft); return 0;#else /* ! _WIN32 */#ifdef _USE_THREADSAFE_TIME_FUNCS struct tm tmlocal; struct tm* ptm = gmtime_r(&t, &tmlocal);#else struct tm* ptm = gmtime(&t);#endif if (ptm != NULL) { m_tTime = mktime(ptm); return 0; } m_tTime = INVALID_TIME_T; return -1;#endif /* _WIN32 */}intUTCTimeRep::fromUTCTime(time_t t){ m_tTime = t; return 0;}intUTCTimeRep::fromTm(struct tm* ptm){#ifdef _WIN32 // See note above in fromTime() time_t t = mktime_gmt(ptm); if (t != INVALID_TIME_T) { fromTime(t); return 0; } return -1;#else /* ! _WIN32 */ if (ptm != NULL) { m_tTime = mktime(ptm); return 0; } m_tTime = INVALID_TIME_T; return -1;#endif /* _WIN32 */}intUTCTimeRep::fromUTCTm(struct tm* ptm){ m_tTime = mktime_gmt(ptm); return ( (m_tTime == INVALID_TIME_T) ? -1 : 0 );}// Find next alnum field in string or bail if not found#define NEXT_ALNUM_FIELD(p) \ while ( isalnum(*p)) { p++; if (*p == '\0') goto bail; } \ while (!isalnum(*p)) { p++; if (*p == '\0') goto bail; }intUTCTimeRep::fromString(const char* pStr){ unsigned int n; struct tm stm; memset(&stm, 0, sizeof(stm)); if (pStr == NULL || pStr[0] == '\0') { goto bail; } /* * RFC1123: wkday "," SP DD SP month SP YYYY HH:MM:SS (Wed, 02 Jun 1982) * RFC850 : weekday "," SP DD-month-YY HH:MM:SS (Wednesday, 02-Jun-82) * asctime: wkday SP month SP dD HH:MM:SS YYYY (Wed Jun 2 09:00:00 1982) * UTC : YYYYMMDD'T'HHMMSS'Z' */ if (isdigit(pStr[0])) { // UTC if(strlen(pStr) != 16) { goto bail; } for (n = 0; n < 8; n++) { if (!isdigit(pStr[n])) { goto bail; } } stm.tm_year = (pStr[0]-'0')*1000 + (pStr[1]-'0')*100 + (pStr[2]-'0')*10 + (pStr[3]-'0') - 1900; stm.tm_mon = (pStr[4]-'0')*10 + (pStr[5]-'0'); stm.tm_mday = (pStr[6]-'0')*10 + (pStr[7]-'0'); for (n = 9; n < 15; n++) { if (!isdigit(pStr[n])) { goto bail; } } stm.tm_hour = (pStr[9]-'0')*10 + (pStr[10]-'0'); stm.tm_min = (pStr[11]-'0')*10 + (pStr[12]-'0'); stm.tm_sec = (pStr[13]-'0')*10 + (pStr[14]-'0'); // We have a struct tm in GMT return fromUTCTm(&stm); } else { if (weekday_from_string(pStr) < 0) { goto bail; } NEXT_ALNUM_FIELD(pStr); if (isdigit(pStr[0])) { // RFC1123 or RFC850 if (!isdigit(pStr[1]) || isalnum(pStr[2])) { goto bail; } stm.tm_mday = (pStr[0]-'0')*10 + (pStr[1]-'0'); NEXT_ALNUM_FIELD(pStr); if ((stm.tm_mon = month_from_string(pStr)) < 0) { goto bail; } NEXT_ALNUM_FIELD(pStr); if (!isdigit(pStr[0]) || !isdigit(pStr[1])) { goto bail; } if (!isdigit(pStr[2])) { // RFC850 stm.tm_year = (pStr[0]-'0')*10 + (pStr[1]-'0'); } else if (isdigit(pStr[3]) && !isdigit(pStr[4])) { // RFC1123 stm.tm_year = (pStr[0]-'0')*1000 + (pStr[1]-'0')*100 + (pStr[2]-'0')*10 + (pStr[3]-'0') - 1900; } else { goto bail; } NEXT_ALNUM_FIELD(pStr); for (n = 0; n < 8; n++) { if (n == 2 || n == 5) { if (pStr[n] != ':') { goto bail; } } else if (!isdigit(pStr[n])) { goto bail; } } stm.tm_hour = (pStr[0]-'0')*10 + (pStr[1]-'0'); stm.tm_min = (pStr[3]-'0')*10 + (pStr[4]-'0'); stm.tm_sec = (pStr[6]-'0')*10 + pStr[7]-'0'; pStr += 8; NEXT_ALNUM_FIELD(pStr); if (strncasecmp(pStr, "GMT", 3) != 0) { goto bail; } // We have a struct tm in GMT return fromUTCTm(&stm); } else { // asctime NEXT_ALNUM_FIELD(pStr); if ((stm.tm_mon = month_from_string(pStr)) == -1) { goto bail; } NEXT_ALNUM_FIELD(pStr); if (isdigit(pStr[0]) && pStr[1] == ' ') { stm.tm_mday = (pStr[0]-'0'); } else if (isdigit(pStr[0]) && isdigit(pStr[1]) && pStr[2] == ' ') { stm.tm_mday = (pStr[0]-'0')*10 + (pStr[1]-'0'); } else { goto bail; } NEXT_ALNUM_FIELD(pStr); for (n = 0; n < 8; n++) { if (n == 2 || n == 5) { if (pStr[n] != ':') { goto bail; } } else if (!isdigit(pStr[n])) { goto bail; } } stm.tm_hour = (pStr[0]-'0')*10 + (pStr[1]-'0'); stm.tm_min = (pStr[3]-'0')*10 + (pStr[4]-'0'); stm.tm_sec = (pStr[6]-'0')*10 + pStr[7]-'0'; pStr += 8; NEXT_ALNUM_FIELD(pStr); if (!isdigit(pStr[0]) || !isdigit(pStr[1]) || !isdigit(pStr[2]) || !isdigit(pStr[3]) || isdigit(pStr[4])) { goto bail; } stm.tm_year = (pStr[0]-'0')*1000 + (pStr[1]-'0')*100 + (pStr[2]-'0')*10 + (pStr[3]-'0') - 1900; // We have a struct tm in localtime return fromTm(&stm); } } /* NOTREACHED */bail: m_tTime = INVALID_TIME_T; return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -