📄 ptime.cxx
字号:
PString fmt, dsep; PString tsep = GetTimeSeparator(); BOOL is12hour = GetTimeAMPM(); switch (format & 7) { case LongDateTime : case LongTime : case MediumDateTime : case ShortDateTime : case ShortTime : if (!is12hour) fmt = "h"; fmt += "h" + tsep + "mm"; switch (format) { case LongDateTime : case LongTime : fmt += tsep + "ss"; default : break; } if (is12hour) fmt += "a"; break; default : break; } switch (format & 7) { case LongDateTime : case MediumDateTime : case ShortDateTime : fmt += ' '; break; default : break; } switch (format & 7) { case LongDateTime : case LongDate : fmt += "wwww "; switch (GetDateOrder()) { case MonthDayYear : fmt += "MMMM d, yyyy"; break; case DayMonthYear : fmt += "d MMMM yyyy"; break; case YearMonthDay : fmt += "yyyy MMMM d"; } break; case MediumDateTime : case MediumDate : fmt += "www "; switch (GetDateOrder()) { case MonthDayYear : fmt += "MMM d, yy"; break; case DayMonthYear : fmt += "d MMM yy"; break; case YearMonthDay : fmt += "yy MMM d"; } break; case ShortDateTime : case ShortDate : dsep = GetDateSeparator(); switch (GetDateOrder()) { case MonthDayYear : fmt += "MM" + dsep + "dd" + dsep + "yy"; break; case DayMonthYear : fmt += "dd" + dsep + "MM" + dsep + "yy"; break; case YearMonthDay : fmt += "yy" + dsep + "MM" + dsep + "dd"; } break; default : break; } if (zone != Local) fmt += " z"; return AsString(fmt, zone);}PString PTime::AsString(const char * format, int zone) const{ PAssert(format != NULL, PInvalidParameter); BOOL is12hour = strchr(format, 'a') != NULL; PStringStream str; str.fill('0'); // the localtime call automatically adjusts for daylight savings time // so take this into account when converting non-local times if (zone == Local) zone = GetTimeZone(); // includes daylight savings time time_t realTime = theTime + zone*60; // to correct timezone struct tm ts; struct tm * t = os_gmtime(&realTime, &ts); PINDEX repeatCount; while (*format != '\0') { repeatCount = 1; switch (*format) { case 'a' : while (*++format == 'a') ; if (t->tm_hour < 12) str << GetTimeAM(); else str << GetTimePM(); break; case 'h' : while (*++format == 'h') repeatCount++; str << setw(repeatCount) << (is12hour ? (t->tm_hour+11)%12+1 : t->tm_hour); break; case 'm' : while (*++format == 'm') repeatCount++; str << setw(repeatCount) << t->tm_min; break; case 's' : while (*++format == 's') repeatCount++; str << setw(repeatCount) << t->tm_sec; break; case 'w' : while (*++format == 'w') repeatCount++; if (repeatCount != 3 || *format != 'e') str << GetDayName((Weekdays)t->tm_wday, repeatCount <= 3 ? Abbreviated : FullName); else { static const char * const EnglishDayName[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; str << EnglishDayName[t->tm_wday]; format++; } break; case 'M' : while (*++format == 'M') repeatCount++; if (repeatCount < 3) str << setw(repeatCount) << (t->tm_mon+1); else if (repeatCount > 3 || *format != 'E') str << GetMonthName((Months)(t->tm_mon+1), repeatCount == 3 ? Abbreviated : FullName); else { static const char * const EnglishMonthName[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; str << EnglishMonthName[t->tm_mon]; format++; } break; case 'd' : while (*++format == 'd') repeatCount++; str << setw(repeatCount) << t->tm_mday; break; case 'y' : while (*++format == 'y') repeatCount++; if (repeatCount < 3) str << setw(2) << (t->tm_year%100); else str << setw(4) << (t->tm_year+1900); break; case 'z' : while (*++format == 'z') ; if (zone == 0) str << "GMT"; else { str << (zone < 0 ? '-' : '+'); zone = PABS(zone); str << setw(2) << (zone/60) << setw(2) << (zone%60); } break; case 'u' : while (*++format == 'u') repeatCount++; switch (repeatCount) { case 1 : str << ((microseconds+50000)/100000); break; case 2 : str << setw(2) << ((microseconds+5000)/10000); break; case 3 : str << setw(3) << ((microseconds+500)/1000); break; default : str << setw(6) << microseconds; break; } break; default : str << *format++; } } return str;}/////////////////////////////////////////////////////////////// Time parser//extern "C" {#ifndef STDAPICALLTYPE#define STDAPICALLTYPE#endiftime_t STDAPICALLTYPE PTimeParse(void *, struct tm *, int);int STDAPICALLTYPE PTimeGetChar(void * stream){ return ((istream *)stream)->get();}void STDAPICALLTYPE PTimeUngetChar(void * stream, int c){ ((istream *)stream)->putback((char)c);}int STDAPICALLTYPE PTimeGetDateOrder(){ return PTime::GetDateOrder();}int STDAPICALLTYPE PTimeIsMonthName(const char * str, int month, int abbrev){ return PTime::GetMonthName((PTime::Months)month, abbrev ? PTime::Abbreviated : PTime::FullName) *= str;}int STDAPICALLTYPE PTimeIsDayName(const char * str, int day, int abbrev){ return PTime::GetDayName((PTime::Weekdays)day, abbrev ? PTime::Abbreviated : PTime::FullName) *= str;}};void PTime::ReadFrom(istream & strm){ time_t now; struct tm timeBuf; time(&now); microseconds = 0; theTime = PTimeParse(&strm, os_localtime(&now, &timeBuf), GetTimeZone());}PTime PTime::operator+(const PTimeInterval & t) const{ PInt64 msecs = (PInt64)theTime*1000 + t.GetMilliSeconds(); return PTime((time_t)(msecs/1000), (long)(msecs%1000)*1000);}PTime & PTime::operator+=(const PTimeInterval & t){ PInt64 msecs = (PInt64)theTime*1000 + t.GetMilliSeconds(); theTime = (time_t)(msecs/1000); microseconds = (long)(msecs%1000)*1000; return *this;}PTimeInterval PTime::operator-(const PTime & t) const{ time_t secs = theTime - t.theTime; long usecs = microseconds - t.microseconds; if (usecs < 0) { usecs += 1000000; secs--; } else if (usecs >= 1000000) { usecs -= 1000000; secs++; } return PTimeInterval(usecs/1000, secs);}PTime PTime::operator-(const PTimeInterval & t) const{ time_t secs = theTime - t.GetSeconds(); long msecs = (long)(microseconds/1000 - t.GetMilliSeconds()%1000); if (msecs < 0) { msecs += 1000; secs--; } else if (msecs >= 1000) { msecs -= 1000; secs++; } return PTime(secs, msecs*1000);}PTime & PTime::operator-=(const PTimeInterval & t){ theTime -= t.GetSeconds(); microseconds -= (long)(t.GetMilliSeconds()%1000)*1000; if (microseconds < 0) { microseconds += 1000000; theTime--; } else if (microseconds >= 1000000) { microseconds -= 1000000; theTime++; } return *this;}// End Of File ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -