NCBI C++ ToolKit
ncbitime.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CORELIB__NCBITIME__HPP
2 #define CORELIB__NCBITIME__HPP
3 
4 /* $Id: ncbitime.hpp 69255 2015-10-14 17:54:55Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Anton Butanayev, Denis Vakatov, Vladimir Ivanov
30  *
31  * DayOfWeek(): Used code has been posted on comp.lang.c on March 10th, 1993
32  * by Tomohiko Sakamoto (sakamoto@sm.sony.co.jp).
33  *
34  */
35 
36 /// @file ncbitime.hpp
37 /// Defines:
38 /// CTimeFormat - storage class for time format.
39 /// CTime - standard Date/Time class to represent an absolute time.
40 /// CTimeSpan - class to represents a relative time span.
41 /// CStopWatch - stop watch class to measure elapsed time.
42 /// CTimeout - timeout interval for various I/O etc activity.
43 /// CNanoTimeout - timeout interval with nanoseconds precision.
44 /// CDeadline - timeout that use absolute time mark (deadline time).
45 /// CFastLocalTime - class for quick and dirty getting a local time.
46 ///
47 /// NOTE about CTime:
48 ///
49 /// - The time zone database in most Windows-based computer systems usually
50 /// stores only a single start and end rule for each zone, regardless
51 /// of year. One of the problems of this approach is that using time-zone
52 /// information will get incorrect results if referring to a year with
53 /// rules that are different from those currently in the database.
54 ///
55 /// - Do not use Local time and time_t and its dependent functions with
56 /// dates outside the range January 1, 1970 UTC to January 18, 2038 UTC.
57 /// Also avoid to use universal->local time conversion functions.
58 ///
59 /// - Do not use DataBase conversion functions for dates
60 /// prior to January 1, 1900.
61 
62 #include <corelib/ncbistd.hpp>
63 
64 
66 
67 /** @addtogroup Time
68  *
69  * @{
70  */
71 
72 // Forward declarations
73 class CTimeSpan;
75 
76 /// Number of seconds.
77 typedef Int8 TSeconds;
78 
79 
80 /// Number of nanoseconds in one second.
81 ///
82 /// Interval for it is from 0 to 999,999,999.
83 const long kNanoSecondsPerSecond = 1000000000;
84 
85 /// Number of microseconds in one second.
86 ///
87 /// Interval for it is from 0 to 999,999.
88 const long kMicroSecondsPerSecond = 1000000;
89 
90 /// Number milliseconds in one second.
91 ///
92 /// Interval for it is from 0 to 999.
93 const long kMilliSecondsPerSecond = 1000;
94 
95 /// The average length of the year in the Gregorian (modern) calendar (in days)
96 const double kAverageDaysPerYear = 365.2425;
97 /// The average length of the year in the Gregorian (modern) calendar (in seconds)
98 const TSeconds kAverageSecondsPerYear = 31556952; // 365.2425*24*3600
99 const TSeconds kAverageSecondsPerMonth = kAverageSecondsPerYear/12;
100 
101 
102 // Time formats in databases (always contain local time only!)
103 
104 /// Database format for time where day and time is unsigned 16 bit.
105 typedef struct {
106  Uint2 days; ///< Days from 1/1/1900
107  Uint2 time; ///< Minutes from the beginning of current day
109 
110 /// Database format for time where day and time is signed 32 bit.
111 typedef struct {
112  Int4 days; ///< days from 1/1/1900
113  Int4 time; ///< x/300 seconds from the beginning of current day
115 
116 
117 
118 /////////////////////////////////////////////////////////////////////////////
119 ///
120 /// CTimeFormat --
121 ///
122 /// Defines a storage class for time format.
123 ///
124 /// See CTime::SetFormat() and CTimeSpan::SetFormat() for description
125 /// of format symbols for specific class.
126 
128 {
129 public:
130  /// Flags.
131  ///
132  /// @note
133  /// It not specified otherwise, format have fDefault value "by default",
134  /// that mean simple format string and strict matching.
135  /// @sa SetFormat, AsString
136  enum EFlags {
137  // Format flags
138 
139  /// Use single characters as format symbols.
140  /// (default)
141  fFormat_Simple = (1 << 0),
142  /// Specify each format symbol with a preceding symbol '$'.
143  /// This can be useful if your format string includes output characters
144  /// that otherwise can be treated as format symbols.
145  /// To include symbol '$' use '$$'.
146  fFormat_Ncbi = (1 << 1),
147 
148  // Time string parsing flags
149 
150  /// A time string should strictly match the format string.
151  fMatch_Strict = (1 << 5), ///< eg "Y" and "1997"
152  /// A time/format string can have extra trailing format symbols,
153  /// that do not have matching symbols in the time string.
154  /// Any missed time components will be initialized by default
155  /// in the time object.
156  fMatch_ShortTime = (1 << 6), ///< eg "Y/M/D h:m:s" and "1997"
157  fMatch_ShortFormat = (1 << 7), ///< eg "Y" and "1997/07/16"
158  /// Combination of both modes above.
159  /// Note that it matches until time or format string have symbols.
160  /// It not allow unprocessed symbols left in both time and format
161  /// strings at the same time.
162  fMatch_Weak = fMatch_ShortTime | fMatch_ShortFormat,
163  /// Ignore all white spaces in the time and format strings
164  /// on the matching/parsing step (CTime only).
165  /// Use it for backward compatibility with old code only.
166  /// Don't recommended to use, can lead to odd results and incorrect
167  /// time on parsing some time strings.
168  fMatch_IgnoreSpaces = (1 << 8),
169 
170  /// Prefer "UTC" over "GMT" abbreviation for universal time.
171  /// Used for output purposes only, parsing accept both.
172  fConf_UTC = (1 << 9),
173 
174  /// Default flags
175  fDefault = 0, // fFormat_Simple | fMatch_Strict
176 
177  /// "Enum"s, used for backward compatibility. Please use flags instead.
178  eNcbiSimple = fFormat_Simple,
179  eNcbi = fFormat_Ncbi,
180  eDefault = fDefault
181  };
182  typedef unsigned int TFlags; ///< Binary OR of "EFlags"
183 
184  /// Predefined formats.
185  ///
186  /// @sa GetPredefined, CTime::SetFormat
187  enum EPredefined {
188  // ISO 8601 formats (without time zone)
189  eISO8601_Year = 0, ///< Y (eg 1997)
190  eISO8601_YearMonth = 1, ///< Y-M (eg 1997-07)
191  eISO8601_Date = 2, ///< Y-M-D (eg 1997-07-16)
192  eISO8601_DateTimeMin = 3, ///< Y-M-DTh:m (eg 1997-07-16T19:20)
193  eISO8601_DateTimeSec = 4, ///< Y-M-DTh:m:s (eg 1997-07-16T19:20:30)
194  eISO8601_DateTimeFrac = 5 ///< Y-M-DTh:m:g (eg 1997-07-16T19:20:30.1234)
195  };
196 
197  /// Default constructor.
198  CTimeFormat(void);
199 
200  /// Copy constructor.
201  CTimeFormat(const CTimeFormat& fmt);
202 
203  /// Constructor.
204  ///
205  /// @sa SetFormat
206  CTimeFormat(const char* fmt, TFlags flags = fDefault);
207 
208  /// Constructor.
209  ///
210  /// @sa SetFormat
211  CTimeFormat(const string& fmt, TFlags flags = fDefault);
212 
213  /// Assignment operator.
214  CTimeFormat& operator= (const CTimeFormat& fmt);
215 
216  /// Set the current time format.
217  ///
218  /// @param fmt
219  /// String of symbols describing the time format.
220  /// @param flags
221  /// Flags specifying how to match a time string against format string.
222  /// @sa
223  /// GetFormat, EFormat, EFlags
224  void SetFormat(const char* fmt, TFlags flags = fDefault);
225 
226  /// Set the current time format.
227  ///
228  /// @param fmt
229  /// String of symbols describing the time format.
230  /// @param flags
231  /// Flags specifying how to match a time string against format string.
232  /// @sa
233  /// GetFormat, EFormat, CTime::SetFormat, CTimeSpan::SetFormat
234  void SetFormat(const string& fmt, TFlags flags = fDefault);
235 
236  /// Get format string.
237  ///
238  /// @return
239  /// A string of symbols describing the time format.
240  /// @sa SetFormat, GetFlags
241  const string& GetString(void) const;
242 
243  /// Get format flags.
244  ///
245  /// @return
246  /// A flags specifying how to match a time string against format string.
247  /// @sa SetFormat, GetString
248  TFlags GetFlags(void) const;
249 
250  /// Check that format string is empty.
251  bool IsEmpty(void) const;
252 
253  /// Get predefined format.
254  /// @param fmt
255  /// String of symbols describing the time format.
256  /// @param fmt_type
257  /// Specify type of the format string.
258  /// @return
259  /// A time format object.
260  /// @sa EPredefined, SetFormat
261  static CTimeFormat GetPredefined(EPredefined fmt, TFlags flags = fDefault);
262 
263 public:
264  /// Return time format as string.
265  /// Note: This method added temporarily, and will be deleted soon.
266  /// @deprecated Use CTimeFormat::GetString()/GetFormat() methods instead.
267  NCBI_DEPRECATED operator string(void) const;
268 
269 private:
270  string m_Str; ///< String format.
271  TFlags m_Flags; ///< Format flags.
272 };
273 
274 
275 /////////////////////////////////////////////////////////////////////////////
276 ///
277 /// CTime --
278 ///
279 /// Defines a standard Date/Time class.
280 ///
281 /// Can be used to span time (to represent elapsed time). Can operate with
282 /// local and universal (GMT/UTC) time. The time is kept in class in
283 /// the format in which it was originally given.
284 ///
285 /// Throw exception of type CTimeException on errors.
286 ///
287 /// NOTE: Do not use local time with time span and dates < "1/1/1900"
288 /// (use universal time only!!!).
289 
291 {
292 public:
293  /// Which initial value to use for time.
294  enum EInitMode {
295  eCurrent, ///< Use current time
296  eEmpty ///< Use "empty" time
297  };
298 
299  /// Which initial value to use for timezone.
300  enum ETimeZone {
301  eLocal = 1, ///< Local time
302  eUTC, ///< UTC (Universal Coordinated Time)
303  eGmt = eUTC, ///< GMT (Greenwich Mean Time)
304  };
305 
306  /// Current timezone. Used in AsString() method.
307  enum {
308  eCurrentTimeZone = -1
309  };
310 
311  /// Which format use to get name of month or week of day.
312  enum ENameFormat {
313  eFull, ///< Use full name.
314  eAbbr ///< Use abbreviated name.
315  };
316 
317  /// Month names.
318  enum EMonth {
319  eJanuary = 1,
330  eDecember
331  };
332 
333  /// Day of week names.
334  enum EDayOfWeek {
335  eSunday = 0,
341  eSaturday
342  };
343 
344  /// What time zone precision to use for adjusting daylight saving time.
345  ///
346  /// Controls when (if ever) to adjust for the daylight saving time
347  /// (only if the time is represented in local timezone format).
348  ///
349  /// NOTE: if diff between previous time value and the time
350  /// after manipulation is greater than this range, then try apply
351  /// daylight saving conversion on the result time value.
353  eNone, ///< Daylight saving not to affect time manipulations.
354  eMinute, ///< Check condition - new minute.
355  eHour, ///< Check condition - new hour.
356  eDay, ///< Check condition - new day.
357  eMonth, ///< Check condition - new month.
358  eTZPrecisionDefault = eNone
359  };
360 
361  /// Whether to adjust for daylight saving time.
362  enum EDaylight {
363  eIgnoreDaylight, ///< Ignore daylight saving time.
364  eAdjustDaylight, ///< Adjust for daylight saving time.
365  eDaylightDefault = eAdjustDaylight
366  };
367 
368  /// Constructor.
369  ///
370  /// @param mode
371  /// Whether to build time object with current time or empty
372  /// time (default).
373  /// @param tz
374  /// Whether to use local time (default) or UTC.
375  /// @param tzp
376  /// What time zone precision to use.
377  CTime(EInitMode mode = eEmpty,
378  ETimeZone tz = eLocal,
379  ETimeZonePrecision tzp = eTZPrecisionDefault);
380 
381  /// Conversion constructor for time_t representation of time.
382  ///
383  /// Construct time object from UTC time_t value.
384  /// The constructed object will be in the eUTC format.
385  ///
386  /// @param t
387  /// Time in the UTC time_t format.
388  /// @param tzp
389  /// What time zone precision to use.
390  /// @sa SetTimeT, GetTimeT
391  explicit CTime(time_t t, ETimeZonePrecision tzp = eTZPrecisionDefault);
392 
393  /// Conversion constructor for "struct tm" local time representation.
394  ///
395  /// Construct time object from "struct tm" time value.
396  /// The constructed object will be in the eLocal format.
397  ///
398  /// @param t
399  /// Time in "struct tm" format.
400  /// @param tzp
401  /// What time zone precision to use.
402  /// @sa SetTimeTM, GetTimeTM
403  CTime(const struct tm& t, ETimeZonePrecision tzp = eTZPrecisionDefault);
404 
405  /// Constructor.
406  ///
407  /// Construct time given the year, month, day, hour, minute, second,
408  /// nanosecond parts of a time value.
409  ///
410  /// @param year
411  /// Year part of time.
412  /// @param month
413  /// Month part of time. Note month starts from 1.
414  /// @param day
415  /// Day part of time. Note day starts from 1.
416  /// @param hour
417  /// Hour part of time.
418  /// @param minute
419  /// Minute part of time.
420  /// @param second
421  /// Second part of time.
422  /// @param nanosecond
423  /// Nanosecond part of time.
424  /// @param tz
425  /// Whether to use local time (default) or UTC.
426  /// @param tzp
427  /// What time zone precision to use.
428  CTime(int year, int month, int day,
429  int hour = 0, int minute = 0, int second = 0, long nanosecond = 0,
430  ETimeZone tz = eLocal,
431  ETimeZonePrecision tzp = eTZPrecisionDefault);
432 
433  /// Constructor.
434  ///
435  /// Construct date as N-th day of the year.
436  ///
437  /// @param year
438  /// Year part of date.
439  /// @param yearDayNumber
440  /// N-th day of the year.
441  /// @param tz
442  /// Whether to use local time (default) or UTC.
443  /// @param tzp
444  /// What time zone precision to use.
445  CTime(int year, int yearDayNumber,
446  ETimeZone tz = eLocal,
447  ETimeZonePrecision tzp = eTZPrecisionDefault);
448 
449  /// Explicit conversion constructor for string representation of time.
450  ///
451  /// Construct time object from string representation of time.
452  ///
453  /// @param str
454  /// String representation of time in format "fmt".
455  /// @param fmt
456  /// Format in which "str" is presented. Default value of kEmptyStr,
457  /// implies the format, that was previously setup using SetFormat()
458  /// method, or default "M/D/Y h:m:s".
459  /// @param tz
460  /// If current format contains 'Z', then objects timezone will be set to:
461  /// - eUTC if "str" has words "GMT", "UTC" or "Z" (zero UTC offset) in
462  /// the appropriate position;
463  /// - eLocal otherwise.
464  /// If current format does not contain 'Z', objects timezone
465  /// will be set to 'tz' value.
466  /// @sa AsString, operator=
467  explicit CTime(const string& str, const CTimeFormat& fmt = kEmptyStr,
468  ETimeZone tz = eLocal,
469  ETimeZonePrecision tzp = eTZPrecisionDefault);
470 
471  /// Copy constructor.
472  CTime(const CTime& t);
473 
474  /// Assignment operator.
475  CTime& operator= (const CTime& t);
476 
477  /// Assignment operator from string.
478  ///
479  /// If current format contains 'Z', then objects timezone will be set to:
480  /// - eUTC if "str" has words "GMT", "UTC" or "Z" (zero UTC offset) in
481  /// the appropriate position;
482  /// - eLocal otherwise.
483  /// If current format does not contain 'Z', objects timezone
484  /// will not be changed.
485  /// @note
486  /// This operator expect a string in the format, that was previously
487  /// set using SetFormat() method.
488  /// @sa
489  /// CTime constructor from string, AsString
490  CTime& operator= (const string& str);
491 
492  /// Set time using time_t time value.
493  ///
494  /// @param t
495  /// Time to set in time object. This is always in UTC time format, and
496  /// nanoseconds will be truncated.
497  /// @return
498  /// Time object that is set.
499  CTime& SetTimeT(const time_t t);
500 
501  /// Get time in time_t format.
502  ///
503  /// The function return the number of seconds elapsed since midnight
504  /// (00:00:00), January 1, 1970 UTC. Do not use this function if the year
505  /// is before 1970.
506  /// @return
507  /// Time in time_t format.
508  time_t GetTimeT(void) const;
509 
510  /// Get current UTC time in time_t format (with nanoseconds).
511  ///
512  /// @param sec
513  /// The function return the number of seconds elapsed since
514  /// midnight (00:00:00), January 1, 1970 UTC.
515  /// @param nanosec
516  /// Number of nanoseconds (0, if not possible to get).
517  /// @attention
518  /// Result can differs from time(NULL) due different implementation
519  /// and taking into account nanosecond part on some platforms (rounding).
520  static void GetCurrentTimeT(time_t* sec, long* nanosec = 0);
521 
522  /// Set time using "struct tm" time value.
523  ///
524  /// @param t
525  /// Time to set in time object. This time always represents a local
526  /// time in current time zone. Time object will be set to have eLocal
527  /// time format, and nanoseconds will be truncated. Note that all
528  /// significant fields in the time structure should be set and have
529  /// correct vales, otherwise exception will be thrown.
530  /// @return
531  /// Time object that is set.
532  CTime& SetTimeTM(const struct tm& t);
533 
534  /// Get time in "struct tm" format.
535  ///
536  /// @return
537  /// Time in "struct tm" format (local time).
538  struct tm GetTimeTM(void) const;
539 
540  /// Set time using database format time, TDBTimeU.
541  ///
542  /// Object's time format will always change to eLocal after call.
543  ///
544  /// @param t
545  /// Time to set in time object in TDBTimeU format.
546  /// This is always in local time format, and seconds, and nanoseconds
547  /// will be truncated.
548  /// @return
549  /// Time object that is set.
550  CTime& SetTimeDBU(const TDBTimeU& t);
551 
552  /// Set time using database format time, TDBTimeI.
553  ///
554  /// Object's time format will always change to eLocal after call.
555  ///
556  /// @param t
557  /// Time to set in time object in TDBTimeI format.
558  /// This is always in local time format, and seconds, and nanoseconds
559  /// will be truncated.
560  /// @return
561  /// Time object that is set.
562  CTime& SetTimeDBI(const TDBTimeI& t);
563 
564  /// Get time in database format time, TDBTimeU.
565  ///
566  /// @return
567  /// Time value in database format, TDBTimeU.
568  TDBTimeU GetTimeDBU(void) const;
569 
570  /// Get time in database format time, TDBTimeI.
571  ///
572  /// @return
573  /// Time value in database format TDBTimeI.
574  TDBTimeI GetTimeDBI(void) const;
575 
576  /// Make the time current in the presently active time zone.
577  CTime& SetCurrent(void);
578 
579  /// Make the time "empty",
580  CTime& Clear(void);
581 
582  /// Set the current time format.
583  ///
584  /// The default format is: "M/D/Y h:m:s".
585  /// @param fmt
586  /// An object contains string of symbols describing the time
587  /// format and its type. The following format letters have
588  /// the special meaning:
589  /// - Y = year with century
590  /// - y = year without century (00-99)
591  /// - M = month as decimal number (01-12)
592  /// - B = full month name (January-December)
593  /// - b = abbreviated month name (Jan-Dec)
594  /// - D = day as decimal number (01-31)
595  /// - d = day as decimal number (w/o 0) (1-31)
596  /// - H = hour in 12-hour format (00-12)
597  /// - h = hour in 24-hour format (00-23)
598  /// - m = minute as decimal number (00-59)
599  /// - s = second as decimal number (00-59)
600  /// - l = milliseconds as decimal number (000-999)
601  /// - r = microseconds as decimal number (000000-999999)
602  /// - S = nanosecond as decimal number (000000000-999999999)
603  /// - G = seconds and fraction part of second as double value
604  /// ("s.nnn") with floating number (1-9) of digits after dot.
605  /// - g = same as "G" but w/o leading "0" if number of seconds < 10.
606  /// - P = am/pm (AM/PM)
607  /// - p = am/pm (am/pm)
608  /// - W = full day of week name (Sunday-Saturday)
609  /// - w = abbreviated day of week name (Sun-Sat)
610  /// - Z = UTC timezone designator (GMT/UTC/Z or none)
611  ///
612  /// following available on POSIX platforms only:
613  /// - z = timezone shift ([GMT/UTC]+/-HHMM)
614  /// - o = timezone shift (+/-HH:MM)
615  ///
616  /// Format string can represent date/time partially, in this case
617  /// current time, or default values, will be used to amplify time
618  /// object, if possible. Current date/time cannot be used
619  /// if format string contains "z" (time shift) format symbol.
620  /// Also, it cannot be used if time format is ambiguous, like "Y/D".
621  /// Note, that you still can use "Y/M", or even "Y", where month and
622  /// day will be defined as 1; or "M/D", where year will be set as the
623  /// current year.
624  /// @sa
625  /// CTimeFormat, GetFormat, AsString
626  static void SetFormat(const CTimeFormat& fmt);
627 
628  /// Get the current time format.
629  ///
630  /// The default format is: "M/D/Y h:m:s".
631  /// @return
632  /// An object describing the time format.
633  /// @sa
634  /// CTimeFormat, SetFormat, AsString
635  static CTimeFormat GetFormat(void);
636 
637  /// Get numerical value of the month by name.
638  ///
639  /// @param month
640  /// Full or abbreviated month name.
641  /// @return
642  /// Numerical value of a given month (1..12).
643  /// @sa
644  /// MonthNumToName, Month
645  static int MonthNameToNum(const string& month);
646 
647  /// Get name of the month by numerical value.
648  ///
649  /// @param month
650  /// Full or abbreviated month name.
651  /// @param format
652  /// Format for returned value (full or abbreviated).
653  /// @return
654  /// Name of the month.
655  /// @sa
656  /// MonthNameToNum, Month
657  static string MonthNumToName(int month, ENameFormat format = eFull);
658 
659  /// Get numerical value of the day of week by name.
660  ///
661  /// @param day
662  /// Full or abbreviated day of week name.
663  /// @return
664  /// Numerical value of a given day of week (0..6).
665  /// @sa
666  /// DayOfWeekNumToName, DayOfWeek
667  static int DayOfWeekNameToNum(const string& day);
668 
669  /// Get name of the day of week by numerical value.
670  ///
671  /// @param day
672  /// Full or abbreviated day of week name.
673  /// @param format
674  /// Format for returned value (full or abbreviated).
675  /// @return
676  /// Name of the day of week.
677  /// @sa
678  /// DayOfWeekNameToNum, DayOfWeek
679  static string DayOfWeekNumToName(int day, ENameFormat format = eFull);
680 
681  /// Transform time to string.
682  ///
683  /// @param format
684  /// Format specifier used to convert time to string.
685  /// If "format" is not defined, then GetFormat() will be used.
686  /// @param out_tz
687  /// Output timezone. This is a difference in seconds between universal
688  /// and local time for some place (for example, for EST5 timezone
689  /// its value is 18000). This parameter works only with local time.
690  /// If the time object contains universal (GMT/UTC/Z) time it is ignored.
691  /// Before doing transformation to string, the time will be converted
692  /// to the output timezone. Timezone can be printed as a string
693  /// 'GMT/UTC/Z[+|-]HHMM' using the format symbol 'z'.
694  /// By default the current timezone is used.
695  /// @sa
696  /// GetFormat, SetFormat
697  string AsString(const CTimeFormat& format = kEmptyStr,
698  TSeconds out_tz = eCurrentTimeZone) const;
699 
700  /// Return time as string using the format returned by GetFormat().
701  operator string(void) const;
702 
703 
704  //
705  // Get various components of time.
706  //
707 
708  /// Get year.
709  ///
710  /// Year = 1900 ..
711  /// AsString() format symbols "Y", "y".
712  int Year(void) const;
713 
714  /// Get month.
715  ///
716  /// Month number = 1..12.
717  /// AsString() format symbols "M", "B", "b".
718  int Month(void) const;
719 
720  /// Get day.
721  ///
722  /// Day of the month = 1..31
723  /// AsString() format symbol "D".
724  int Day(void) const;
725 
726  /// Get hour.
727  ///
728  /// Hours since midnight = 0..23.
729  /// AsString() format symbol "h".
730  int Hour(void) const;
731 
732  /// Get minute.
733  ///
734  /// Minutes after the hour = 0..59
735  /// AsString() format symbol "m".
736  int Minute(void) const;
737 
738  /// Get second.
739  ///
740  /// Seconds after the minute = 0..59
741  /// AsString() format symbol "s".
742  int Second(void) const;
743 
744  /// Get milliseconds.
745  ///
746  /// Milliseconds after the second = 0..999
747  /// AsString() format symbol "l".
748  /// @sa
749  /// NanoSecond
750  long MilliSecond(void) const;
751 
752  /// Get microseconds.
753  ///
754  /// Microseconds after the second = 0..999999
755  /// AsString() format symbol "r".
756  /// @sa
757  /// NanoSecond
758  long MicroSecond(void) const;
759 
760  /// Get nano-seconds.
761  ///
762  /// Nano-seconds after the second = 0..999999999
763  /// AsString() format symbol "S".
764  /// @sa
765  /// MilliSecond, MicroSecond
766  long NanoSecond(void) const;
767 
768 
769  //
770  // Set various components of time.
771  //
772 
773  /// Set year.
774  ///
775  /// Beware that this operation is inherently inconsistent.
776  /// In case of different number of days in the months, the day number
777  /// can change, e.g.:
778  /// - "Feb 29 2000".SetYear(2001) => "Feb 28 2001".
779  /// Because 2001 is not leap year.
780  /// @param year
781  /// Year to set.
782  /// @sa
783  /// Year
784  void SetYear(int year);
785 
786  /// Set month.
787  ///
788  /// Beware that this operation is inherently inconsistent.
789  /// In case of different number of days in the months, the day number
790  /// can change, e.g.:
791  /// - "Dec 31 2000".SetMonth(2) => "Feb 29 2000".
792  /// Therefore e.g. calling SetMonth(1) again that result will be "Jan 28".
793  /// @param month
794  /// Month number to set. Month number = 1..12.
795  /// @sa
796  /// Month
797  void SetMonth(int month);
798 
799  /// Set day.
800  ///
801  /// Beware that this operation is inherently inconsistent.
802  /// In case of number of days in the months, the day number
803  /// can change, e.g.:
804  /// - "Feb 01 2000".SetDay(31) => "Feb 29 2000".
805  /// @param day
806  /// Day to set. Day of the month = 1..31.
807  /// @sa
808  /// Day
809  void SetDay(int day);
810 
811  /// Set hour.
812  ///
813  /// @param hour
814  /// Hours since midnight = 0..23.
815  /// @sa
816  /// Hour
817  void SetHour(int hour);
818 
819  /// Set minute.
820  ///
821  /// @param minute
822  /// Minutes after the hour = 0..59.
823  /// @sa
824  /// Minute
825  void SetMinute(int minute);
826 
827  /// Set second.
828  ///
829  /// @param second
830  /// Seconds after the minute = 0..59.
831  /// @sa
832  /// Second
833  void SetSecond(int second);
834 
835  /// Set milliseconds.
836  ///
837  /// @param millisecond
838  /// Milliseconds after the second = 0..999.
839  /// @sa
840  /// MilliSecond, SetNanoSecond
841  void SetMilliSecond(long millisecond);
842 
843  /// Set microseconds.
844  ///
845  /// @param microsecond
846  /// Microseconds after the second = 0..999999.
847  /// @sa
848  /// MicroSecond, SetNanoSecond
849  void SetMicroSecond(long microsecond);
850 
851  /// Set nanoseconds.
852  ///
853  /// @param nanosecond
854  /// Nanoseconds after the second = 0..999999999.
855  /// @sa
856  /// NanoSecond, SetMilliSecond, SetMicroSecond
857  void SetNanoSecond(long nanosecond);
858 
859  /// Get year's day number.
860  ///
861  /// Year day number = 1..366
862  int YearDayNumber(void) const;
863 
864  /// Get this date's week number within the year.
865  ///
866  /// Calculate the week number in a year of a given date.
867  /// The week can start on any day accordingly given parameter.
868  /// First week always start with 1st January.
869  /// @param week_start
870  /// What day of week is first.
871  /// Default is to use Sunday as first day of week. For Monday-based
872  /// weeks use eMonday as parameter value.
873  /// @return
874  /// Week number = 1..54.
875  int YearWeekNumber(EDayOfWeek first_day_of_week = eSunday) const;
876 
877  /// Get this date's week number in the month.
878  ///
879  /// @return
880  /// Week number in the month = 1..6.
881  /// @sa
882  /// YearWeekNumber
883  int MonthWeekNumber(EDayOfWeek first_day_of_week = eSunday) const;
884 
885  /// Get day of week.
886  ///
887  /// Days since Sunday = 0..6
888  /// AsString() format symbols "W", "w".
889  int DayOfWeek(void) const;
890 
891  /// Get number of days in the month.
892  ///
893  /// Number of days = 28..31
894  int DaysInMonth(void) const;
895 
896  /// Add specified years and adjust for daylight saving time.
897  ///
898  /// It is an exact equivalent of calling AddMonth(years * 12).
899  /// @sa
900  /// AddMonth
901  CTime& AddYear(int years = 1, EDaylight adl = eDaylightDefault);
902 
903  /// Add specified months and adjust for daylight saving time.
904  ///
905  /// Beware that this operation is inherently inconsistent.
906  /// In case of different number of days in the months, the day number
907  /// can change, e.g.:
908  /// - "Dec 31 2000".AddMonth(2) => "Feb 28 2001" ("Feb 29" if leap year).
909  /// Therefore e.g. calling AddMonth(1) 12 times for e.g. "Jul 31" will
910  /// result in "Jul 28" (or "Jul 29") of the next year.
911  /// @param months
912  /// Months to add. Default is 1 month.
913  /// If negative, it will result in a "subtraction" operation.
914  /// @param adl
915  /// Whether to adjust for daylight saving time. Default is to adjust
916  /// for daylight savings time. This parameter is for eLocal time zone
917  /// and where the time zone precision is not eNone.
918  CTime& AddMonth(int months = 1, EDaylight adl = eDaylightDefault);
919 
920  /// Add specified days and adjust for daylight saving time.
921  ///
922  /// @param days
923  /// Days to add. Default is 1 day.
924  /// If negative, it will result in a "subtraction" operation.
925  /// @param adl
926  /// Whether to adjust for daylight saving time. Default is to adjust
927  /// for daylight saving time. This parameter is for eLocal time zone
928  /// and where the time zone precision is not eNone.
929  CTime& AddDay(int days = 1, EDaylight adl = eDaylightDefault);
930 
931  /// Add specified hours and adjust for daylight saving time.
932  ///
933  /// @param hours
934  /// Hours to add. Default is 1 hour.
935  /// If negative, it will result in a "subtraction" operation.
936  /// @param adl
937  /// Whether to adjust for daylight saving time. Default is to adjust
938  /// for daylight saving time. This parameter is for eLocal time zone
939  /// and where the time zone precision is not eNone.
940  CTime& AddHour(int hours = 1, EDaylight adl = eDaylightDefault);
941 
942  /// Add specified minutes and adjust for daylight saving time.
943  ///
944  /// @param minutes
945  /// Minutes to add. Default is 1 minute.
946  /// If negative, it will result in a "subtraction" operation.
947  /// @param adl
948  /// Whether to adjust for daylight saving time. Default is to adjust
949  /// for daylight saving time. This parameter is for eLocal time zone
950  /// and where the time zone precision is not eNone.
951  CTime& AddMinute(int minutes = 1, EDaylight adl = eDaylightDefault);
952 
953  /// Add specified seconds.
954  ///
955  /// @param seconds
956  /// Seconds to add. Default is 1 second.
957  /// If negative, it will result in a "subtraction" operation.
958  CTime& AddSecond(TSeconds seconds = 1, EDaylight adl = eDaylightDefault);
959 
960  /// Add specified nanoseconds.
961  ///
962  /// @param nanoseconds
963  /// Nanoseconds to add. Default is 1 nanosecond.
964  /// If negative, it will result in a "subtraction" operation.
965  CTime& AddNanoSecond(long nanoseconds = 1);
966 
967  /// Add specified time span.
968  ///
969  /// @param timespan
970  /// Object of CTimeSpan class to add.
971  /// If negative, it will result in a "subtraction" operation.
972  CTime& AddTimeSpan(const CTimeSpan& timespan);
973 
974 
975  /// Precision for rounding time.
976  /// @sa Round, Truncate
978  eRound_Day, ///< Round to days
979  eRound_Hour, ///< Round to hours
980  eRound_Minute, ///< Round to minutes
981  eRound_Second, ///< Round to seconds
982  eRound_Millisecond, ///< Round to milliseconds
983  eRound_Microsecond ///< Round to microseconds
984  };
985 
986  /// Round time.
987  ///
988  /// Round stored time to specified precision. All time components with
989  /// precision less that specified will be zero-filled, all other
990  /// components will be adjusted accordingly to rules for rounding
991  /// numbers.
992  /// @param precision
993  /// Rounding precision.
994  /// @param adl
995  /// Whether to adjust for daylight saving time. Default is to adjust
996  /// for daylight saving time. This parameter is for eLocal time zone
997  /// and where the time zone precision is not eNone.
998  /// @sa ERoundPrecision, Truncate
999  CTime& Round(ERoundPrecision precision = eRound_Day,
1000  EDaylight adl = eDaylightDefault);
1001 
1002  /// Truncate time.
1003  ///
1004  /// Truncate stored time to specified precision. All time components with
1005  /// precision less that specified will be zero-filled.
1006  /// By default method strips hours, minutes, seconds and nanoseconds.
1007  /// @param precision
1008  /// Truncating precision.
1009  /// @sa ERoundPrecision, Round
1010  CTime& Truncate(ERoundPrecision precision = eRound_Day);
1011 
1012 
1013  //
1014  // Add/subtract time span
1015  //
1016 
1017  // Operator to add time span.
1018  CTime& operator+= (const CTimeSpan& ts);
1019 
1020  /// Operator to subtract time span.
1021  CTime& operator-= (const CTimeSpan& ts);
1022 
1023  // Operator to add time span.
1024  CTime operator+ (const CTimeSpan& ts) const;
1025 
1026  /// Operator to subtract time span.
1027  CTime operator- (const CTimeSpan& ts) const;
1028 
1029  /// Operator to subtract times.
1030  CTimeSpan operator- (const CTime& t) const;
1031 
1032 
1033  //
1034  // Time comparison ('>' means "later", '<' means "earlier")
1035  //
1036 
1037  /// Operator to test equality of time.
1038  bool operator== (const CTime& t) const;
1039 
1040  /// Operator to test in-equality of time.
1041  bool operator!= (const CTime& t) const;
1042 
1043  /// Operator to test if time is later.
1044  bool operator> (const CTime& t) const;
1045 
1046  /// Operator to test if time is earlier.
1047  bool operator< (const CTime& t) const;
1048 
1049  /// Operator to test if time is later or equal.
1050  bool operator>= (const CTime& t) const;
1051 
1052  /// Operator to test if time is earlier or equal.
1053  bool operator<= (const CTime& t) const;
1054 
1055 
1056  //
1057  // Time difference
1058  //
1059 
1060  /// Difference in whole days from specified time.
1061  int DiffWholeDays(const CTime& t) const;
1062 
1063  /// Difference in days from specified time.
1064  double DiffDay(const CTime& t) const;
1065 
1066  /// Difference in hours from specified time.
1067  double DiffHour(const CTime& t) const;
1068 
1069  /// Difference in minutes from specified time.
1070  double DiffMinute(const CTime& t) const;
1071 
1072  /// Difference in seconds from specified time.
1073  TSeconds DiffSecond(const CTime& t) const;
1074 
1075  /// Difference in nanoseconds from specified time.
1076  double DiffNanoSecond(const CTime& t) const;
1077 
1078  /// Difference in nanoseconds from specified time.
1079  CTimeSpan DiffTimeSpan(const CTime& t) const;
1080 
1081 
1082  //
1083  // Checks
1084  //
1085 
1086  /// Is time object empty (date and time)?
1087  bool IsEmpty (void) const;
1088 
1089  /// Is date empty?
1090  bool IsEmptyDate (void) const;
1091 
1092  /// Is time in a leap year?
1093  bool IsLeap (void) const;
1094 
1095  /// Is time valid?
1096  bool IsValid (void) const;
1097 
1098  /// Is time local time?
1099  bool IsLocalTime (void) const;
1100 
1101  /// Is time universal (GMT/UTC/Z)?
1102  bool IsUniversalTime(void) const;
1103  bool IsGmtTime(void) const { return IsUniversalTime(); };
1104 
1105  /// Is DST (daylight savings time) in effect for this time?
1106  /// @note
1107  /// This method use current DST rules on current machine, so be aware
1108  /// to use it against time in the past or future, because such rules
1109  /// can be changed.
1110  bool IsDST(void) const;
1111 
1112  /// Validate if string match time format.
1113  ///
1114  /// @param str
1115  /// String representation of time.
1116  /// @param fmt
1117  /// Format that is used to check time string. Default value of kEmptyStr,
1118  /// implies the format, that was previously setup using SetFormat()
1119  /// method, or default one.
1120  /// @return
1121  /// TRUE if CTime can be initialized from "str" using format "fmt",
1122  /// FALSE otherwise.
1123  /// @sa
1124  /// CTimeFormat
1125  static bool ValidateString(const string& str, const CTimeFormat& fmt = kEmptyStr);
1126 
1127 
1128  //
1129  // Timezone functions
1130  //
1131 
1132  /// Get time zone.
1133  ETimeZone GetTimeZone(void) const;
1134  NCBI_DEPRECATED ETimeZone GetTimeZoneFormat(void) const;
1135 
1136  /// Set time zone.
1137  ETimeZone SetTimeZone(ETimeZone val);
1138  NCBI_DEPRECATED ETimeZone SetTimeZoneFormat(ETimeZone val);
1139 
1140  /// Get time zone precision.
1141  ETimeZonePrecision GetTimeZonePrecision(void) const;
1142 
1143  /// Set time zone precision.
1144  ETimeZonePrecision SetTimeZonePrecision(ETimeZonePrecision val);
1145 
1146  /// Get the time as local time.
1147  CTime GetLocalTime(void) const;
1148 
1149  /// Get the time as universal (GMT/UTC) time.
1150  CTime GetUniversalTime(void) const;
1151  CTime GetGmtTime(void) const { return GetUniversalTime(); }
1152 
1153  /// Convert the time into specified time zone time.
1154  CTime& ToTime(ETimeZone val);
1155 
1156  /// Convert the time into local time.
1157  CTime& ToLocalTime(void);
1158 
1159  /// Convert the time into universal (GMT/UTC) time.
1160  CTime& ToUniversalTime(void);
1161  CTime& ToGmtTime(void) { return ToUniversalTime(); };
1162 
1163  /// Get difference between local timezone for current time object
1164  /// and UTC in seconds.
1165  /// @deprecated Use CTime::TimeZoneOffset() instead.
1166  NCBI_DEPRECATED TSeconds TimeZoneDiff(void) const;
1167 
1168  /// Get difference between local timezone for current time object
1169  /// and UTC in seconds.
1170  TSeconds TimeZoneOffset(void) const;
1171 
1172  /// Get time zone offset string in format [+/-]HHMM.
1173  /// @sa TimeZoneName, TimeZoneOffset
1174  string TimeZoneOffsetStr(void);
1175 
1176  /// Get current time zone name.
1177  ///
1178  /// @return
1179  /// String with time zone name for current time, if supported by OS,
1180  /// or empty string otherwise. This name can be any one of 3-letter
1181  /// abbreviated code or some arbitrary string, depending on OS.
1182  /// @sa
1183  /// TimeZoneOffset, TimeZoneOffsetStr
1184  string TimeZoneName(void);
1185 
1186 private:
1187  /// Defines how to behave on error
1188  enum EErrAction {
1189  eErr_Throw, ///< Throw an exception on error
1190  eErr_NoThrow ///< Return default value on error
1191  };
1192 
1193  /// Helper method to set time value from string "str" using format "fmt".
1194  bool x_Init(const string& str, const CTimeFormat& fmt,
1195  EErrAction err_action = eErr_Throw);
1196 
1197  /// Helper method to set time from 'time_t' -- If "t" not specified,
1198  /// then set to current time.
1199  CTime& x_SetTime(const time_t* t = 0);
1200 
1201  /// Version of x_SetTime() with MT-safe locks
1202  CTime& x_SetTimeMTSafe(const time_t* t = 0);
1203 
1204  /// Helper method to adjust day number to correct value after day
1205  /// manipulations.
1206  void x_AdjustDay(void);
1207 
1208  /// Helper method to adjust the time to correct timezone (across the
1209  /// barrier of winter & summer times) using "from" as a reference point.
1210  ///
1211  /// This does the adjustment only if the time object:
1212  /// - contains local time, and
1213  /// - has TimeZonePrecision != CTime::eNone, and
1214  /// - differs from "from" in the TimeZonePrecision (or larger) part.
1215  CTime& x_AdjustTime(const CTime& from, bool shift_time = true);
1216 
1217  /// Helper method to forcibly adjust timezone using "from" as a
1218  /// reference point.
1219  CTime& x_AdjustTimeImmediately(const CTime& from, bool shift_time = true);
1220 
1221  /// Helper method to check if there is a need adjust time in timezone.
1222  bool x_NeedAdjustTime(void) const;
1223 
1224  /// Helper method to add hour with/without shift time.
1225  /// Parameter "shift_time" access or denied use time shift in
1226  /// process adjust hours.
1227  CTime& x_AddHour(int hours = 1, EDaylight daylight = eDaylightDefault,
1228  bool shift_time = true);
1229 
1230 private:
1231 #if defined(NCBI_COMPILER_WORKSHOP) && defined(__x86_64) && NCBI_COMPILER_VERSION < 590
1232 // Work around some WorkShop versions' incorrect handling of bitfields
1233 // when compiling for x86-64 (at least with optimization enabled) by
1234 // not using them at all. :-/
1235 # define NCBI_TIME_BITFIELD(n)
1236 # define NCBI_TIME_EMPTY_BITFIELD
1237 #else
1238 # define NCBI_TIME_BITFIELD(n) : n
1239 # define NCBI_TIME_EMPTY_BITFIELD unsigned : 0;
1240 #endif
1241  typedef struct {
1242  // Time
1243  unsigned int year NCBI_TIME_BITFIELD(12); // 4 digits
1244  unsigned char month NCBI_TIME_BITFIELD( 4); // 0..12
1245  unsigned char day NCBI_TIME_BITFIELD( 5); // 0..31
1246  unsigned char hour NCBI_TIME_BITFIELD( 5); // 0..23
1247  unsigned char min NCBI_TIME_BITFIELD( 6); // 0..59
1248  unsigned char sec NCBI_TIME_BITFIELD( 6); // 0..61
1249  // Difference between universal and local times in seconds,
1250  // as stored during the last call to x_AdjustTime***().
1251  Int4 adjTimeDiff NCBI_TIME_BITFIELD(18);
1252  // Timezone and precision
1253  ETimeZone tz NCBI_TIME_BITFIELD(3); // local/universal
1254  ETimeZonePrecision tzprec NCBI_TIME_BITFIELD(4); // Time zone precision
1255  NCBI_TIME_EMPTY_BITFIELD // Force alignment
1257  } TData;
1258  TData m_Data; ///< Packed members
1259 
1260  // Friend class
1261  friend class CFastLocalTime;
1262 };
1263 
1264 
1265 
1266 /////////////////////////////////////////////////////////////////////////////
1267 ///
1268 /// CTimeSpan
1269 ///
1270 /// Defines a class to represents a relative time span.
1271 /// Time span can be both positive and negative and hold value in range
1272 /// [LONG_MIN - LONG_MAX] seconds (a little more than 68 years in each direction).
1273 ///
1274 /// Throw exception of type CTimeException on errors.
1275 
1277 {
1278 public:
1279  /// Default constructor.
1280  CTimeSpan(void);
1281 
1282  /// Constructor.
1283  ///
1284  /// Construct time span given the number of days, hours, minutes, seconds,
1285  /// nanoseconds parts of a time span value.
1286  /// @param days
1287  /// Day part of time. Note day starts from 1.
1288  /// @param hours
1289  /// Hour part of time.
1290  /// @param minutes
1291  /// Minute part of time.
1292  /// @param seconds
1293  /// Second part of time.
1294  /// @param nanoseconds
1295  /// Nanosecond part of time.
1296  CTimeSpan(long days, long hours, long minutes, long seconds,
1297  long nanoseconds = 0);
1298 
1299  /// Constructor.
1300  ///
1301  /// Construct time span given the number of seconds and nanoseconds.
1302  /// @param seconds
1303  /// Second part of time.
1304  /// @param nanoseconds
1305  /// Nanosecond part of time.
1306  explicit CTimeSpan(long seconds, long nanoseconds = 0);
1307 
1308  /// Constructor.
1309  ///
1310  /// Construct time span from number of seconds.
1311  /// Please, use this constructor as rarely as possible, because after
1312  /// doing some arithmetical operations and conversion with it,
1313  /// the time span can differ at some nanoseconds from expected value.
1314  /// @param seconds
1315  /// Second part of time. The fractional part is used to compute
1316  /// nanoseconds.
1317  explicit CTimeSpan(double seconds);
1318 
1319  /// Explicit conversion constructor for string representation of time span.
1320  ///
1321  /// Construct time span object from string representation of time.
1322  ///
1323  /// @param str
1324  /// String representation of time span in format "fmt".
1325  /// @param fmt
1326  /// Format in which "str" is presented. Default value of kEmptyStr,
1327  /// implies the format set with SetFormat(), or "-G" if SetFormat()
1328  /// has not used before.
1329  /// @sa SetFormat
1330  explicit CTimeSpan(const string& str, const CTimeFormat& fmt = kEmptyStr);
1331 
1332  /// Copy constructor.
1333  CTimeSpan(const CTimeSpan& t);
1334 
1335  /// Assignment operator.
1336  CTimeSpan& operator= (const CTimeSpan& t);
1337 
1338  /// Assignment operator.
1339  CTimeSpan& operator= (const string& str);
1340 
1341  /// Make the time span "empty",
1342  CTimeSpan& Clear(void);
1343 
1344  /// Get sign of time span.
1345  ESign GetSign(void) const;
1346 
1347  /// Set the current time span format.
1348  ///
1349  /// The default format is: "-S.n".
1350  /// @param format
1351  /// An object contains string of letters describing the time
1352  /// format and its type. The format letters have
1353  /// the following meanings:
1354  /// - - = add minus for negative time spans
1355  /// - d = number of whole days
1356  /// - H = total whole number of hours stored in the time span
1357  /// - h = hours, "H" modulo 24 (-23 - 23)
1358  /// - M = total whole number of minutes stored in the time span
1359  /// - m = minutes, "M" modulo 60 (-59 - 59)
1360  /// - S = total whole number of seconds stored in the time span
1361  /// - s = seconds, "S" modulo 60 (-59 - 59)
1362  /// - N = total whole number of nanoseconds stored in the time span
1363  /// - n = nanoseconds (-999999999 - 999999999)
1364  /// - G = total whole number of seconds and part of second as double value
1365  /// ("S.n") with floating number (1-9) of digits after dot.
1366  /// - g = seconds, "S" modulo 60 and part of second as double value
1367  /// ("s.n") with floating number (1-9) of digits after dot.
1368  /// @sa
1369  /// CTimeFormat, GetFormat, AsString
1370  static void SetFormat(const CTimeFormat& format);
1371 
1372  /// Get the current time span format.
1373  ///
1374  /// If SetFormat() has not used before, that default formats are:
1375  /// "-S.n" - to output with AsString();
1376  /// "-G" - to initialize from string.
1377  /// @return
1378  /// An object describing the time format.
1379  /// @sa
1380  /// CTimeFormat, SetFormat, AsString
1381  static CTimeFormat GetFormat(void);
1382 
1383  /// Transform time span to string.
1384  ///
1385  /// @param fmt
1386  /// Format specifier used to convert time span to string.
1387  /// If format is not defined, then GetFormat() will be used.
1388  /// @return
1389  /// A string representation of time span in specified format.
1390  /// @sa
1391  /// CTimeFormat, GetFormat, SetFormat
1392  string AsString(const CTimeFormat& fmt = kEmptyStr) const;
1393 
1394  /// Return span time as string using the format returned by GetFormat().
1395  operator string(void) const;
1396 
1397 
1398  /// AsSmartString() conversion flags.
1399  ///
1400  /// @attention
1401  /// Use one flag in each group only, or CTimeException exception
1402  /// will be thrown. If not specified otherwise default value will be used.
1403  /// @sa AsSmartString
1405  /// @note precision
1406  /// Flags describing how many parts of time span should be
1407  /// returned.
1408  /// - Values from fSS_Year to fSS_Nanosecond apparently
1409  /// describe part of time span which will be last in output string.
1410  /// - Floating precision levels fSS_PrecisionN say that only up to 'N'
1411  /// of most significant parts of time span will be printed.
1412  /// The parts counting begin from first non-zero value.
1413  /// - fSS_Smart is default, it tries to represent time span as close
1414  /// and short as possible, usually using only one or two most
1415  /// significant non-zero parts of the time span.
1416  fSS_Year = (1 << 0), ///< Round to years
1417  fSS_Month = (1 << 1), ///< Round to months
1418  fSS_Day = (1 << 2), ///< Round to days
1419  fSS_Hour = (1 << 3), ///< Round to hours
1420  fSS_Minute = (1 << 4), ///< Round to minutes
1421  fSS_Second = (1 << 5), ///< Round to seconds
1422  fSS_Millisecond = (1 << 6), ///< Round to milliseconds
1423  fSS_Microsecond = (1 << 7), ///< Round to microseconds
1424  fSS_Nanosecond = (1 << 8), ///< Do not round at all (accurate time span)
1425  fSS_Precision1 = (1 << 9), ///< Floating precision level 1
1426  fSS_Precision2 = (1 << 10), ///< Floating precision level 2
1427  fSS_Precision3 = (1 << 11), ///< Floating precision level 3
1428  fSS_Precision4 = (1 << 12), ///< Floating precision level 4
1429  fSS_Precision5 = (1 << 13), ///< Floating precision level 5
1430  fSS_Precision6 = (1 << 14), ///< Floating precision level 6
1431  fSS_Precision7 = (1 << 15), ///< Floating precision level 7
1432  fSS_Smart = (1 << 16), ///< Be as smart as possible (see above)
1433  fSS_PrecisionMask = 0x1FFFF, ///< Mask of precision flags (sum of all above)
1434 
1435  /// @note rounding
1436  /// Rounding flags. By default time span will be truncated at last value
1437  /// specified by precision. If fSS_Round specified, that time span will be
1438  /// arithmetically rounded by precision level.
1439  fSS_Round = (1 << 20),
1440  fSS_Trunc = (1 << 21),
1441 
1442  /// @note zero parts
1443  /// Flags to print or skip zero parts of time span which should be
1444  /// printed but have 0 value. Apply to middle and trailing zero parts
1445  /// of time span only. Leading zeros will be ignored in common case
1446  /// in any mode, they can be printed only if it is impossible
1447  /// to represent time span otherwise.
1448  /// @attention
1449  /// fSS_NoSkipZero is not compatible with fSS_Smart (exception will be thrown).
1450  fSS_NoSkipZero = (1 << 22),
1451  fSS_SkipZero = (1 << 23),
1452 
1453  /// @note naming
1454  /// Specify what kind of names use for output time components,
1455  /// short standard abbreviations for time component like "m",
1456  /// or full "minutes".
1457  fSS_Short = (1 << 24),
1458  fSS_Full = (1 << 25),
1459 
1460  /// Default flags
1461  fSS_Default = 0 // fSS_Smart | fSS_Trunc | fSS_SkipZero | fSS_Full
1462  };
1463  typedef unsigned int TSmartStringFlags; ///< Binary OR of "ESmartStringFlags"
1464 
1465  /// Precision for span "smart" string. Used in AsSmartString() method.
1466  /// @deprecated Use ESmartStringFlags instead
1468  // Named precision levels
1469  eSSP_Year = fSS_Year,
1470  eSSP_Month = fSS_Month,
1471  eSSP_Day = fSS_Day,
1472  eSSP_Hour = fSS_Hour,
1473  eSSP_Minute = fSS_Minute,
1474  eSSP_Second = fSS_Second,
1475  eSSP_Millisecond = fSS_Millisecond,
1476  eSSP_Microsecond = fSS_Microsecond,
1477  eSSP_Nanosecond = fSS_Nanosecond,
1478  eSSP_Precision1 = fSS_Precision1,
1479  eSSP_Precision2 = fSS_Precision2,
1480  eSSP_Precision3 = fSS_Precision3,
1481  eSSP_Precision4 = fSS_Precision4,
1482  eSSP_Precision5 = fSS_Precision5,
1483  eSSP_Precision6 = fSS_Precision6,
1484  eSSP_Precision7 = fSS_Precision7,
1485  eSSP_Smart = fSS_Smart
1486  };
1487 
1488  /// Which format use to output zero time span parts.
1489  /// @deprecated Use ESmartStringFlags instead
1491  eSSZ_NoSkipZero = fSS_NoSkipZero, ///< Print zero valued parts
1492  eSSZ_SkipZero = fSS_SkipZero ///< Skip zero valued parts
1493  };
1494 
1495  /// Transform time span to "smart" string.
1496  ///
1497  /// @deprecated Use AsSmartString(TSmartStringFlags) instead.
1499  string AsSmartString(ESmartStringPrecision precision,
1500  ERound rounding,
1501  ESmartStringZeroMode zero_mode = eSSZ_SkipZero) const;
1502 
1503  /// Transform time span to "smart" string.
1504  ///
1505  /// @param flags
1506  /// How to convert string to value.
1507  /// @return
1508  /// A string representation of time span.
1509  /// It MUST NOT be negative, or an exception will be thrown!
1510  /// @sa
1511  /// AssignFromSmartString, AsString, ESmartStringFlags
1512  /// @note
1513  /// For very big time spans it use the average length of the year
1514  /// in the Gregorian (modern) calendar (365.2425 days)
1515  /// to calculate number of months and years.
1516  string AsSmartString(TSmartStringFlags flags = 0) const;
1517 
1518  /// Assign value to time span object from string representation of time
1519  /// in formats produced by AsSmartString().
1520  ///
1521  /// @param str
1522  /// String representation of time span in the format produced by AsSmartString().
1523  /// All numeric time parts should have letter/word specifiers.
1524  /// The string should represent "positive" timespan, the sign (minus)
1525  /// is not allowed there.
1526  /// @sa
1527  /// AsSmartString
1528  /// @note
1529  /// It use the average length of the year in the Gregorian (modern)
1530  /// calendar (365.2425 days) to convert years and months.
1531  CTimeSpan& AssignFromSmartString(const string& str);
1532 
1533 
1534  //
1535  // Get various components of time span.
1536  //
1537 
1538  /// Get number of complete days.
1539  long GetCompleteDays(void) const;
1540 
1541  /// Get number of complete hours.
1542  long GetCompleteHours(void) const;
1543 
1544  /// Get number of complete minutes.
1545  long GetCompleteMinutes(void) const;
1546 
1547  /// Get number of complete seconds.
1548  long GetCompleteSeconds(void) const;
1549 
1550  /// Get number of nanoseconds.
1551  long GetNanoSecondsAfterSecond(void) const;
1552 
1553  /// Return time span as number of seconds.
1554  ///
1555  /// @return
1556  /// Return representative of time span as type double.
1557  /// The fractional part represents nanoseconds part of time span.
1558  /// The double representation of the time span is aproximate.
1559  double GetAsDouble(void) const;
1560 
1561  /// Return TRUE is an object keep zero time span.
1562  bool IsEmpty(void) const;
1563 
1564  //
1565  // Set time span
1566  //
1567 
1568  /// Set time span in seconds and nanoseconds.
1569  void Set(long seconds, long nanoseconds = 0);
1570 
1571  /// Set time span from number of seconds (fractional value).
1572  void Set(double seconds);
1573 
1574  //
1575  // Arithmetic
1576  //
1577 
1578  // Operator to add time span.
1579  CTimeSpan& operator+= (const CTimeSpan& t);
1580 
1581  // Operator to add time span.
1582  CTimeSpan operator+ (const CTimeSpan& t) const;
1583 
1584  /// Operator to subtract time span.
1585  CTimeSpan& operator-= (const CTimeSpan& t);
1586 
1587  /// Operator to subtract time span.
1588  CTimeSpan operator- (const CTimeSpan& t) const;
1589 
1590  /// Unary operator "-" (minus) to change time span sign.
1591  CTimeSpan operator- (void) const;
1592 
1593  /// Invert time span. Changes time span sign.
1594  void Invert(void);
1595 
1596  //
1597  // Comparison
1598  //
1599 
1600  /// Operator to test equality of time span.
1601  bool operator== (const CTimeSpan& t) const;
1602 
1603  /// Operator to test in-equality of time span.
1604  bool operator!= (const CTimeSpan& t) const;
1605 
1606  /// Operator to test if time span is greater.
1607  bool operator> (const CTimeSpan& t) const;
1608 
1609  /// Operator to test if time span is less.
1610  bool operator< (const CTimeSpan& t) const;
1611 
1612  /// Operator to test if time span is greater or equal.
1613  bool operator>= (const CTimeSpan& t) const;
1614 
1615  /// Operator to test if time span is less or equal.
1616  bool operator<= (const CTimeSpan& t) const;
1617 
1618 private:
1619  /// Get hour.
1620  /// Hours since midnight = -23..23
1621  int x_Hour(void) const;
1622 
1623  /// Get minute.
1624  /// Minutes after the hour = -59..59
1625  int x_Minute(void) const;
1626 
1627  /// Get second.
1628  /// Seconds after the minute = -59..59
1629  int x_Second(void) const;
1630 
1631  /// Helper method to set time value from string "str" using format "fmt".
1632  void x_Init(const string& str, const CTimeFormat& fmt);
1633 
1634  /// Helper method to normalize stored time value.
1635  void x_Normalize(void);
1636 
1637  /// Helpers for AsSmartString()
1638  string x_AsSmartString_Smart_Big(TSmartStringFlags flags) const;
1639  string x_AsSmartString_Smart_Small(TSmartStringFlags flags) const;
1640  string x_AsSmartString_Precision(TSmartStringFlags flags) const;
1641 
1642 private:
1643  long m_Sec; ///< Seconds part of the time span
1644  long m_NanoSec; ///< Nanoseconds after the second
1645 };
1646 
1647 
1648 
1649 /////////////////////////////////////////////////////////////////////////////
1650 ///
1651 /// CTimeout -- Timeout interval
1652 ///
1653 /// @sa CNanoTimeout, STimeout, CConnTimeout, CTimeSpan
1654 /// @note Throw exception of type CTimeException on errors.
1655 
1657 {
1658 public:
1659  /// Type of timeouts.
1660  enum EType {
1661  eFinite, ///< A finite timeout value has been set.
1662  eDefault, ///< Default timeout (to be interpreted by the client code)
1663  eInfinite, ///< Infinite timeout.
1664  eZero ///< Zero timeout, equal to CTimeout(0,0).
1665  };
1666 
1667  /// Create default timeout.
1668  CTimeout(void);
1669 
1670  /// Create timeout of specified type.
1671  CTimeout(EType type);
1672 
1673  /// Initialize timeout from CTimeSpan.
1674  CTimeout(const CTimeSpan& ts);
1675 
1676  /// Initialize timeout in seconds and microseconds.
1677  /// @note
1678  /// Use CNanoTimeout ctor to initialize with (seconds and) nanoseconds
1679  /// @sa CNanoTimeout
1680  CTimeout(unsigned int sec, unsigned int usec);
1681 
1682  /// Initialize timeout from number of seconds (fractional value).
1683  explicit CTimeout(double sec);
1684 
1685  /// Destructor.
1686  ~CTimeout(void) {}
1687 
1688  // Check on special timeout values.
1689  bool IsDefault() const;
1690  bool IsInfinite() const;
1691  bool IsZero() const;
1692  /// Check if timeout holds a numeric value.
1693  bool IsFinite() const;
1694 
1695  //
1696  // Get timeout
1697  //
1698 
1699  /// Get as number of milliseconds.
1700  unsigned long GetAsMilliSeconds(void) const;
1701 
1702  /// Get as number of seconds (fractional value).
1703  double GetAsDouble(void) const;
1704 
1705  /// Convert to CTimeSpan.
1706  CTimeSpan GetAsTimeSpan(void) const;
1707 
1708  /// Get timeout in seconds and microseconds.
1709  void Get(unsigned int *sec, unsigned int *microsec) const;
1710 
1711  /// Get timeout in seconds and nanoseconds.
1712  void GetNano(unsigned int *sec, unsigned int *nanosec) const;
1713 
1714 
1715  //
1716  // Set timeout
1717  //
1718 
1719  /// Set special value.
1720  void Set(EType type);
1721 
1722  /// Set timeout in seconds and microseconds.
1723  void Set(unsigned int sec, unsigned int microsec);
1724 
1725  /// Set timeout in seconds and nanoseconds.
1726  void SetNano(unsigned int sec, unsigned int nanosec);
1727 
1728  /// Set timeout from number of seconds (fractional value).
1729  void Set(double sec);
1730 
1731  /// Set from CTimeSpan.
1732  void Set(const CTimeSpan& ts);
1733 
1734  //
1735  // Comparison.
1736  // eDefault special value cannot be compared with any value.
1737  //
1738 
1739  /// Operator to test equality of timeouts.
1740  bool operator== (const CTimeout& t) const;
1741 
1742  /// Operator to test in-equality of timeouts.
1743  bool operator!= (const CTimeout& t) const;
1744 
1745  /// Operator to test if timeout is greater.
1746  bool operator> (const CTimeout& t) const;
1747 
1748  /// Operator to test if timeout is less.
1749  bool operator< (const CTimeout& t) const;
1750 
1751  /// Operator to test if timeout is greater or equal.
1752  bool operator>= (const CTimeout& t) const;
1753 
1754  /// Operator to test if timeout is less or equal.
1755  bool operator<= (const CTimeout& t) const;
1756 
1757 private:
1758  EType m_Type; ///< Type of timeout.
1759  unsigned int m_Sec; ///< Seconds part of the timeout.
1760  unsigned int m_NanoSec; ///< Nanoseconds part of the timeout.
1761 };
1762 
1763 
1764 
1765 /////////////////////////////////////////////////////////////////////////////
1766 ///
1767 /// CNanoTimeout -- Timeout interval, using nanoseconds
1768 ///
1769 /// @sa CTimeout, STimeout, CConnTimeout, CTimeSpan
1770 /// @note Throw exception of type CTimeException on errors.
1771 
1772 
1774 {
1775 public:
1776  CNanoTimeout(unsigned int seconds, unsigned int nanoseconds)
1777  : CTimeout() {
1778  SetNano(seconds, nanoseconds);
1779  }
1780 };
1781 
1782 
1783 
1784 /////////////////////////////////////////////////////////////////////////////
1785 ///
1786 /// CDeadline
1787 ///
1788 /// Given a relative timeout, compose the absolute time mark
1789 /// by adding the timeout to the current time.
1790 ///
1791 /// @sa CTimeout
1792 
1794 {
1795 public:
1796  /// Initialize deadline using seconds and nanoseconds
1797  /// (adding to the current time)
1798  /// @param seconds
1799  /// Number of seconds to add to the current time
1800  /// @param nanoseconds
1801  /// Number of nanoseconds to add to the current time
1802  CDeadline(unsigned int rel_seconds, unsigned int rel_nanoseconds = 0);
1803 
1804  /// Initialize deadline by adding relative timeout to the current time.
1805  CDeadline(const CTimeout& timeout);
1806 
1807  /// Check if the deadline is infinite.
1808  bool IsInfinite(void) const { return m_Infinite; }
1809 
1810  /// Check if the deadline is expired.
1811  bool IsExpired(void) const
1812  { return !IsInfinite() && GetRemainingTime().IsZero(); }
1813 
1814  /// Get the number of seconds and nanoseconds (since 1/1/1970).
1815  /// Throw an exception if the deadline is infinite.
1816  void GetExpirationTime(time_t* sec, unsigned int* nanosec) const;
1817 
1818  /// Get time left to the expiration.
1819  CNanoTimeout GetRemainingTime(void) const;
1820 
1821  /// Compare two CDeadline values.
1822  bool operator < (const CDeadline& right_hand_operand) const;
1823 
1824 private:
1825  void x_Now(void);
1826  void x_Add(unsigned int seconds, unsigned int nanoseconds);
1827 
1828  time_t m_Seconds;
1829  unsigned int m_Nanoseconds;
1831 };
1832 
1833 
1834 /// @deprecated Use CDeadline instead
1836 
1837 
1838 
1839 /////////////////////////////////////////////////////////////////////////////
1840 ///
1841 /// CFastLocalTime --
1842 ///
1843 /// Define a class for quick and dirty getting a local time.
1844 ///
1845 /// Getting local time may trigger request to a time server daemon,
1846 /// thus potentially causing a relatively significant delay,
1847 /// so we 'll need a caching local timer.
1848 
1850 {
1851 public:
1852  /// Constructor.
1853  /// It should not try to get local time from OS more often than once
1854  /// an hour. Default: check once, 5 seconds after each hour.
1855  CFastLocalTime(unsigned int sec_after_hour = 5);
1856 
1857  /// Get local time
1858  CTime GetLocalTime(void);
1859 
1860  /// Get difference in seconds between UTC and current local time
1861  /// (daylight information included)
1862  int GetLocalTimezone(void);
1863 
1864  /// Do unscheduled check
1865  void Tuneup(void);
1866 
1867 private:
1868  /// Internal version of Tuneup()
1869  bool x_Tuneup(time_t timer, long nanosec);
1870 
1871 private:
1872  unsigned int m_SecAfterHour; ///< Time interval in seconds after hour
1873  ///< in which we should avoid to do Tuneup().
1874  CTime m_LocalTime; ///< Current local time
1875  CTime m_TunedTime; ///< Last tuned time (changed by Tuneup())
1876 
1877  time_t m_LastTuneupTime; ///< Last Tuneup() time
1878  time_t m_LastSysTime; ///< Last system time
1879  int m_Timezone; ///< Cached timezone adjustment for local time
1880  int m_Daylight; ///< Cached system daylight information
1881  void* volatile m_IsTuneup;///< (bool) Tuneup() in progress (MT)
1882 };
1883 
1884 
1885 /////////////////////////////////////////////////////////////////////////////
1886 ///
1887 /// CStopWatch --
1888 ///
1889 /// Define a stop watch class to measure elapsed time.
1890 
1892 {
1893 public:
1894  /// Defines how to create new timer.
1895  enum EStart {
1896  eStart, ///< Start timer immediately after creating.
1897  eStop ///< Do not start timer, just create it.
1898  };
1899 
1900  /// Constructor.
1901  /// NB. By default ctor doesn't start timer, it merely creates it.
1902  CStopWatch(EStart state = eStop);
1903 
1904  /// Constructor.
1905  /// Start timer if argument is true.
1906  /// @deprecated Use CStopWatch(EStat) constructor instead.
1907  NCBI_DEPRECATED_CTOR(CStopWatch(bool start));
1908 
1909  /// Start the timer.
1910  /// Do nothing if already started.
1911  void Start(void);
1912 
1913  /// Return time elapsed since first Start() or last Restart() call
1914  /// (in seconds).
1915  /// Result is 0.0 if Start() or Restart() wasn't previously called.
1916  double Elapsed(void) const;
1917 
1918  /// Suspend the timer.
1919  /// Next Start() call continue to count time accured before.
1920  void Stop(void);
1921 
1922  /// Return time elapsed since first Start() or last Restart() call
1923  /// (in seconds). Start new timer after that.
1924  /// Result is 0.0 if Start() or Restart() wasn't previously called.
1925  double Restart(void);
1926 
1927  /// Stop (if running) and reset the timer.
1928  void Reset(void);
1929 
1930  /// Check state of stopwatch.
1931  /// @return
1932  /// TRUE if stopwatch is "running", FALSE otherwise.
1933  /// @sa
1934  /// Start, Stop
1935  bool IsRunning(void) const;
1936 
1937  /// Set the current stopwatch time format.
1938  ///
1939  /// The default format is: "S.n".
1940  /// @param fmt
1941  /// Format specifier used to convert time span to string.
1942  /// If format is not defined, then GetFormat() will be used.
1943  /// Uses the same time format as CTimeSpan class.
1944  /// @sa
1945  /// CTimeFormat, CTimeSpan::SetFormat, AsString
1946  static void SetFormat(const CTimeFormat& fmt);
1947 
1948  /// Get the current stopwatch time format.
1949  ///
1950  /// The default format is: "S.n".
1951  /// @return
1952  /// An object describing the time format.
1953  /// The letters having the same means that for CTimeSpan.
1954  /// @sa
1955  /// CTimeFormat, CTimeSpan::GetFormat, AsString
1956  static CTimeFormat GetFormat(void);
1957 
1958  /// Transform stopwatch time to string.
1959  ///
1960  /// According to used OS, the double representation can provide much
1961  /// finer grained time control. The string representation is limited
1962  /// by nanoseconds.
1963  /// @param fmt
1964  /// If format is not defined, then GetFormat() will be used.
1965  /// Format specifier used to convert value returned by Elapsed()
1966  /// to string.
1967  /// @sa
1968  /// CTimeSpan::AsString, CTimeFormat, Elapsed, GetFormat, SetFormat
1969  string AsString(const CTimeFormat& fmt = kEmptyStr) const;
1970 
1971  /// Return stopwatch time as string using the format returned
1972  /// by GetFormat().
1973  operator string(void) const;
1974 
1975  /// Transform elapsed time to "smart" string.
1976  ///
1977  /// For more details see CTimeSpan::AsSmartString().
1978  /// @param precision
1979  /// Enum value describing how many parts of time span should be
1980  /// returned.
1981  /// @param rounding
1982  /// Rounding mode.
1983  /// @param zero_mode
1984  /// Mode to print or skip zero parts of time span.
1985  /// @return
1986  /// A string representation of elapsed time span.
1987  /// @sa
1988  /// CTimeSpan::AsSmartString, AsString, Elapsed
1989  /// @deprecated Use AsSmartString(TSmartStringFlags) instead.
1991  string AsSmartString(
1993  ERound rounding,
1995  const;
1996 
1997  /// Transform elapsed time to "smart" string.
1998  ///
1999  /// For more details see CTimeSpan::AsSmartString().
2000  /// @param flags
2001  /// How to convert string to value.
2002  /// @return
2003  /// A string representation of elapsed time span.
2004  /// @sa
2005  /// CTimeSpan::AsSmartString, AsString, Elapsed
2006  string AsSmartString(CTimeSpan::TSmartStringFlags flags = 0) const;
2007 
2008 protected:
2009  /// Get current time mark.
2010  static double GetTimeMark();
2011 
2012 private:
2013  double m_Start; ///< Start time value.
2014  double m_Total; ///< Accumulated elapsed time.
2015  EStart m_State; ///< Stopwatch state (started/stopped)
2016 };
2017 
2018 
2019 
2020 /////////////////////////////////////////////////////////////////////////////
2021 ///
2022 /// CTimeException --
2023 ///
2024 /// Define exceptions generated by Time API.
2025 ///
2026 /// CTimeException inherits its basic functionality from CCoreException
2027 /// and defines additional error codes.
2028 
2030 {
2031 public:
2032  /// Error types that CTime can generate.
2033  enum EErrCode {
2034  eArgument, ///< Bad function argument.
2035  eConvert, ///< Error converting value from one format to another.
2036  eInvalid, ///< Invalid time value.
2037  eFormat ///< Incorrect format.
2038  };
2039 
2040  /// Translate from the error code value to its string representation.
2041  virtual const char* GetErrCodeString(void) const;
2042 
2043  // Standard exception boilerplate code.
2045 };
2046 
2047 
2048 /* @} */
2049 
2050 
2051 
2052 //=============================================================================
2053 //
2054 // Extern
2055 //
2056 //=============================================================================
2057 
2058 /// Quick and dirty getter of local time.
2059 /// Use global object of CFastLocalTime class to obtain time.
2060 /// See CFastLocalTime for details.
2062 extern CTime GetFastLocalTime(void);
2063 
2065 extern void TuneupFastLocalTime(void);
2066 
2067 
2068 //=============================================================================
2069 //
2070 // Inline
2071 //
2072 //=============================================================================
2073 
2074 // Add (subtract if negative) to the time (see CTime::AddXXX)
2075 
2076 inline
2077 CTime AddYear(const CTime& t, int years = 1)
2078 {
2079  CTime tmp(t);
2080  return tmp.AddYear(years);
2081 }
2082 
2083 inline
2084 CTime AddMonth(const CTime& t, int months = 1)
2085 {
2086  CTime tmp(t);
2087  return tmp.AddMonth(months);
2088 }
2089 
2090 inline
2091 CTime AddDay(const CTime& t, int days = 1)
2092 {
2093  CTime tmp(t);
2094  return tmp.AddDay(days);
2095 }
2096 
2097 inline
2098 CTime AddHour(const CTime& t, int hours = 1)
2099 {
2100  CTime tmp(t);
2101  return tmp.AddHour(hours);
2102 }
2103 
2104 inline
2105 CTime AddMinute(const CTime& t, int minutes = 1)
2106 {
2107  CTime tmp(t);
2108  return tmp.AddMinute(minutes);
2109 }
2110 
2111 inline
2112 CTime AddSecond(const CTime& t, long seconds = 1)
2113 {
2114  CTime tmp(t);
2115  return tmp.AddSecond(seconds);
2116 }
2117 
2118 inline
2119 CTime AddNanoSecond (const CTime& t, long nanoseconds = 1)
2120 {
2121  CTime tmp(t);
2122  return tmp.AddNanoSecond(nanoseconds);
2123 }
2124 
2125 // Add/subtract CTimeSpan (see CTime::operator +/-)
2126 inline
2127 CTime operator+ (const CTimeSpan& ts, const CTime& t)
2128 {
2129  CTime tmp(t);
2130  tmp.AddTimeSpan(ts);
2131  return tmp;
2132 }
2133 
2134 #ifdef CurrentTime // from <X11/X.h>, perhaps
2135 # undef CurrentTime
2136 #endif
2137 // Get current time (in local or universal format)
2138 inline
2142  )
2143 {
2144  return CTime(CTime::eCurrent, tz, tzp);
2145 }
2146 
2147 // Truncate the time to days (see CTime::Truncate)
2148 inline
2150 {
2151  CTime tmp(t);
2152  return tmp.Truncate();
2153 }
2154 
2155 /// Dumps the current stopwatch time to an output stream.
2156 /// The time will be printed out using format specified
2157 /// by CStopWatch::GetFormat().
2158 inline
2159 ostream& operator<< (ostream& os, const CStopWatch& sw)
2160 {
2161  return os << sw.AsString();
2162 }
2163 
2164 /// Dumps the current CTime time to an output stream.
2165 /// The time will be printed out using format
2166 /// returned by CTime::GetFormat().
2167 inline
2168 ostream& operator<< (ostream& os, const CTime& t)
2169 {
2170  return os << t.AsString();
2171 }
2172 
2173 
2174 //=============================================================================
2175 //
2176 // Inline class methods
2177 //
2178 //=============================================================================
2179 
2180 //
2181 // CTimeFormat
2182 //
2183 
2184 inline
2185 void CTimeFormat::SetFormat(const char* fmt, TFlags flags)
2186 {
2187  SetFormat(string(fmt), flags);
2188 }
2189 
2190 inline
2191 const string& CTimeFormat::GetString(void) const
2192 {
2193  return m_Str;
2194 }
2195 
2196 inline
2198 {
2199  return m_Flags;
2200 }
2201 
2202 inline
2203 bool CTimeFormat::IsEmpty(void) const
2204 {
2205  return m_Str.empty();
2206 }
2207 
2208 inline
2209 CTimeFormat::operator string(void) const
2210 {
2211  return m_Str;
2212 }
2213 
2214 
2215 //
2216 // CTime
2217 //
2218 
2219 inline
2220 int CTime::Year(void) const { return m_Data.year; }
2221 
2222 inline
2223 int CTime::Month(void) const { return m_Data.month; }
2224 
2225 inline
2226 int CTime::Day(void) const { return m_Data.day; }
2227 
2228 inline
2229 int CTime::Hour(void) const { return m_Data.hour; }
2230 
2231 inline
2232 int CTime::Minute(void) const { return m_Data.min; }
2233 
2234 inline
2235 int CTime::Second(void) const { return m_Data.sec; }
2236 
2237 inline
2238 long CTime::MilliSecond(void) const { return (long)m_Data.nanosec/1000000; }
2239 
2240 inline
2241 long CTime::MicroSecond(void) const { return (long)m_Data.nanosec/1000; }
2242 
2243 inline
2244 long CTime::NanoSecond(void) const { return (long)m_Data.nanosec; }
2245 
2246 inline
2248 {
2249  return AddMonth(years * 12, adl);
2250 }
2251 
2252 inline
2253 CTime& CTime::SetTimeT(const time_t t) { return x_SetTimeMTSafe(&t); }
2254 
2255 inline
2256 CTime& CTime::SetCurrent(void) { return x_SetTimeMTSafe(); }
2257 
2258 inline
2259 CTime& CTime::operator+= (const CTimeSpan& ts) { return AddTimeSpan(ts); }
2260 
2261 inline
2262 CTime& CTime::operator-= (const CTimeSpan& ts) { return AddTimeSpan(-ts); }
2263 
2264 inline
2266 {
2267  CTime tmp(*this);
2268  tmp.AddTimeSpan(ts);
2269  return tmp;
2270 }
2271 
2272 inline
2274 {
2275  CTime tmp(*this);
2276  tmp.AddTimeSpan(-ts);
2277  return tmp;
2278 }
2279 
2280 inline
2282 {
2283  return DiffTimeSpan(t);
2284 }
2285 
2286 inline
2287 CTime::operator string(void) const { return AsString(); }
2288 
2289 
2290 inline
2291 CTime& CTime::operator= (const string& str)
2292 {
2293  x_Init(str, GetFormat());
2294  return *this;
2295 }
2296 
2297 inline
2299 {
2300  if ( &t == this ) {
2301  return *this;
2302  }
2303  m_Data = t.m_Data;
2304  return *this;
2305 }
2306 
2307 inline
2308 bool CTime::operator!= (const CTime& t) const
2309 {
2310  return !(*this == t);
2311 }
2312 
2313 inline
2314 bool CTime::operator>= (const CTime& t) const
2315 {
2316  return !(*this < t);
2317 }
2318 
2319 inline
2320 bool CTime::operator<= (const CTime& t) const
2321 {
2322  return !(*this > t);
2323 }
2324 
2325 inline
2326 CTime& CTime::AddHour(int hours, EDaylight use_daylight)
2327 {
2328  return x_AddHour(hours, use_daylight, true);
2329 }
2330 
2331 inline
2332 bool CTime::IsEmpty() const
2333 {
2334  return
2335  !Day() && !Month() && !Year() &&
2336  !Hour() && !Minute() && !Second() && !NanoSecond();
2337 }
2338 
2339 inline
2341 {
2342  // We check year value only, because all time object date fields
2343  // can be zeros only at one time.
2344  return !Year();
2345 }
2346 
2347 inline
2348 double CTime::DiffDay(const CTime& t) const
2349 {
2350  return (double)DiffSecond(t) / 60.0 / 60.0 / 24.0;
2351 }
2352 
2353 inline
2354 double CTime::DiffHour(const CTime& t) const
2355 {
2356  return (double)DiffSecond(t) / 60.0 / 60.0;
2357 }
2358 
2359 inline
2360 double CTime::DiffMinute(const CTime& t) const
2361 {
2362  return (double)DiffSecond(t) / 60.0;
2363 }
2364 
2365 inline
2366 double CTime::DiffNanoSecond(const CTime& t) const
2367 {
2368  long dNanoSec = NanoSecond() - t.NanoSecond();
2369  return double(DiffSecond(t)) * double(kNanoSecondsPerSecond) + double(dNanoSec);
2370 }
2371 
2372 inline
2373 bool CTime::IsLocalTime(void) const { return m_Data.tz == eLocal; }
2374 
2375 inline
2376 bool CTime::IsUniversalTime(void) const { return m_Data.tz == eGmt; }
2377 
2378 inline
2380 {
2381  return m_Data.tz;
2382 }
2383 
2384 inline
2386 {
2387  return GetTimeZone();
2388 }
2389 
2390 inline
2392 {
2393  return m_Data.tzprec;
2394 }
2395 
2396 inline
2398 {
2399  ETimeZone tmp = m_Data.tz;
2400  m_Data.tz = val;
2401  return tmp;
2402 }
2403 
2404 inline
2406 {
2407  return SetTimeZone(val);
2408 }
2409 
2410 inline
2412 {
2413  ETimeZonePrecision tmp = m_Data.tzprec;
2414  m_Data.tzprec = val;
2415  return tmp;
2416 }
2417 
2418 inline
2420 {
2421  ToTime(eLocal);
2422  return *this;
2423 }
2424 
2425 inline
2427 {
2428  ToTime(eUTC);
2429  return *this;
2430 }
2431 
2432 // @deprecated
2433 inline
2434 TSeconds CTime::TimeZoneDiff(void) const
2435 {
2436  return TimeZoneOffset();
2437 }
2438 
2439 inline
2440 bool CTime::x_NeedAdjustTime(void) const
2441 {
2442  return GetTimeZone() == eLocal && GetTimeZonePrecision() != eNone;
2443 }
2444 
2445 
2446 //
2447 // CTimeSpan
2448 //
2449 
2450 inline
2452 {
2453  Clear();
2454  return;
2455 }
2456 
2457 inline
2458 CTimeSpan::CTimeSpan(long seconds, long nanoseconds)
2459 {
2460  Set(seconds, nanoseconds);
2461 }
2462 
2463 inline
2464 CTimeSpan::CTimeSpan(double seconds)
2465 {
2466  Set(seconds);
2467 }
2468 
2469 inline
2471 {
2472  m_Sec = t.m_Sec;
2473  m_NanoSec = t.m_NanoSec;
2474 }
2475 
2476 inline
2478  m_Sec = 0;
2479  m_NanoSec = 0;
2480  return *this;
2481 }
2482 
2483 inline
2485 {
2486  if ((m_Sec < 0) || (m_NanoSec < 0)) {
2487  return eNegative;
2488  }
2489  if (!m_Sec && !m_NanoSec) {
2490  return eZero;
2491  }
2492  return ePositive;
2493 }
2494 
2495 inline
2496 int CTimeSpan::x_Hour(void) const { return int((m_Sec / 3600L) % 24); }
2497 
2498 inline
2499 int CTimeSpan::x_Minute(void) const { return int((m_Sec / 60L) % 60); }
2500 
2501 inline
2502 int CTimeSpan::x_Second(void) const { return int(m_Sec % 60L); }
2503 
2504 inline
2505 long CTimeSpan::GetCompleteDays(void) const { return m_Sec / 86400L; }
2506 
2507 inline
2508 long CTimeSpan::GetCompleteHours(void) const { return m_Sec / 3600L; }
2509 
2510 inline
2511 long CTimeSpan::GetCompleteMinutes(void) const { return m_Sec / 60L; }
2512 
2513 inline
2514 long CTimeSpan::GetCompleteSeconds(void) const { return m_Sec; }
2515 
2516 inline
2517 long CTimeSpan::GetNanoSecondsAfterSecond(void) const { return m_NanoSec; }
2518 
2519 inline
2520 double CTimeSpan::GetAsDouble(void) const
2521 {
2522  return double(m_Sec) + double(m_NanoSec) / double(kNanoSecondsPerSecond);
2523 }
2524 
2525 inline
2526 void CTimeSpan::Set(long seconds, long nanoseconds)
2527 {
2528  m_Sec = seconds + nanoseconds/kNanoSecondsPerSecond;
2529  m_NanoSec = nanoseconds % kNanoSecondsPerSecond;
2530  x_Normalize();
2531 }
2532 
2533 inline
2534 bool CTimeSpan::IsEmpty(void) const
2535 {
2536  return m_Sec == 0 && m_NanoSec == 0;
2537 }
2538 
2539 inline
2541 {
2542  m_Sec = t.m_Sec;
2543  m_NanoSec = t.m_NanoSec;
2544  return *this;
2545 }
2546 
2547 inline
2548 CTimeSpan::operator string(void) const { return AsString(); }
2549 
2550 inline
2552 {
2553  m_Sec += t.m_Sec;
2554  m_NanoSec += t.m_NanoSec;
2555  x_Normalize();
2556  return *this;
2557 }
2558 
2559 inline
2561 {
2562  CTimeSpan tnew(0, 0, 0, m_Sec + t.m_Sec, m_NanoSec + t.m_NanoSec);
2563  return tnew;
2564 }
2565 
2566 inline
2568 {
2569  m_Sec -= t.m_Sec;
2570  m_NanoSec -= t.m_NanoSec;
2571  x_Normalize();
2572  return *this;
2573 }
2574 
2575 inline
2577 {
2578  CTimeSpan tnew(0, 0, 0, m_Sec - t.m_Sec, m_NanoSec - t.m_NanoSec);
2579  return tnew;
2580 }
2581 
2582 inline
2584 {
2585  CTimeSpan t;
2586  t.m_Sec = -m_Sec;
2587  t.m_NanoSec = -m_NanoSec;
2588  return t;
2589 }
2590 
2591 inline
2593 {
2594  m_Sec = -m_Sec;
2595  m_NanoSec = -m_NanoSec;
2596 }
2597 
2598 inline
2599 bool CTimeSpan::operator== (const CTimeSpan& t) const
2600 {
2601  return m_Sec == t.m_Sec && m_NanoSec == t.m_NanoSec;
2602 }
2603 
2604 inline
2605 bool CTimeSpan::operator!= (const CTimeSpan& t) const
2606 {
2607  return !(*this == t);
2608 }
2609 
2610 inline
2611 bool CTimeSpan::operator> (const CTimeSpan& t) const
2612 {
2613  if (m_Sec == t.m_Sec) {
2614  return m_NanoSec > t.m_NanoSec;
2615  }
2616  return m_Sec > t.m_Sec;
2617 }
2618 
2619 
2620 inline
2621 bool CTimeSpan::operator< (const CTimeSpan& t) const
2622 {
2623  if (m_Sec == t.m_Sec) {
2624  return m_NanoSec < t.m_NanoSec;
2625  }
2626  return m_Sec < t.m_Sec;
2627 }
2628 
2629 inline
2630 bool CTimeSpan::operator>= (const CTimeSpan& t) const
2631 {
2632  return !(*this < t);
2633 }
2634 
2635 inline
2636 bool CTimeSpan::operator<= (const CTimeSpan& t) const
2637 {
2638  return !(*this > t);
2639 }
2640 
2641 // @deprecated
2642 inline
2645  ERound rounding,
2646  ESmartStringZeroMode zero_mode) const
2647 {
2649  flags |= (rounding == eRound) ?
2651  flags |= (zero_mode == CTimeSpan::eSSZ_NoSkipZero) ?
2653  return AsSmartString(flags);
2654 }
2655 
2656 
2657 
2658 //
2659 // CTimeout
2660 //
2661 
2662 inline
2664 
2665 inline
2667 
2668 inline
2669 CTimeout::CTimeout(const CTimeSpan& ts) { Set(ts); }
2670 
2671 inline
2672 CTimeout::CTimeout(unsigned int sec, unsigned int usec) { Set(sec, usec); }
2673 
2674 inline
2675 CTimeout::CTimeout(double sec) { Set(sec); }
2676 
2677 inline
2679 {
2680  return m_Type == eDefault;
2681 }
2682 
2683 inline
2685 {
2686  return m_Type == eInfinite;
2687 }
2688 
2689 inline
2691 {
2692  return m_Type == eFinite;
2693 }
2694 
2695 inline
2696 bool CTimeout::operator!= (const CTimeout& t) const
2697 {
2698  return !(*this == t);
2699 }
2700 
2701 
2702 
2703 //
2704 // CStopWatch
2705 //
2706 
2707 inline
2709 {
2710  m_Total = 0;
2711  m_Start = 0;
2712  m_State = eStop;
2713  if ( state == eStart ) {
2714  Start();
2715  }
2716 }
2717 
2718 inline
2720 {
2721  if ( m_State == eStart ) {
2722  return;
2723  }
2724  m_Start = GetTimeMark();
2725  m_State = eStart;
2726 }
2727 
2728 
2729 inline
2730 double CStopWatch::Elapsed(void) const
2731 {
2732  double total = m_Total;
2733  if ( m_State == eStop ) {
2734  return total;
2735  }
2736  // Workaround for -0 (negative zero) values,
2737  // that can occur at subtraction of very close doubles.
2738  double mark = GetTimeMark() - m_Start;
2739  if (mark > 0.0) {
2740  total += mark;
2741  }
2742  return total;
2743 }
2744 
2745 
2746 inline
2748 {
2749  if ( m_State == eStop ) {
2750  return;
2751  }
2752  m_State = eStop;
2753 
2754  double mark = GetTimeMark() - m_Start;
2755  if (mark > 0.0) {
2756  m_Total += mark;
2757  }
2758 }
2759 
2760 
2761 inline
2763 {
2764  m_State = eStop;
2765  m_Total = 0;
2766  m_Start = 0;
2767 }
2768 
2769 
2770 inline
2772 {
2773  double total = m_Total;
2774  double current = GetTimeMark();
2775  if ( m_State == eStart ) {
2776  // Workaround for -0 (negative zero) values,
2777  // that can occur at subtraction of very close doubles.
2778  double mark = current - m_Start;
2779  if ( mark > 0.0 ) {
2780  total += mark;
2781  }
2782  }
2783  m_Total = 0;
2784  m_Start = current;
2785  m_State = eStart;
2786  return total;
2787 }
2788 
2789 inline
2790 bool CStopWatch::IsRunning(void) const
2791 {
2792  return m_State == eStart;
2793 }
2794 
2795 
2796 inline
2797 CStopWatch::operator string(void) const
2798 {
2799  return AsString();
2800 }
2801 
2802 
2803 // @deprecated
2804 inline
2807  ERound rounding,
2809  const
2810 {
2812  flags |= (rounding == eRound) ?
2814  flags |= (zero_mode == CTimeSpan::eSSZ_NoSkipZero) ?
2816  return CTimeSpan(Elapsed()).AsSmartString(flags);
2817 }
2818 
2819 
2820 inline
2822 {
2823  return CTimeSpan(Elapsed()).AsSmartString(flags);
2824 }
2825 
2826 
2828 
2829 #endif /* CORELIB__NCBITIME__HPP */
static char tmp[2048]
Definition: utf8.c:62
Print zero valued parts.
Definition: ncbitime.hpp:1491
string AsString(const CTimeFormat &fmt=kEmptyStr) const
Transform stopwatch time to string.
Definition: ncbitime.cpp:4111
Int4 days
days from 1/1/1900
Definition: ncbitime.hpp:112
CTime CurrentTime(CTime::ETimeZone tz=CTime::eLocal, CTime::ETimeZonePrecision tzp=CTime::eTZPrecisionDefault)
Definition: ncbitime.hpp:2139
CNanoTimeout – Timeout interval, using nanoseconds.
Definition: ncbitime.hpp:1773
bool IsInfinite(void) const
Check if the deadline is infinite.
Definition: ncbitime.hpp:1808
CStopWatch –.
Definition: ncbitime.hpp:1891
ERound
Whether to truncate/round a value.
Definition: ncbimisc.hpp:128
Check condition - new month.
Definition: ncbitime.hpp:357
CExpression operator>=(CREATED, time_point)
const long kMicroSecondsPerSecond
Number of microseconds in one second.
Definition: ncbitime.hpp:88
void Start(void)
Start the timer.
Definition: ncbitime.hpp:2719
TFlags m_Flags
Flags, hints, attributes.
Definition: ncbiexpt.hpp:964
unsigned int m_SecAfterHour
Time interval in seconds after hour.
Definition: ncbitime.hpp:1872
int m_Timezone
Cached timezone adjustment for local time.
Definition: ncbitime.hpp:1879
bool IsEmpty(void) const
Is time object empty (date and time)?
Definition: ncbitime.hpp:2332
time_t m_Seconds
Definition: ncbitime.hpp:1828
static double GetTimeMark(void)
Definition: net.c:288
struct TDBTimeI * TDBTimeIPtr
double DiffNanoSecond(const CTime &t) const
Difference in nanoseconds from specified time.
Definition: ncbitime.hpp:2366
ENameFormat
Which format use to get name of month or week of day.
Definition: ncbitime.hpp:312
signed int Int4
Alias for signed int.
Definition: ncbitype.h:120
CTimeException –.
Definition: ncbitime.hpp:2029
Start timer immediately after creating.
Definition: ncbitime.hpp:1896
const TSeconds kAverageSecondsPerMonth
Definition: ncbitime.hpp:99
time_t m_LastSysTime
Last system time.
Definition: ncbitime.hpp:1878
Value is zero.
Definition: ncbimisc.hpp:122
double m_Start
Start time value.
Definition: ncbitime.hpp:2013
Value must be rounded.
Definition: ncbimisc.hpp:130
long GetCompleteHours(void) const
Get number of complete hours.
Definition: ncbitime.hpp:2508
Database format for time where day and time is signed 32 bit.
Definition: ncbitime.hpp:111
void Invert(void)
Invert time span. Changes time span sign.
Definition: ncbitime.hpp:2592
bool IsExpired(void) const
Check if the deadline is expired.
Definition: ncbitime.hpp:1811
ETimeZonePrecision SetTimeZonePrecision(ETimeZonePrecision val)
Set time zone precision.
Definition: ncbitime.hpp:2411
ETimeZonePrecision GetTimeZonePrecision(void) const
Get time zone precision.
Definition: ncbitime.hpp:2391
double Restart(void)
Return time elapsed since first Start() or last Restart() call (in seconds).
Definition: ncbitime.hpp:2771
ESign
Signedness of a value.
Definition: ncbimisc.hpp:120
int x_Second(void) const
Get second.
Definition: ncbitime.hpp:2502
ETimeZone SetTimeZone(ETimeZone val)
Set time zone.
Definition: ncbitime.hpp:2397
CTime & AddHour(int hours=1, EDaylight adl=eDaylightDefault)
Add specified hours and adjust for daylight saving time.
Definition: ncbitime.hpp:2326
TFlags m_Flags
Format flags.
Definition: ncbitime.hpp:271
#define NCBI_EXCEPTION_DEFAULT(exception_class, base_class)
To help declare new exception class.
Definition: ncbiexpt.hpp:1122
CTime & operator=(const CTime &t)
Assignment operator.
Definition: ncbitime.hpp:2298
bool IsUniversalTime(void) const
Is time universal (GMT/UTC/Z)?
Definition: ncbitime.hpp:2376
bool operator>(const typename tree< T, tree_node_allocator >::iterator_base &one, const typename tree< T, tree_node_allocator >::iterator_base &two)
Definition: tree_msvc7.hpp:432
Error converting value from one format to another.
Definition: ncbitime.hpp:2035
CNcbiMatrix< T > & operator-=(CNcbiMatrix< T > &, const CNcbiMatrix< U > &)
global subtraction: matrix -= matrix
Definition: matrix.hpp:622
const double kAverageDaysPerYear
The average length of the year in the Gregorian (modern) calendar (in days)
Definition: ncbitime.hpp:96
CVect2< T > operator-(const CVect2< T > &v)
Definition: globals.hpp:207
CTime & operator+=(const CTimeSpan &ts)
Definition: ncbitime.hpp:2259
Check condition - new day.
Definition: ncbitime.hpp:356
Uint2 time
Minutes from the beginning of current day.
Definition: ncbitime.hpp:107
int Second(void) const
Get second.
Definition: ncbitime.hpp:2235
bool operator>=(const CTime &t) const
Operator to test if time is later or equal.
Definition: ncbitime.hpp:2314
EPredefined
Predefined formats.
Definition: ncbitime.hpp:187
Throw an exception on error.
Definition: ncbitime.hpp:1189
CTime & AddSecond(TSeconds seconds=1, EDaylight adl=eDaylightDefault)
Add specified seconds.
Definition: ncbitime.cpp:1862
CTimeFormat –.
Definition: ncbitime.hpp:127
Round to days.
Definition: ncbitime.hpp:978
unsigned int TFlags
Binary OR of "EFlags".
Definition: ncbitime.hpp:182
const long kNanoSecondsPerSecond
Number of nanoseconds in one second.
Definition: ncbitime.hpp:83
CTime & AddNanoSecond(long nanoseconds=1)
Add specified nanoseconds.
Definition: ncbitime.cpp:1878
CTime AddHour(const CTime &t, int hours=1)
Definition: ncbitime.hpp:2098
string
Definition: cgiapp.hpp:498
long GetNanoSecondsAfterSecond(void) const
Get number of nanoseconds.
Definition: ncbitime.hpp:2517
bool IsInfinite() const
Definition: ncbitime.hpp:2684
CTimeSpan operator+(const CTimeSpan &t) const
Definition: ncbitime.hpp:2560
ETimeZone GetTimeZoneFormat(void) const
Definition: ncbitime.hpp:2385
CNcbiMatrix< T > & operator+=(CNcbiMatrix< T > &, const CNcbiMatrix< U > &)
global addition: matrix += matrix
Definition: matrix.hpp:570
ETimeZonePrecision
What time zone precision to use for adjusting daylight saving time.
Definition: ncbitime.hpp:352
string AsSmartString(CTimeSpan::ESmartStringPrecision precision, ERound rounding, CTimeSpan::ESmartStringZeroMode zero_mode=CTimeSpan::eSSZ_SkipZero) const
Transform elapsed time to "smart" string.
Definition: ncbitime.hpp:2805
bool IsRunning(void) const
Check state of stopwatch.
Definition: ncbitime.hpp:2790
CTime AddYear(const CTime &t, int years=1)
Definition: ncbitime.hpp:2077
signed NCBI_INT8_TYPE Int8
Signed 8 byte sized integer.
Definition: ncbitype.h:143
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1266
void *volatile m_IsTuneup
(bool) Tuneup() in progress (MT)
Definition: ncbitime.hpp:1881
EFlags
Flags.
Definition: ncbitime.hpp:136
long m_Sec
Seconds part of the time span.
Definition: ncbitime.hpp:1643
int Month(void) const
Get month.
Definition: ncbitime.hpp:2223
#define NCBI_TIME_BITFIELD(n)
Definition: ncbitime.hpp:1238
#define kEmptyStr
Definition: ncbistr.hpp:120
void Stop(void)
Suspend the timer.
Definition: ncbitime.hpp:2747
bool operator<=(const CTimeSpan &t) const
Operator to test if time span is less or equal.
Definition: ncbitime.hpp:2636
CTime & SetCurrent(void)
Make the time current in the presently active time zone.
Definition: ncbitime.hpp:2256
void TuneupFastLocalTime(void)
Definition: ncbitime.cpp:4135
CCoreException –.
Definition: ncbiexpt.hpp:1300
CTime & AddYear(int years=1, EDaylight adl=eDaylightDefault)
Add specified years and adjust for daylight saving time.
Definition: ncbitime.hpp:2247
double DiffMinute(const CTime &t) const
Difference in minutes from specified time.
Definition: ncbitime.hpp:2360
int x_Minute(void) const
Get minute.
Definition: ncbitime.hpp:2499
Ignore daylight saving time.
Definition: ncbitime.hpp:363
Round to minutes.
Definition: ncbitime.hpp:980
static char precision
Definition: genparams.c:31
int Hour(void) const
Get hour.
Definition: ncbitime.hpp:2229
EStart
Defines how to create new timer.
Definition: ncbitime.hpp:1895
Database format for time where day and time is unsigned 16 bit.
Definition: ncbitime.hpp:105
CTime m_LocalTime
Current local time.
Definition: ncbitime.hpp:1874
ESmartStringZeroMode
Which format use to output zero time span parts.
Definition: ncbitime.hpp:1490
double DiffHour(const CTime &t) const
Difference in hours from specified time.
Definition: ncbitime.hpp:2354
Daylight saving not to affect time manipulations.
Definition: ncbitime.hpp:353
CTime & ToLocalTime(void)
Convert the time into local time.
Definition: ncbitime.hpp:2419
static int flags
Definition: mdb_load.c:31
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
CTime & SetTimeT(const time_t t)
Set time using time_t time value.
Definition: ncbitime.hpp:2253
EDaylight
Whether to adjust for daylight saving time.
Definition: ncbitime.hpp:362
Round to milliseconds.
Definition: ncbitime.hpp:982
CTime operator+(const CTimeSpan &ts) const
Definition: ncbitime.hpp:2265
TFlags GetFlags(void) const
Get format flags.
Definition: ncbitime.hpp:2197
CTime m_TunedTime
Last tuned time (changed by Tuneup())
Definition: ncbitime.hpp:1875
CTimeSpan.
Definition: ncbitime.hpp:1276
long GetCompleteMinutes(void) const
Get number of complete minutes.
Definition: ncbitime.hpp:2511
CTimeSpan & Clear(void)
Make the time span "empty",.
Definition: ncbitime.hpp:2477
bool operator==(const CTimeSpan &t) const
Operator to test equality of time span.
Definition: ncbitime.hpp:2599
CTime & ToUniversalTime(void)
Convert the time into universal (GMT/UTC) time.
Definition: ncbitime.hpp:2426
bool IsDefault() const
Definition: ncbitime.hpp:2678
int Year(void) const
Get year.
Definition: ncbitime.hpp:2220
CTime & Truncate(ERoundPrecision precision=eRound_Day)
Truncate time.
Definition: ncbitime.cpp:1952
mdb_mode_t mode
Definition: lmdb++.h:46
ESmartStringPrecision
Precision for span "smart" string.
Definition: ncbitime.hpp:1467
string m_Str
String format.
Definition: ncbitime.hpp:270
CTime & AddDay(int days=1, EDaylight adl=eDaylightDefault)
Add specified days and adjust for daylight saving time.
Definition: ncbitime.cpp:1770
ETimeZone SetTimeZoneFormat(ETimeZone val)
Definition: ncbitime.hpp:2405
Bad function argument.
Definition: ncbitime.hpp:2034
TData m_Data
Packed members.
Definition: ncbitime.hpp:1258
#define NCBI_DEPRECATED_CTOR(decl)
Macro used to mark a constructor as deprecated.
Definition: ncbimisc.hpp:1099
CTime operator-(const CTimeSpan &ts) const
Operator to subtract time span.
Definition: ncbitime.hpp:2273
CTime AddSecond(const CTime &t, long seconds=1)
Definition: ncbitime.hpp:2112
CNanoTimeout(unsigned int seconds, unsigned int nanoseconds)
Definition: ncbitime.hpp:1776
Check condition - new minute.
Definition: ncbitime.hpp:354
long MicroSecond(void) const
Get microseconds.
Definition: ncbitime.hpp:2241
bool IsLocalTime(void) const
Is time local time?
Definition: ncbitime.hpp:2373
bool x_NeedAdjustTime(void) const
Helper method to check if there is a need adjust time in timezone.
Definition: ncbitime.hpp:2440
const string & GetString(void) const
Get format string.
Definition: ncbitime.hpp:2191
Definition: type.c:8
double DiffDay(const CTime &t) const
Difference in days from specified time.
Definition: ncbitime.hpp:2348
bool IsEmptyDate(void) const
Is date empty?
Definition: ncbitime.hpp:2340
CTimeSpan operator-(void) const
Unary operator "-" (minus) to change time span sign.
Definition: ncbitime.hpp:2583
static int Round(double Num)
Definition: su_pssm.cpp:88
Local time.
Definition: ncbitime.hpp:301
ostream & operator<<(ostream &os, const CStopWatch &sw)
Dumps the current stopwatch time to an output stream.
Definition: ncbitime.hpp:2159
Value is negative.
Definition: ncbimisc.hpp:121
virtual const char * GetErrCodeString(void) const
Translate from the error code value to its string representation.
Definition: ncbiexpt.cpp:746
A finite timeout value has been set.
Definition: ncbitime.hpp:1661
ETimeZone GetTimeZone(void) const
Get time zone.
Definition: ncbitime.hpp:2379
bool IsGmtTime(void) const
Definition: ncbitime.hpp:1103
int Day(void) const
Get day.
Definition: ncbitime.hpp:2226
CTimeSpan & operator-=(const CTimeSpan &t)
Operator to subtract time span.
Definition: ncbitime.hpp:2567
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1515
unsigned int m_Nanoseconds
Definition: ncbitime.hpp:1829
void SetFormat(const char *fmt, TFlags flags=fDefault)
Set the current time format.
Definition: ncbitime.hpp:2185
bool IsValid(const CSeq_point &pt, CScope *scope)
Checks that point >= 0 and point < length of Bioseq.
bool operator!=(const CTime &t) const
Operator to test in-equality of time.
Definition: ncbitime.hpp:2308
bool operator<=(const CTime &t) const
Operator to test if time is earlier or equal.
Definition: ncbitime.hpp:2320
int Minute(void) const
Get minute.
Definition: ncbitime.hpp:2232
CTime AddNanoSecond(const CTime &t, long nanoseconds=1)
Definition: ncbitime.hpp:2119
CTime & AddTimeSpan(const CTimeSpan &timespan)
Add specified time span.
Definition: ncbitime.cpp:1894
void SetNano(unsigned int sec, unsigned int nanosec)
Set timeout in seconds and nanoseconds.
Definition: ncbitime.cpp:3587
bool m_Infinite
Definition: ncbitime.hpp:1830
CFastLocalTime –.
Definition: ncbitime.hpp:1849
CTime –.
Definition: ncbitime.hpp:290
bool operator!=(const _Ht_iterator< _Val, _Nonconst_traits< _Val >, _Key, _HF, _ExK, _EqK, _All > &__x, const _Ht_iterator< _Val, _Const_traits< _Val >, _Key, _HF, _ExK, _EqK, _All > &__y)
Definition: _hashtable.h:173
CTimeSpan & operator+=(const CTimeSpan &t)
Definition: ncbitime.hpp:2551
bool operator<(const CEquivRange &A, const CEquivRange &B)
Definition: equiv_range.cpp:90
ESmartStringFlags
AsSmartString() conversion flags.
Definition: ncbitime.hpp:1404
bool IsFinite() const
Check if timeout holds a numeric value.
Definition: ncbitime.hpp:2690
CTime & AddMonth(int months=1, EDaylight adl=eDaylightDefault)
Add specified months and adjust for daylight saving time.
Definition: ncbitime.cpp:1739
T min(T x_, T y_)
CDeadline CAbsTimeout
Definition: ncbitime.hpp:1835
CStopWatch(EStart state=eStop)
Constructor.
Definition: ncbitime.hpp:2708
Default timeout (to be interpreted by the client code)
Definition: ncbitime.hpp:1662
CTimeSpan & operator=(const CTimeSpan &t)
Assignment operator.
Definition: ncbitime.hpp:2540
EType
Type of timeouts.
Definition: ncbitime.hpp:1660
bool operator<(const CTimeSpan &t) const
Operator to test if time span is less.
Definition: ncbitime.hpp:2621
Int4 time
x/300 seconds from the beginning of current day
Definition: ncbitime.hpp:113
ETimeZone
Which initial value to use for timezone.
Definition: ncbitime.hpp:300
CTime & ToGmtTime(void)
Definition: ncbitime.hpp:1161
long MilliSecond(void) const
Get milliseconds.
Definition: ncbitime.hpp:2238
no filtering at all.
struct TDBTimeU * TDBTimeUPtr
bool IsEmpty(void) const
Return TRUE is an object keep zero time span.
Definition: ncbitime.hpp:2534
bool operator>(const CTimeSpan &t) const
Operator to test if time span is greater.
Definition: ncbitime.hpp:2611
static CStopWatch sw
EMonth
Month names.
Definition: ncbitime.hpp:318
Skip zero valued parts.
Definition: ncbitime.hpp:1492
long m_NanoSec
Nanoseconds after the second.
Definition: ncbitime.hpp:1644
bool operator!=(const CTimeout &t) const
Operator to test in-equality of timeouts.
Definition: ncbitime.hpp:2696
None specified.
Definition: blast_def.h:326
UTC (Universal Coordinated Time)
Definition: ncbitime.hpp:302
CTime & AddMinute(int minutes=1, EDaylight adl=eDaylightDefault)
Add specified minutes and adjust for daylight saving time.
Definition: ncbitime.cpp:1832
Invalid time value.
Definition: ncbitime.hpp:2036
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1153
const TYPE & Get(const CNamedParameterList *param)
double Elapsed(void) const
Return time elapsed since first Start() or last Restart() call (in seconds).
Definition: ncbitime.hpp:2730
CExpression operator<=(time_point, CREATED)
CTime GetGmtTime(void) const
Definition: ncbitime.hpp:1151
CTime operator+(const CTimeSpan &ts, const CTime &t)
Definition: ncbitime.hpp:2127
#define NCBI_TIME_EMPTY_BITFIELD
Definition: ncbitime.hpp:1239
int m_Daylight
Cached system daylight information.
Definition: ncbitime.hpp:1880
void Reset(void)
Stop (if running) and reset the timer.
Definition: ncbitime.hpp:2762
bool operator!=(const CTimeSpan &t) const
Operator to test in-equality of time span.
Definition: ncbitime.hpp:2605
EErrAction
Defines how to behave on error.
Definition: ncbitime.hpp:1188
double GetAsDouble(void) const
Return time span as number of seconds.
Definition: ncbitime.hpp:2520
CTime AddMinute(const CTime &t, int minutes=1)
Definition: ncbitime.hpp:2105
long GetCompleteDays(void) const
Get number of complete days.
Definition: ncbitime.hpp:2505
CTime & operator-=(const CTimeSpan &ts)
Operator to subtract time span.
Definition: ncbitime.hpp:2262
unsigned int m_NanoSec
Nanoseconds part of the timeout.
Definition: ncbitime.hpp:1760
Adjust for daylight saving time.
Definition: ncbitime.hpp:364
~CTimeout(void)
Destructor.
Definition: ncbitime.hpp:1686
Use current time.
Definition: ncbitime.hpp:295
double m_Total
Accumulated elapsed time.
Definition: ncbitime.hpp:2014
virtual void x_Init(const CDiagCompileInfo &info, const string &message, const CException *prev_exception, EDiagSev severity)
Helper method for initializing exception data.
Definition: ncbiexpt.cpp:502
unsigned short Uint2
Alias for unsigned short.
Definition: ncbitype.h:119
ESerialDataFormat GetFormat(const string &name)
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:720
long GetCompleteSeconds(void) const
Get number of complete seconds.
Definition: ncbitime.hpp:2514
CTimeout – Timeout interval.
Definition: ncbitime.hpp:1656
Check condition - new hour.
Definition: ncbitime.hpp:355
#define NCBI_DEPRECATED
Definition: ncbiconf_msvc.h:71
Value is positive.
Definition: ncbimisc.hpp:123
bool operator==(const CEquivRange &A, const CEquivRange &B)
Definition: equiv_range.cpp:86
void Set(long seconds, long nanoseconds=0)
Set time span in seconds and nanoseconds.
Definition: ncbitime.hpp:2526
#define const
Definition: zconf.h:217
CTimeSpan(void)
Default constructor.
Definition: ncbitime.hpp:2451
bool operator>=(const CTimeSpan &t) const
Operator to test if time span is greater or equal.
Definition: ncbitime.hpp:2630
CTimeout(void)
Create default timeout.
Definition: ncbitime.hpp:2663
EDayOfWeek
Day of week names.
Definition: ncbitime.hpp:334
CTime AddDay(const CTime &t, int days=1)
Definition: ncbitime.hpp:2091
CTime Truncate(const CTime &t)
Definition: ncbitime.hpp:2149
ERoundPrecision
Precision for rounding time.
Definition: ncbitime.hpp:977
Use full name.
Definition: ncbitime.hpp:313
string AsSmartString(ESmartStringPrecision precision, ERound rounding, ESmartStringZeroMode zero_mode=eSSZ_SkipZero) const
Transform time span to "smart" string.
Definition: ncbitime.hpp:2643
CTime GetFastLocalTime(void)
Quick and dirty getter of local time.
Definition: ncbitime.cpp:4129
const long kMilliSecondsPerSecond
Number milliseconds in one second.
Definition: ncbitime.hpp:93
time_t m_LastTuneupTime
Last Tuneup() time.
Definition: ncbitime.hpp:1877
unsigned int TSmartStringFlags
Binary OR of "ESmartStringFlags".
Definition: ncbitime.hpp:1463
Infinite timeout.
Definition: ncbitime.hpp:1663
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
long NanoSecond(void) const
Get nano-seconds.
Definition: ncbitime.hpp:2244
ESign GetSign(void) const
Get sign of time span.
Definition: ncbitime.hpp:2484
Int8 TSeconds
Number of seconds.
Definition: ncbitime.hpp:74
int x_Hour(void) const
Get hour.
Definition: ncbitime.hpp:2496
TSeconds TimeZoneDiff(void) const
Get difference between local timezone for current time object and UTC in seconds. ...
Definition: ncbitime.hpp:2434
Round to seconds.
Definition: ncbitime.hpp:981
bool IsEmpty(void) const
Check that format string is empty.
Definition: ncbitime.hpp:2203
Round to hours.
Definition: ncbitime.hpp:979
CTime AddMonth(const CTime &t, int months=1)
Definition: ncbitime.hpp:2084
Uint2 days
Days from 1/1/1900.
Definition: ncbitime.hpp:106
EInitMode
Which initial value to use for time.
Definition: ncbitime.hpp:294
CDeadline.
Definition: ncbitime.hpp:1793
EStart m_State
Stopwatch state (started/stopped)
Definition: ncbitime.hpp:2015
unsigned int m_Sec
Seconds part of the timeout.
Definition: ncbitime.hpp:1759
const TSeconds kAverageSecondsPerYear
The average length of the year in the Gregorian (modern) calendar (in seconds)
Definition: ncbitime.hpp:98
EType m_Type
Type of timeout.
Definition: ncbitime.hpp:1758
Modified on Sat Sep 23 14:32:13 2017 by modify_doxy.py rev. 546573