📄 ncbitime.cpp
字号:
if (Day() < 1 || Day() > s_DaysInMonth[Month() - 1]) return false; } if (Hour() < 0 || Hour() > 23) return false; if (Minute() < 0 || Minute() > 59) return false; if (Second() < 0 || Second() > 59) return false; if (NanoSecond() < 0 || NanoSecond() >= kNanoSecondsPerSecond) return false; return true;}CTime& CTime::ToTime(ETimeZone tz){ if (GetTimeZoneFormat() != tz) { struct tm* t; time_t timer; timer = GetTimeT(); if (timer == -1) return *this; // MT-Safe protect CFastMutexGuard LOCK(s_TimeMutex);#if defined(HAVE_LOCALTIME_R) struct tm temp; if (tz == eLocal) { localtime_r(&timer, &temp); } else { gmtime_r(&timer, &temp); } t = &temp;#else t = ( tz == eLocal ) ? localtime(&timer) : gmtime(&timer);#endif m_Year = t->tm_year + 1900; m_Month = t->tm_mon + 1; m_Day = t->tm_mday; m_Hour = t->tm_hour; m_Minute = t->tm_min; m_Second = t->tm_sec; m_Tz = tz; } return *this;}bool CTime::operator == (const CTime& t) const{ CTime tmp(t); tmp.ToTime(GetTimeZoneFormat()); return Year() == tmp.Year() && Month() == tmp.Month() && Day() == tmp.Day() && Hour() == tmp.Hour() && Minute() == tmp.Minute() && Second() == tmp.Second() && NanoSecond() == tmp.NanoSecond();}bool CTime::operator > (const CTime& t) const{ CTime tmp(t); tmp.ToTime(GetTimeZoneFormat()); if (Year() > tmp.Year()) return true; if (Year() < tmp.Year()) return false; if (Month() > tmp.Month()) return true; if (Month() < tmp.Month()) return false; if (Day() > tmp.Day()) return true; if (Day() < tmp.Day()) return false; if (Hour() > tmp.Hour()) return true; if (Hour() < tmp.Hour()) return false; if (Minute() > tmp.Minute()) return true; if (Minute() < tmp.Minute()) return false; if (Second() > tmp.Second()) return true; if (Second() < tmp.Second()) return false; if (NanoSecond() > tmp.NanoSecond()) return true; return false;}bool CTime::operator < (const CTime& t) const{ CTime tmp(t); tmp.ToTime(GetTimeZoneFormat()); if (Year() < tmp.Year()) return true; if (Year() > tmp.Year()) return false; if (Month() < tmp.Month()) return true; if (Month() > tmp.Month()) return false; if (Day() < tmp.Day()) return true; if (Day() > tmp.Day()) return false; if (Hour() < tmp.Hour()) return true; if (Hour() > tmp.Hour()) return false; if (Minute() < tmp.Minute()) return true; if (Minute() > tmp.Minute()) return false; if (Second() < tmp.Second()) return true; if (Second() > tmp.Second()) return false; if (NanoSecond() > tmp.NanoSecond()) return true; return false;}bool CTime::IsLeap(void) const{ int year = Year(); return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;}CTime& CTime::Truncate(void){ m_Hour = 0; m_Minute = 0; m_Second = 0; m_NanoSecond = 0; m_AdjustTimeDiff = 0; return *this;}CTime& CTime::Clear(){ m_Year = 0; m_Month = 0; m_Day = 0; Truncate(); return *this;}int CTime::DiffSecond(const CTime& t) const{ int dSec = Second() - t.Second(); int dMin = Minute() - t.Minute(); int dHour = Hour() - t.Hour(); int dDay = (*this) - t; return dSec + 60 * dMin + 60 * 60 * dHour + 60 * 60 * 24 * dDay;}void CTime::x_AdjustDay(){ int n_days = DaysInMonth(); if (Day() > n_days) { m_Day = n_days; }}CTime& CTime::x_AdjustTime(const CTime& from, bool shift_time){ if ( !x_NeedAdjustTime() ) return *this; switch ( GetTimeZonePrecision() ) { case eMinute: if (Minute() != from.Minute()) return x_AdjustTimeImmediately(from, shift_time); case eHour: if (Hour() != from.Hour()) return x_AdjustTimeImmediately(from, shift_time); case eDay: if (Day() != from.Day()) return x_AdjustTimeImmediately(from, shift_time); case eMonth: if (Month() != from.Month()) return x_AdjustTimeImmediately(from, shift_time); default: break; } return *this;} CTime& CTime::x_AdjustTimeImmediately(const CTime& from, bool shift_time){ // Time in hours for temporary time shift. // Shift used for obtainment correct result at changeover daytime saving. // Must be > 3 (Linux distinction). On other platforms may be == 3. const int kShift = 4; // MT-Safe protect CFastMutexGuard LOCK(s_TimeAdjustMutex); // Special conversion from <const CTime> to <CTime> CTime tmp(from); int sign = 0; int diff = 0; // Primary procedure call if ( shift_time ) { sign = ( *this > from ) ? 1 : -1; // !!! Run TimeZoneDiff() first for old time value diff = -tmp.TimeZoneDiff() + TimeZoneDiff(); // Correction need's if time already in identical timezone if (!diff || diff == m_AdjustTimeDiff) { return *this; } } // Recursive procedure call. Inside below // x_AddHour(*, eAdjustDaylight, false) else { // Correction need't if difference not found if (diff == m_AdjustTimeDiff) { return *this; } } // Make correction with temporary time shift time_t t = GetTimeT(); CTime tn(t + diff + 3600 * kShift * sign); if (from.GetTimeZoneFormat() == eLocal) { tn.ToLocalTime(); } tn.SetTimeZonePrecision(GetTimeZonePrecision()); // Release adjust time mutex LOCK.Release(); // Primary procedure call if ( shift_time ) { // Cancel temporary time shift tn.x_AddHour(-kShift * sign, eAdjustDaylight, false); tn.m_AdjustTimeDiff = diff; } *this = tn; return *this;}//=============================================================================//// CStopWatch////=============================================================================double CStopWatch::GetTimeMark(){#if defined(NCBI_OS_MSWIN) // For Win32, we use QueryPerformanceCounter() LARGE_INTEGER bigint; static double freq; static bool first = true; if ( first ) { LARGE_INTEGER nfreq; QueryPerformanceFrequency(&nfreq); freq = nfreq.QuadPart; first = false; } if ( !QueryPerformanceCounter(&bigint) ) { return 0.0; } return double(bigint.QuadPart) / freq;#else // For Unixes, we use gettimeofday() struct timeval time; if ( gettimeofday (&time, 0) ) { return 0.0; } return double(time.tv_sec) + double(time.tv_usec) / 1e6;#endif}//============================================================================//// Extern////============================================================================// Return difference between times <t1> and <t2> as number of daysint operator - (const CTime& t1, const CTime& t2){ return (int) (s_Date2Number(t1) - s_Date2Number(t2));}CTime AddYear(const CTime& t, int years){ CTime tmp(t); return tmp.AddYear(years);} CTime AddMonth(const CTime& t, int months){ CTime tmp(t); return tmp.AddMonth(months);} CTime AddDay(const CTime& t, int days){ CTime tmp(t); return tmp.AddDay(days);} CTime AddHour(const CTime& t, int hours){ CTime tmp(t); return tmp.AddHour(hours);} CTime AddMinute(const CTime& t, int minutes){ CTime tmp(t); return tmp.AddMinute(minutes);} CTime AddSecond(const CTime& t, int seconds){ CTime tmp(t); return tmp.AddSecond(seconds);} CTime AddNanoSecond(const CTime& t, long nanoseconds){ CTime tmp(t); return tmp.AddNanoSecond(nanoseconds);}CTime operator + (const CTime& t, int days){ CTime tmp = s_Number2Date(s_Date2Number(t) + days, t); tmp.x_AdjustTime(t); return tmp;} CTime operator + (int days, const CTime& t){ CTime tmp = s_Number2Date(s_Date2Number(t) + days, t); tmp.x_AdjustTime(t); return tmp;} CTime operator - (const CTime& t, int days){ CTime tmp = s_Number2Date(s_Date2Number(t) - days, t); tmp.x_AdjustTime(t); return tmp;} CTime CurrentTime(CTime::ETimeZone tz, CTime::ETimeZonePrecision tzp){ return CTime(CTime::eCurrent,tz,tzp);} CTime Truncate(const CTime& t){ CTime tmp(t); return tmp.Truncate();}END_NCBI_SCOPE/* * =========================================================================== * $Log: ncbitime.cpp,v $ * Revision 1000.4 2004/06/01 19:09:28 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.49 * * Revision 1.49 2004/05/14 13:59:27 gorelenk * Added include of ncbi_pch.hpp * * Revision 1.48 2004/03/26 16:05:29 ivanov * Format symbol 'z' and output time zone for AsString are unsupported * on NCBI_OS_DARWIN and NCBI_OS_BSD (problems with determinition daylight * flag for specified time). * * Revision 1.47 2004/03/25 13:03:31 ivanov * Temporary fix for NCBI_OS_DARWIN and NCBI_OS_BSD * * Revision 1.46 2004/03/24 18:47:47 ivanov * Replaced abs() with if-clause * * Revision 1.45 2004/03/24 15:52:08 ivanov * Added new format symbol support 'z' (local time in format GMT{+|-}HHMM). * Added second parameter to AsString() method that specify an output * timezone. * * Revision 1.44 2003/11/25 19:55:49 ivanov * Added setters for various components of time -- Set*(). * Added YearWeekNumber(), MonthWeekNumber(). * Reimplemented AddYear() as AddMonth(years*12). * Changed DayOfWeek() to use other computation method. * * Revision 1.43 2003/11/21 20:04:23 ivanov * + DaysInMonth() * * Revision 1.42 2003/10/06 13:59:01 ivanov * Some cosmetics * * Revision 1.41 2003/10/06 13:30:37 ivanov * Fixed cut&paste bug in the MonthNumToName() * * Revision 1.40 2003/10/03 18:27:06 ivanov * Added month and day of week names conversion functions * * Revision 1.39 2003/09/29 21:20:17 golikov * x_Init: create empty CTime obj if str.empty() true, ignore format * * Revision 1.38 2003/07/15 20:09:22 ivanov * Fixed some memory leaks * * Revision 1.37 2003/07/15 19:37:03 vakatov * CTime::x_Init() -- recognize (but then just skip, ignoring the value) * the weekday * * Revision 1.36 2003/04/23 21:07:31 ivanov * CStopWatch::GetTimeMark: removed 'f' to avoid a conversion float * 1e6f to double. * * Revision 1.35 2003/04/16 20:28:00 ivanov * Added class CStopWatch * * Revision 1.34 2003/04/04 16:02:38 lavr * Lines wrapped at 79th column; some minor reformatting * * Revision 1.33 2003/04/03 14:15:48 rsmith * combine pp symbols NCBI_COMPILER_METROWERKS & _MSL_USING_MW_C_HEADERS * into NCBI_COMPILER_MW_MSL * * Revision 1.32 2003/04/02 16:22:34 rsmith * clean up metrowerks ifdefs. * * Revision 1.31 2003/04/02 13:31:18 rsmith * change #ifdefs to allow compilation on MacOSX w/codewarrior * using MSL headers. * * Revision 1.30 2003/02/10 17:17:30 lavr * Fix off-by-one bug in DayOfWeek() calculation * * Revision 1.29 2002/10/24 15:16:37 ivanov * Fixed bug with using two obtainments of the current time in x_SetTime(). * * Revision 1.28 2002/10/18 20:13:01 ivanov * Get rid of some Sun Workshop compilation warnings * * Revision 1.27 2002/10/17 16:55:30 ivanov * Added new time format symbols - 'b' and 'B' (month abbreviated and full name) * * Revision 1.26 2002/09/19 20:05:43 vasilche * Safe initialization of static mutexes * * Revision 1.25 2002/08/19 14:02:24 ivanov * MT-safe fix for ToTime() * * Revision 1.24 2002/07/23 19:51:17 lebedev * NCBI_OS_MAC: GetTimeT fix * * Revision 1.23 2002/07/15 18:17:25 gouriano * renamed CNcbiException and its descendents * * Revision 1.22 2002/07/11 14:18:28 gouriano * exceptions replaced by CNcbiException-type ones * * Revision 1.21 2002/06/26 20:47:45 lebedev * Darwin specific: ncbitime changes * * Revision 1.20 2002/06/19 17:18:05 ucko * Fix confusing indentation introduced by R1.19. * * Revision 1.19 2002/06/19 16:18:36 lebedev * Added CoreServices.h for Darwin (timezone and daylight * * Revision 1.18 2002/06/18 16:08:01 ivanov * Fixed all clauses "#if defined *" to "#if defined(*)" * * Revision 1.17 2002/05/13 13:56:46 ivanov * Added MT-Safe support * * Revision 1.16 2002/04/11 21:08:04 ivanov * CVS log moved to end of the file * * Revision 1.15 2002/03/25 17:08:17 ucko * Centralize treatment of Cygwin as Unix rather than Windows in configure. * * Revision 1.14 2002/03/22 19:59:29 ucko * Use timegm() when available [fixes FreeBSD build]. * Tweak to work on Cygwin. * * Revision 1.13 2001/07/23 16:05:57 ivanov * Fixed bug in Get/Set DB-time formats (1 day difference) * * Revision 1.12 2001/07/06 15:11:11 ivanov * Added support DataBase-time's -- GetTimeDBI(), GetTimeDBU() * SetTimeDBI(), SetTimeDBU() * * Revision 1.11 2001/06/20 14:46:17 vakatov * Get rid of the '^M' symbols introduced in the R1.10 log * * Revision 1.10 2001/06/19 23:03:46 juran * Replace timezone and daylight with macros * Implement for Mac OS * Note: This compiles, but it may not work correctly yet. * * Revision 1.9 2001/05/29 20:14:03 ivanov * Added #include <sys/time.h> for UNIX platforms. * * Revision 1.8 2001/05/29 16:14:01 ivanov * Return to nanosecond-revision. Corrected mistake of the work with local * time on Linux. Polish and improvement source code. * Renamed AsTimeT() -> GetTimerT(). * * Revision 1.7 2001/05/17 15:05:00 lavr * Typos corrected * * Revision 1.6 2001/04/30 22:01:30 lavr * Rollback to pre-nanosecond-revision due to necessity to use * configure to figure out names of global variables governing time zones * * Revision 1.5 2001/04/29 03:06:09 lavr * #include <time.h>" moved from .cpp to ncbitime.hpp * * Revision 1.4 2001/04/27 20:38:14 ivanov * Support for Local and UTC time added. * Support for work with nanoseconds added. * * Revision 1.3 2001/01/03 17:53:05 butanaev * Fixed bug in SetCurrent() * * Revision 1.2 2000/11/21 18:14:58 butanaev * Fixed bug in operator ++/-- (int) * * Revision 1.1 2000/11/20 22:17:46 vakatov * Added NCBI date/time class CTime ("ncbitime.[ch]pp") and * its test suite ("test/test_ncbitime.cpp") * * =========================================================================== */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -