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

Go to the SVN repository for this file.

1 #ifndef NETCACHE__SRV_TIME__HPP
2 #define NETCACHE__SRV_TIME__HPP
3 /* $Id: srv_time.hpp 67829 2015-06-17 13:26:20Z gouriano $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors: Pavel Ivanov
29  *
30  * File Description:
31  */
32 
33 
34 #ifdef NCBI_OS_LINUX
35 # include <sys/time.h>
36 #endif
37 
38 
40 
41 
42 enum {
44  kUSecsPerMSec = 1000,
45  kNSecsPerUSec = 1000,
49 };
50 
51 
52 /// Class incorporating convenient methods to work with struct timespec.
53 /// Besides that the class allows to hide all dependency on Linux inside
54 /// making outside code cleaner. Object of this class can store absolute times
55 /// or time intervals.
56 #ifdef NCBI_OS_LINUX
57 class CSrvTime : public timespec
58 #else
59 class CSrvTime
60 #endif
61 {
62 public:
63  /// Current time in seconds since epoch (time_t). This is not absolutely
64  /// exact current time, it's the value of variable which should be updated
65  /// once in a jiffy (0.001 of a second by default) although with overloaded
66  /// server it can update less often.
67  static int CurSecs(void);
68  /// Exact current time with precision up to nanoseconds.
69  static CSrvTime Current(void);
70  /// Timezone adjustment (in seconds) for times stored in CSrvTime.
71  /// If you add result of this method to number of seconds from CSrvTime
72  /// (or from CurSecs() function) and pass the result to gmtime_r you'll
73  /// get correct time components in the current timezone.
74  static int TZAdjustment(void);
75 
76  CSrvTime(void);
77  CSrvTime(Uint8 sec);
78  /// Read/set number of seconds since epoch stored in the object.
79  time_t& Sec(void);
80  time_t Sec(void) const;
81  /// Read/set number of nanoseconds stored in the object
82  long& NSec(void);
83  long NSec(void) const;
84  /// Converts object's value to microseconds since epoch.
85  Uint8 AsUSec(void) const;
86  /// Compares object's value to another CSrvTime object's value. Returns 0
87  /// if values are equal, -1 if this object's value is earlier than
88  /// provided, 1 if this object's value is later than provided.
89  int Compare(const CSrvTime& t) const;
90 
91  /// Add value of other CSrvTime object to this one. Result is stored in
92  /// this object.
93  CSrvTime& operator+= (const CSrvTime& t);
94  /// Subtract value of other CSrvTime object from this one. Result is stored
95  /// in this object.
96  CSrvTime& operator-= (const CSrvTime& t);
97  /// Compare object's value to another CSrvTime object's value.
98  bool operator> (const CSrvTime& t) const;
99  bool operator>= (const CSrvTime& t) const;
100  bool operator< (const CSrvTime& t) const;
101  bool operator<= (const CSrvTime& t) const;
102 
103  /// Type of time formatting in the Print() method.
104  enum EFormatType {
105  /// Format used in logs which is YYYY-MM-DDThh:mm:ss.ssssss
107  /// Format that can be readable by humans with precision up to seconds.
108  /// This format looks like MM/DD/YYYY hh:mm:ss.
110  /// Format that can be readable by humans with precision up to
111  /// microseconds. This format looks like MM/DD/YYYY hh:mm:ss.ssssss.
113  /// For JSON output
114  eFmtJson
115  };
116  /// Formats time value in the object and writes it in buf. Buffer should
117  /// have sufficient size for the whole time (see above formats description
118  /// for size necessary for each format).
119  Uint1 Print(char* buf, EFormatType fmt) const;
120 };
121 
122 
124 
125 #endif /* NETCACHE__SRV_TIME__HPP */
Class incorporating convenient methods to work with struct timespec.
Definition: srv_time.hpp:61
bool operator>(const CSrvTime &t) const
Compare object's value to another CSrvTime object's value.
int Compare(const CSrvTime &t) const
Compares object's value to another CSrvTime object's value.
CSrvTime(void)
bool operator<(const CSrvTime &t) const
static int CurSecs(void)
Current time in seconds since epoch (time_t).
static int TZAdjustment(void)
Timezone adjustment (in seconds) for times stored in CSrvTime.
Definition: time_man.cpp:222
bool operator>=(const CSrvTime &t) const
CSrvTime & operator-=(const CSrvTime &t)
Subtract value of other CSrvTime object from this one.
time_t & Sec(void)
Read/set number of seconds since epoch stored in the object.
CSrvTime & operator+=(const CSrvTime &t)
Add value of other CSrvTime object to this one.
bool operator<=(const CSrvTime &t) const
static CSrvTime Current(void)
Exact current time with precision up to nanoseconds.
Uint8 AsUSec(void) const
Converts object's value to microseconds since epoch.
Uint1 Print(char *buf, EFormatType fmt) const
Formats time value in the object and writes it in buf.
Definition: time_man.cpp:155
long & NSec(void)
Read/set number of nanoseconds stored in the object.
EFormatType
Type of time formatting in the Print() method.
Definition: srv_time.hpp:104
@ eFmtHumanUSecs
Format that can be readable by humans with precision up to microseconds.
Definition: srv_time.hpp:112
@ eFmtHumanSeconds
Format that can be readable by humans with precision up to seconds.
Definition: srv_time.hpp:109
@ eFmtJson
For JSON output.
Definition: srv_time.hpp:114
@ eFmtLogging
Format used in logs which is YYYY-MM-DDThh:mm:ss.ssssss.
Definition: srv_time.hpp:106
uint8_t Uint1
1-byte (8-bit) unsigned integer
Definition: ncbitype.h:99
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
char * buf
EIPRangeType t
Definition: ncbi_localip.c:101
@ kUSecsPerSecond
Definition: srv_time.hpp:46
@ kNSecsPerMSec
Definition: srv_time.hpp:47
@ kNSecsPerSecond
Definition: srv_time.hpp:48
@ kNSecsPerUSec
Definition: srv_time.hpp:45
@ kUSecsPerMSec
Definition: srv_time.hpp:44
@ kMSecsPerSecond
Definition: srv_time.hpp:43
Modified on Fri Jan 05 07:21:51 2024 by modify_doxy.py rev. 669887