int gettimeofday(struct timeval *tv, struct timezone *tz) { FILETIME ft; unsigned __int64 t = 0; ULARGE_INTEGER li; static int tzflag; if (NULL != tv) { GetSystemTimeAsFileTime(&ft); li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; t = li.QuadPart; /* In 100-nanosecond intervals */ t /= 10; /* In microseconds */ t -= DELTA_EPOCH_IN_MICROSECS; /* Offset to the Epoch time */ tv->tv_sec = (long)(t / 1000000UL); tv->tv_usec = (long)(t % 1000000UL); } if (NULL != tz) { if (!tzflag) { _tzset(); tzflag++; } tz->tz_minuteswest = _get_timezone(NULL) / 60; tz->tz_dsttime = _get_daylight(NULL); } return 0; }
int gettimeofday(struct timeval *_tv, struct timezone *_tz) { FILETIME ft; LARGE_INTEGER li; __int64 t; static int tzflag; if (_tv) { GetSystemTimeAsFileTime(&ft); li.LowPart = ft.dwLowDateTime; li.HighPart = ft.dwHighDateTime; t = li.QuadPart; /* In 100-nanosecond intervals */ t -= EPOCHFILETIME; /* Offset to the Epoch time */ t /= 10; /* In microseconds */ _tv->tv_sec = (long)(t / 1000000); _tv->tv_usec = (long)(t % 1000000); } if (_tz) { if (!tzflag) { _tzset(); tzflag++; } _get_timezone(&_tz->tz_minuteswest); _tz->tz_minuteswest /= 60; _get_daylight(&_tz->tz_dsttime); } return 0; }
int gettimeofday(struct timeval * tv, struct timezone * tz) { FILETIME ft; unsigned __int64 tmpres(0); static int tzflag; if (NULL != tv) { GetSystemTimeAsFileTime(&ft); tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; /*converting file time to unix epoch*/ tmpres -= DELTA_EPOCH_IN_MICROSECS; tmpres /= 10; /*convert into microseconds*/ tv->tv_sec = (long)(tmpres / 1000000UL); tv->tv_usec = (long)(tmpres % 1000000UL); } if (NULL != tz) { if (!tzflag) { _tzset(); tzflag++; } long time_zone(0); errno_t err(_get_timezone(&time_zone)); if (err == NO_ERROR) { tz->tz_minuteswest = time_zone / 60; } else { return -1; } int day_light(0); err = (_get_daylight(&day_light)); if (err == NO_ERROR) { tz->tz_dsttime = day_light; return 0; } else { return -1; } } return -1; }
/** * Get the current UNIX time * * @param tv the output timeval structure (or NULL) * @param tz the output timezone structure (or NULL) * @return 0 on success or -1 on error */ int gettimeofday(struct timeval *tv, struct timezone *tz) { // From: http://suacommunity.com/dictionary/gettimeofday-entry.php // Define a structure to receive the current Windows filetime FILETIME ft; // Initialize the present time to 0 and the timezone to UTC unsigned __int64 tmpres = 0; static int tzflag = 0; if (NULL != tv) { GetSystemTimeAsFileTime(&ft); // The GetSystemTimeAsFileTime returns the number of 100 nanosecond // intervals since Jan 1, 1601 in a structure. Copy the high bits to // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits. tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; // Convert to microseconds by dividing by 10 tmpres /= 10; // The Unix epoch starts on Jan 1 1970. Need to subtract the difference // in seconds from Jan 1 1601. tmpres -= DELTA_EPOCH_IN_MICROSECS; // Finally change microseconds to seconds and place in the seconds value. // The modulus picks up the microseconds. tv->tv_sec = (long)(tmpres / 1000000UL); tv->tv_usec = (long)(tmpres % 1000000UL); } if (NULL != tz) { if (!tzflag) { _tzset(); tzflag++; } // Adjust for the timezone west of Greenwich long t = 0; int d = 0; _get_timezone(&t); _get_daylight(&d); tz->tz_minuteswest = t / 60; tz->tz_dsttime = d; } return 0; }
bool DateTime::IsDayLight() { #ifdef MEDUSA_WINDOWS //return _daylight!=0; int daylight; _get_daylight(&daylight); return daylight != 0; #else return daylight; /*time_t rawtime; time ( &rawtime ); tm localDate = localtime(&rawtime); return localDate.tm_isdst;*/ #endif }
int gettimeofday(struct timeval *tv, struct timezone *tz) { FILETIME ft; UINT64 tmpres = 0; static int tzflag; if (NULL != tv) { GetSystemTimeAsFileTime(&ft); tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; /*convert into microseconds*/ tmpres /= 10; /*converting file time to unix epoch*/ tmpres -= DELTA_EPOCH_IN_MICROSECS; tv->tv_sec = (long)(tmpres / 1000000UL); tv->tv_usec = (long)(tmpres % 1000000UL); } if (NULL != tz) { long ttz; int tdl; if (!tzflag) { _tzset(); tzflag++; } _get_timezone(&ttz); _get_daylight(&tdl); tz->tz_minuteswest = ttz / 60; tz->tz_dsttime = tdl; } return 0; }
int gettimeofday (struct timeval *tv, struct timezone *tz) { struct timespec ts; static int tzflag; if (tv) { clock_gettime (0, &ts); tv->tv_sec = (long) ts.tv_sec; tv->tv_usec = ts.tv_nsec / 1000; } if (tz) { if (!tzflag) { _tzset (); tzflag++; } _get_timezone (&tz->tz_minuteswest); tz->tz_minuteswest /= 60; _get_daylight (&tz->tz_dsttime); } return (0); }
int gettimeofday64(struct timeval64*tv, struct timezone*tz) { static int tzflag; if (tv != nullptr) { // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL); SYSTEMTIME system_time; FILETIME file_time; uint64_t time; GetSystemTime(&system_time); SystemTimeToFileTime(&system_time, &file_time); time = ((uint64_t)file_time.dwLowDateTime); time += ((uint64_t)file_time.dwHighDateTime) << 32; tv->tv_sec = (long long) ((time - EPOCH) / 10000000L); tv->tv_usec = (long) (system_time.wMilliseconds * 1000); } if (tz != nullptr) { if (!tzflag) { _tzset(); tzflag++; } long timezoneseconds = 0; _get_timezone(&timezoneseconds); tz->tz_minuteswest = (int)(timezoneseconds / 60); int dsthours = 0; _get_daylight(&dsthours); tz->tz_dsttime = dsthours; } return 0; }
int gettimeofday(struct timeval *tv, struct timezone *tz) { FILETIME ft; unsigned __int64 tmpres = 0; static int tzflag = 0; if (NULL != tv) { GetSystemTimeAsFileTime(&ft); tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; ULARGE_INTEGER test = { ft.dwLowDateTime, ft.dwHighDateTime }; /*converting file time to unix epoch*/ tmpres -= DELTA_EPOCH_IN_MICROSECS; tmpres /= 10; /*convert into microseconds*/ tv->tv_sec = (long)(tmpres / 1000000UL); tv->tv_usec = (long)(tmpres % 1000000UL); } if (NULL != tz) { if (!tzflag) { _tzset(); tzflag++; } long tztmp = 0; _get_timezone(&tztmp); tz->tz_minuteswest = tztmp / 60; _get_daylight(&tz->tz_dsttime); } return 0; }
int gettimeofday(struct timeval*tv, struct timezone*tz) { FILETIME ft; unsigned __int64 tmpres = 0; static int tzflag; if (tv != nullptr) { GetSystemTimeAsFileTime(&ft); tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; /*converting file time to unix epoch*/ tmpres -= DELTA_EPOCH_IN_MICROSECS; tmpres /= 10; /*convert into microseconds*/ tv->tv_sec = (long)(tmpres / 1000000UL); tv->tv_usec = (long)(tmpres % 1000000UL); } if (tz != nullptr) { if (!tzflag) { _tzset(); tzflag++; } long timezoneseconds = 0; _get_timezone(&timezoneseconds); tz->tz_minuteswest = (int)(timezoneseconds / 60); int dsthours = 0; _get_daylight(&dsthours); tz->tz_dsttime = dsthours; } return 0; }
int gettimeofday(struct timeval * tv, struct timezone * tz) { //define a structure to receive the current Windows filetime FILETIME ft; //initialize the present time to 0 and the timezone to UTC unsigned __int64 tmpres = 0; static int tzflag = 0; if(NULL != tv) { GetSystemTimeAsFileTime(&ft); tmpres |= ft.dwHighDateTime; tmpres <<= 32; tmpres |= ft.dwLowDateTime; tmpres /= 10; tmpres -= DELTA_EPOCH_IN_MICROSECS; tv->tv_sec = (long)(tmpres / 1000000UL); tv->tv_usec = (long)(tmpres % 1000000UL); } if(NULL != tz) { long tzdiff; int tzdaylight; if(!tzflag) { _tzset(); tzflag++; } //Adjust for the timezone west of Greenwich _get_timezone(&tzdiff); _get_daylight(&tzdaylight); //tz->tz_minuteswest = _timezone / 60; //tz->tz_dsttime = _daylight; tz->tz_minuteswest = tzdiff / 60; tz->tz_dsttime = tzdaylight; } return 0; }
//================================================================================================ //-------------+++--> Format T-Clock's OutPut String From Current Date, Time, & System Information: unsigned MakeFormat(char buf[FORMAT_MAX_SIZE], const char* fmt, SYSTEMTIME* pt, int beat100) //------------------+++--> { const char* bufend = buf+FORMAT_MAX_SIZE; const char* pos; char* out = buf; ULONGLONG TickCount = 0; while(*fmt) { if(*fmt == '"') { for(++fmt; *fmt&&*fmt!='"'; ) *out++ = *fmt++; if(*fmt) ++fmt; continue; } if(*fmt=='\\' && fmt[1]=='n') { fmt+=2; *out++='\n'; } /// for testing else if(*fmt == 'S' && fmt[1] == 'S' && (fmt[2] == 'S' || fmt[2] == 's')) { fmt += 3; out += api.WriteFormatNum(out, (int)pt->wSecond, 2, 0); *out++ = '.'; out += api.WriteFormatNum(out, (int)pt->wMilliseconds, 3, 0); } else if(*fmt == 'y' && fmt[1] == 'y') { int len; len = 2; if(*(fmt + 2) == 'y' && *(fmt + 3) == 'y') len = 4; out += api.WriteFormatNum(out, (len==2)?(int)pt->wYear%100:(int)pt->wYear, len, 0); fmt += len; } else if(*fmt == 'm') { if(*(fmt + 1) == 'm' && *(fmt + 2) == 'e') { *out++ = m_MonthEng[pt->wMonth-1][0]; *out++ = m_MonthEng[pt->wMonth-1][1]; *out++ = m_MonthEng[pt->wMonth-1][2]; fmt += 3; } else if(fmt[1] == 'm' && fmt[2] == 'm') { if(*(fmt + 3) == 'm') { fmt += 4; for(pos=m_MonthLong; *pos; ) *out++=*pos++; } else { fmt += 3; for(pos=m_MonthShort; *pos; ) *out++=*pos++; } } else { if(fmt[1] == 'm') { fmt += 2; *out++ = (char)((int)pt->wMonth / 10) + '0'; } else { ++fmt; if(pt->wMonth > 9) *out++ = (char)((int)pt->wMonth / 10) + '0'; } *out++ = (char)((int)pt->wMonth % 10) + '0'; } } else if(*fmt == 'a' && fmt[1] == 'a' && fmt[2] == 'a') { if(*(fmt + 3) == 'a') { fmt += 4; for(pos=m_DayOfWeekLong; *pos; ) *out++=*pos++; } else { fmt += 3; for(pos=m_DayOfWeekShort; *pos; ) *out++=*pos++; } } else if(*fmt=='d') { if(fmt[1]=='d' && fmt[2]=='e'){ fmt+=3; for(pos=m_DayOfWeekEng[pt->wDayOfWeek]; *pos; ) *out++=*pos++; }else if(fmt[1]=='d' && fmt[2]=='d') { fmt+=3; if(*fmt=='d'){ ++fmt; pos=m_DayOfWeekLong; }else{ pos=m_DayOfWeekShort; } for(; *pos; ) *out++=*pos++; }else{ if(fmt[1]=='d') { fmt+=2; *out++ = (char)((int)pt->wDay / 10) + '0'; }else{ ++fmt; if(pt->wDay > 9) *out++ = (char)((int)pt->wDay / 10) + '0'; } *out++ = (char)((int)pt->wDay % 10) + '0'; } } else if(*fmt=='h') { int hour = pt->wHour; while(hour >= 12) // faster than mod 12 if "hour" <= 24 hour -= 12; if(!hour && !g_bHourZero) hour = 12; if(fmt[1] == 'h') { fmt += 2; *out++ = (char)(hour / 10) + '0'; } else { ++fmt; if(hour > 9) *out++ = (char)(hour / 10) + '0'; } *out++ = (char)(hour % 10) + '0'; } else if(*fmt=='H') { if(fmt[1] == 'H') { fmt += 2; *out++ = (char)(pt->wHour / 10) + '0'; } else { ++fmt; if(pt->wHour > 9) *out++ = (char)(pt->wHour / 10) + '0'; } *out++ = (char)(pt->wHour % 10) + '0'; } else if((*fmt=='w'||*fmt=='W') && (fmt[1]=='+'||fmt[1]=='-')) { char is_12h = (*fmt == 'w'); char is_negative = (*++fmt == '-'); int hour = 0; for(; *++fmt<='9'&&*fmt>='0'; ){ hour *= 10; hour += *fmt-'0'; } if(is_negative) hour = -hour; hour = (pt->wHour + hour)%24; if(hour < 0) hour += 24; if(is_12h){ while(hour >= 12) // faster than mod 12 if "hour" <= 24 hour -= 12; if(!hour && !g_bHourZero) hour = 12; } *out++ = (char)(hour / 10) + '0'; *out++ = (char)(hour % 10) + '0'; } else if(*fmt == 'n') { if(fmt[1] == 'n') { fmt += 2; *out++ = (char)((int)pt->wMinute / 10) + '0'; } else { ++fmt; if(pt->wMinute > 9) *out++ = (char)((int)pt->wMinute / 10) + '0'; } *out++ = (char)((int)pt->wMinute % 10) + '0'; } else if(*fmt == 's') { if(fmt[1] == 's') { fmt += 2; *out++ = (char)((int)pt->wSecond / 10) + '0'; } else { ++fmt; if(pt->wSecond > 9) *out++ = (char)((int)pt->wSecond / 10) + '0'; } *out++ = (char)((int)pt->wSecond % 10) + '0'; } else if(*fmt == 't' && fmt[1] == 't') { fmt += 2; if(pt->wHour < 12) pos = m_AM; else pos = m_PM; while(*pos) *out++ = *pos++; } else if(*fmt == 'A' && fmt[1] == 'M') { if(fmt[2] == '/' && fmt[3] == 'P' && fmt[4] == 'M') { if(pt->wHour < 12) *out++ = 'A'; //--+++--> 2010 - Noon / MidNight Decided Here! else *out++ = 'P'; *out++ = 'M'; fmt += 5; } else if(fmt[2] == 'P' && fmt[3] == 'M') { fmt += 4; if(pt->wHour < 12) pos = m_AM; else pos = m_PM; while(*pos) *out++ = *pos++; } else *out++ = *fmt++; } else if(*fmt == 'a' && fmt[1] == 'm' && fmt[2] == '/' && fmt[3] == 'p' && fmt[4] == 'm') { if(pt->wHour < 12) *out++ = 'a'; else *out++ = 'p'; *out++ = 'm'; fmt += 5; } // internet time else if(*fmt == '@' && fmt[1] == '@' && fmt[2] == '@') { fmt += 3; *out++ = '@'; *out++ = (char)(beat100 / 10000) + '0'; *out++ = (char)((beat100 % 10000) / 1000) + '0'; *out++ = (char)((beat100 % 1000) / 100) + '0'; if(*fmt=='.' && fmt[1]=='@') { fmt += 2; *out++ = '.'; *out++ = (char)((beat100 % 100) / 10) + '0'; if(*fmt=='@'){ ++fmt; *out++ = (char)((beat100 % 10)) + '0'; } } } // alternate calendar else if(*fmt == 'Y' && m_AltYear > -1) { int n = 1; while(*fmt == 'Y') { n *= 10; ++fmt; } if(n < m_AltYear) { n = 1; while(n < m_AltYear) n *= 10; } for(;;) { *out++ = (char)((m_AltYear % n) / (n/10)) + '0'; if(n == 10) break; n /= 10; } } else if(*fmt == 'g') { for(pos=m_EraStr; *pos&&*fmt=='g'; ){ char* p2 = CharNextExA(m_codepage, pos, 0); while(pos != p2) *out++ = *pos++; ++fmt; } while(*fmt == 'g') fmt++; } else if(*fmt == 'L' && strncmp(fmt, "LDATE", 5) == 0) { GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 5; } else if(*fmt == 'D' && strncmp(fmt, "DATE", 4) == 0) { GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 4; } else if(*fmt == 'T' && strncmp(fmt, "TIME", 4) == 0) { GetTimeFormat(LOCALE_USER_DEFAULT, 0, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 4; } else if(*fmt == 'S') { // uptime int width, padding, num; const char* old_fmt = ++fmt; char specifier = api.GetFormat(&fmt, &width, &padding); if(!TickCount) TickCount = api.GetTickCount64(); switch(specifier){ case 'd'://days num = (int)(TickCount/86400000); break; case 'a'://hours total num = (int)(TickCount/3600000); break; case 'h'://hours (max 24) num = (TickCount/3600000)%24; break; case 'n'://minutes num = (TickCount/60000)%60; break; case 's'://seconds num = (TickCount/1000)%60; break; case 'T':{// ST, uptime as h:mm:ss ULONGLONG past = TickCount/1000; int hour, minute; num = past%60; past /= 60; minute = past%60; past /= 60; hour = (int)past; out += api.WriteFormatNum(out, hour, width, padding); *out++ = ':'; out += api.WriteFormatNum(out, minute, 2, 0); *out++ = ':'; width = 2; padding = 0; break;} default: specifier = '\0'; fmt = old_fmt; *out++ = 'S'; } if(specifier) out += api.WriteFormatNum(out, num, width, padding); } else if(*fmt == 'W') { // Week-of-Year struct tm tmnow; time_t ts = time(NULL); localtime_r(&ts, &tmnow); ++fmt; if(*fmt == 's') { // Week-Of-Year Starts Sunday out += strftime(out, bufend-out, "%U", &tmnow); ++fmt; } else if(*fmt == 'm') { // Week-Of-Year Starts Monday out += strftime(out, bufend-out, "%W", &tmnow); ++fmt; } else if(*fmt == 'i') { // ISO-8601 week (1st version by henriko.se, 2nd by White-Tiger) int wday,borderdays,week; for(;;){ wday = (!tmnow.tm_wday?6:tmnow.tm_wday-1); // convert from Sun-Sat to Mon-Sun (0-5) borderdays = (tmnow.tm_yday + 7 - wday) % 7; // +7 to prevent it from going negative week = (tmnow.tm_yday + 6 - wday) / 7; if(borderdays >= 4){ // year starts with at least 4 days ++week; } else if(!week){ // we're still in last year's week --tmnow.tm_year; tmnow.tm_mon = 11; tmnow.tm_mday = 31; tmnow.tm_isdst = -1; if(mktime(&tmnow)==-1){ // mktime magically updates tm_yday, tm_wday week = 1; break; // fail safe } tmnow.tm_mon = 0; // just to speed up the "if" below, since we know that it can't be week 1 continue; // repeat (once) } if(tmnow.tm_mon==11 && tmnow.tm_mday>=29){ // end of year, could be week 1 borderdays = 31 - tmnow.tm_mday + wday; if(borderdays < 3) week = 1; } break; } out += wsprintf(out,"%d",week); ++fmt; } else if(*fmt == 'u') { int week = 1 + (tmnow.tm_yday + 6 - tmnow.tm_wday) / 7; out += wsprintf(out,"%d",week); ++fmt; } else if(*fmt == 'w') { // SWN (Simple Week Number) out += wsprintf(out,"%d",1 + tmnow.tm_yday / 7); ++fmt; } } //================================================================================================ //======================================= JULIAN DATE Code ======================================== else if(*fmt == 'J' && *(fmt + 1) == 'D') { double y, M, d, h, m, s, bc, JD; struct tm Julian; int id, is, i=0; char* szJulian; time_t UTC = time(NULL); gmtime_r(&UTC, &Julian); y = Julian.tm_year +1900; // Year M = Julian.tm_mon +1; // Month d = Julian.tm_mday; // Day h = Julian.tm_hour; // Hours m = Julian.tm_min; // Minutes s = Julian.tm_sec; // Seconds // This Handles the January 1, 4713 B.C up to bc = 100.0 * y + M - 190002.5; // Year 0 Part. JD = 367.0 * y; JD -= floor(7.0*(y + floor((M+9.0)/12.0))/4.0); JD += floor(275.0*M/9.0); JD += d; JD += (h + (m + s/60.0)/60.0)/24.0; JD += 1721013.5; // BCE 2 November 18 00:00:00.0 UT - Tuesday JD -= 0.5*bc/fabs(bc); JD += 0.5; szJulian = _fcvt(JD, 4, &id, &is); // Make it a String while(*szJulian) { if(i == id) { //--//-++-> id = Decimal Point Precision/Position *out++ = '.'; // ReInsert the Decimal Point Where it Belongs. } else { *out++ = *szJulian++; //--+++--> Done! } i++; } fmt +=2; } //================================================================================================ //======================================= ORDINAL DATE Code ======================================= else if(*fmt == 'O' && *(fmt + 1) == 'D') { //--------+++--> Ordinal Date UTC: char szOD[16] = {0}; struct tm today; time_t UTC = time(NULL); char* od; gmtime_r(&UTC, &today); strftime(szOD, 16, "%Y-%j", &today); od = szOD; while(*od) *out++ = *od++; fmt +=2; } //========================================================================== else if(*fmt == 'O' && *(fmt + 1) == 'd') { //------+++--> Ordinal Date Local: char szOD[16] = {0}; struct tm today; time_t ts = time(NULL); char* od; localtime_r(&ts, &today); strftime(szOD, 16, "%Y-%j", &today); od = szOD; while(*od) *out++ = *od++; fmt +=2; } //========================================================================== else if(*fmt == 'D' && strncmp(fmt, "DOY", 3) == 0) { //--+++--> Day-Of-Year: char szDoy[8] = {0}; struct tm today; time_t ts = time(NULL); char* doy; localtime_r(&ts, &today); strftime(szDoy, 8, "%j", &today); doy = szDoy; while(*doy) *out++ = *doy++; fmt +=3; } //========================================================================== else if(*fmt == 'P' && strncmp(fmt, "POSIX", 5) == 0) { //-> Posix/Unix Time: char szPosix[16] = {0}; // This will Give the Number of Seconds That Have char* posix; //--+++--> Elapsed Since the Unix Epoch: 1970-01-01 00:00:00 wsprintf(szPosix, "%ld", time(NULL)); posix = szPosix; while(*posix) *out++ = *posix++; fmt +=5; } //========================================================================== else if(*fmt == 'T' && strncmp(fmt, "TZN", 3) == 0) { //--++-> TimeZone Name: #ifndef __GNUC__ /* forces us to link with msvcr100 */ char szTZName[TZNAME_MAX] = {0}; size_t lRet; char* tzn; int iDST; _get_daylight(&iDST); if(iDST) { _get_tzname(&lRet, szTZName, TZNAME_MAX, 1); } else { _get_tzname(&lRet, szTZName, TZNAME_MAX, 0); } tzn = szTZName; while(*tzn) *out++ = *tzn++; #endif fmt +=3; } //================================================================================================= else { for(pos=CharNext(fmt); fmt!=pos; ) *out++=*fmt++; } } *out='\0'; return (unsigned)(out-buf); }
static errno_t __cdecl common_localtime_s( tm* const ptm, TimeType const* const ptime ) throw() { typedef __crt_time_time_t_traits<TimeType> time_traits; _VALIDATE_RETURN_ERRCODE(ptm != nullptr, EINVAL); memset(ptm, 0xff, sizeof(tm)); _VALIDATE_RETURN_ERRCODE(ptime != nullptr, EINVAL); // Check for illegal time_t value: _VALIDATE_RETURN_ERRCODE_NOEXC(*ptime >= 0, EINVAL); _VALIDATE_RETURN_ERRCODE_NOEXC(*ptime <= time_traits::max_time_t, EINVAL); __tzset(); int daylight = 0; long dstbias = 0; long timezone = 0; _ERRCHECK(_get_daylight(&daylight)); _ERRCHECK(_get_dstbias (&dstbias )); _ERRCHECK(_get_timezone(&timezone)); if (*ptime > 3 * _DAY_SEC && *ptime < time_traits::max_time_t - 3 * _DAY_SEC) { // The date does not fall within the first three or last three representable // days; therefore, there is no possibility of overflowing or underflowing // the time_t representation as we compensate for time zone and daylight // savings time. TimeType ltime = *ptime - timezone; errno_t status0 = time_traits::gmtime_s(ptm, <ime); if (status0 != 0) return status0; // Check and adjust for daylight savings time: if (daylight && _isindst(ptm)) { ltime -= dstbias; errno_t const status1 = time_traits::gmtime_s(ptm, <ime); if (status1 != 0) return status1; ptm->tm_isdst = 1; } } else { // The date falls within the first three or last three representable days; // therefore, it is possible that the time_t representation would overflow // or underflow while compensating for time zone and daylight savings time. // Therefore, we make the time zone and daylight savings time adjustments // directly in the tm structure. errno_t const status0 = time_traits::gmtime_s(ptm, ptime); if (status0 != 0) return status0; TimeType ltime = static_cast<TimeType>(ptm->tm_sec); // First, adjust for the time zone: if (daylight && _isindst(ptm)) { ltime -= (timezone + dstbias); ptm->tm_isdst = 1; } else { ltime -= timezone; } ptm->tm_sec = static_cast<int>(ltime % 60); if (ptm->tm_sec < 0) { ptm->tm_sec += 60; ltime -= 60; } ltime = static_cast<TimeType>(ptm->tm_min) + ltime / 60; ptm->tm_min = static_cast<int>(ltime % 60); if (ptm->tm_min < 0) { ptm->tm_min += 60; ltime -= 60; } ltime = static_cast<TimeType>(ptm->tm_hour) + ltime / 60; ptm->tm_hour = static_cast<int>(ltime % 24); if (ptm->tm_hour < 0) { ptm->tm_hour += 24; ltime -=24; } ltime /= 24; if (ltime > 0) { // There is no possibility of overflowing the tm_day and tm_yday // members because the date can be no later than January 19. ptm->tm_wday = (ptm->tm_wday + static_cast<int>(ltime)) % 7; ptm->tm_mday += static_cast<int>(ltime); ptm->tm_yday += static_cast<int>(ltime); } else if (ltime < 0) { // It is possible to underflow the tm_mday and tm_yday fields. If // this happens, then the adjusted date must lie in December 1969: ptm->tm_wday = (ptm->tm_wday + 7 + static_cast<int>(ltime)) % 7; ptm->tm_mday += static_cast<int>(ltime); if (ptm->tm_mday <= 0) { ptm->tm_mday += 31; // Per assumption #4 above, the time zone can cause the date to // underflow the epoch by more than a day. ptm->tm_yday = ptm->tm_yday + static_cast<int>(ltime) + 365; ptm->tm_mon = 11; ptm->tm_year--; } else { ptm->tm_yday += static_cast<int>(ltime); } } } return 0; }