The Gaudi Framework  v40r0 (475e45c1)
Time.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include <GaudiKernel/Time.h>
12 #include <GaudiKernel/time_r.h>
13 #include <cerrno>
14 #include <cstdio>
15 #include <cstring>
16 #include <ctime>
17 #include <iostream>
18 
19 using namespace Gaudi;
20 
21 // architecture dependent includes
22 #ifdef WIN32
23 
24 // this turns off a lot of useless Win stuff which conflicts with Gaudi. (found empirically)
25 # define NOATOM
26 # define NOGDI
27 # define NOGDICAPMASKS
28 # define NOMETAFILE
29 # define NOMINMAX
30 # define NOMSG
31 # define NOOPENFILE
32 # define NORASTEROPS
33 # define NOSCROLL
34 # define NOSOUND
35 # define NOSYSMETRICS
36 # define NOTEXTMETRIC
37 # define NOWH
38 # define NOCOMM
39 # define NOKANJI
40 # define NOCRYPT
41 # define NOMCX
42 
43 # include <windows.h>
44 
45 #else
46 // Linux
47 
48 # include <sys/time.h>
49 
50 #endif
51 
52 //-----------------------------------------------------------------------------
53 // Implementation file for class : Gaudi::Time
54 // Based on seal::Time
55 // 2005-12-15 : Marco Clemencic
56 //-----------------------------------------------------------------------------
57 
58 #ifdef WIN32
59 
68 // # define SECS_1601_TO_1970 ((369 * 365 + 89) * SECS_PER_DAY)
69 # define SECS_1601_TO_1970 ( ( 369 * 365 + 89 ) * 86400ui64 )
70 #endif
71 
72 #ifdef WIN32
73 static time_t timegm( struct tm* t ) {
74  // This code is adapted from wine, samba
75  time_t t1 = mktime( t );
76  struct tm gmt;
77  gmtime_s( &gmt, &t1 );
78  time_t t2 = mktime( &gmt );
79  return t1 + ( t1 - t2 );
80 }
81 #endif
82 
83 Time::Time( int year, int month, int day, int hour, int min, int sec, ValueType nsecs, bool local /* = true */ ) {
84  tm val;
85  memset( &val, 0, sizeof( val ) );
86  val.tm_sec = sec;
87  val.tm_min = min;
88  val.tm_hour = hour;
89  val.tm_mday = day;
90  val.tm_mon = month;
91  val.tm_year = year > 1900 ? year - 1900 : year;
92  val.tm_isdst = -1; // FIXME?
93 
94  m_nsecs = build( local, val, nsecs ).m_nsecs;
95 }
96 
97 #ifdef WIN32
98 
100 Time Time::from( const FILETIME* systime ) {
101  ValueType t = ( (ValueType)systime->dwHighDateTime << 32 ) + (ValueType)systime->dwLowDateTime;
102 
103  if ( t )
104  // Subtract bias (1970--1601 in 100ns), then convert to nanoseconds.
105  t = ( t - SECS_1601_TO_1970 * ( SEC_NSECS / 100 ) ) * 100;
106 
107  return Time( t );
108 }
109 #endif
110 
113 #ifdef WIN32
114  FILETIME ftime;
115  GetSystemTimeAsFileTime( &ftime );
116  return from( &ftime );
117 #else
118  timeval tv;
119  if ( gettimeofday( &tv, nullptr ) != 0 ) {
120  char buf[256];
121  std::ostringstream tag, msg;
122  tag << "errno=" << errno;
123  if ( strerror_r( errno, buf, 256 ) == 0 ) {
124  msg << buf;
125  } else {
126  msg << "Unknown error retrieving current time";
127  }
128  throw GaudiException( msg.str(), tag.str(), StatusCode::FAILURE );
129  }
130  return Time( tv.tv_sec, tv.tv_usec * 1000 );
131 #endif
132 }
133 
135 Time Time::build( bool local, const tm& base, TimeSpan diff /* = 0 */ ) {
136  tm tmp( base );
137  return Time( local ? mktime( &tmp ) : timegm( &tmp ), 0 ) + diff;
138 }
139 
144 tm Time::split( bool local, int* nsecpart /* = 0 */ ) const {
145  if ( nsecpart ) *nsecpart = (int)( m_nsecs % SEC_NSECS );
146 
147  time_t val = (time_t)( m_nsecs / SEC_NSECS );
148 
149  tm retval;
150  if ( local )
151  localtime_r( &val, &retval );
152  else
153  gmtime_r( &val, &retval );
154 
155  return retval;
156 }
157 
161 tm Time::utc( int* nsecpart /* = 0 */ ) const { return split( false, nsecpart ); }
162 
166 tm Time::local( int* nsecpart /* = 0 */ ) const { return split( true, nsecpart ); }
167 
169 int Time::year( bool local ) const { return split( local ).tm_year + 1900; }
170 
172 int Time::month( bool local ) const { return split( local ).tm_mon; }
173 
175 int Time::day( bool local ) const { return split( local ).tm_mday; }
176 
178 int Time::hour( bool local ) const { return split( local ).tm_hour; }
179 
181 int Time::minute( bool local ) const { return split( local ).tm_min; }
182 
186 int Time::second( bool local ) const { return split( local ).tm_sec; }
187 
191 int Time::nsecond( void ) const { return (int)( m_nsecs % SEC_NSECS ); }
192 
194 int Time::weekday( bool local ) const { return split( local ).tm_wday; }
195 
199 bool Time::isdst( bool local ) const { return split( local ).tm_isdst > 0; }
200 
207 Time::ValueType Time::utcoffset( int* daylight /* = 0 */ ) const {
208  ValueType n = 0;
209 
210 #ifndef WIN32
211  tm localtm = local();
212  n = localtm.tm_gmtoff;
213  if ( daylight ) *daylight = localtm.tm_isdst;
214 #else
215  // Adapted from WINE.
216  time_t utctime = (time_t)( m_nsecs / SEC_NSECS );
217  tm localtm;
218  localtime_s( &localtm, &utctime );
219  int savedaylight = localtm.tm_isdst;
220  tm gmt;
221  gmtime_s( &gmt, &utctime );
222 
223  gmt.tm_isdst = savedaylight;
224  n = utctime - mktime( &gmt );
225 
226  if ( daylight ) *daylight = savedaylight;
227 #endif
228  return n * SEC_NSECS;
229 }
230 
231 #ifdef WIN32
232 // disable warning
233 // C4996: 'tzname': This function or variable may be unsafe.
234 # pragma warning( push )
235 # pragma warning( disable : 4996 )
236 #endif
237 
241 const char* Time::timezone( int* daylight /* = 0 */ ) const {
242  tm localtm = local();
243  if ( daylight ) *daylight = localtm.tm_isdst;
244  // extern "C" { extern char *tzname [2]; }
245  return tzname[localtm.tm_isdst > 0 ? 1 : 0];
246 }
247 #ifdef WIN32
248 # pragma warning( pop )
249 #endif
250 
255 std::string Time::format( bool local, std::string spec ) const {
257  std::string result;
258  tm time = split( local );
259  std::string::size_type length = 0;
260 
261  // handle the special case of "%f"
262  std::string::size_type pos = spec.find( "%f" );
263  if ( std::string::npos != pos ) {
264  // Get the milliseconds string
265  std::string ms = nanoformat( 3, 3 );
266  // Replace all the occurrences of '%f' (if not preceded by '%')
267  while ( std::string::npos != pos ) {
268  if ( pos != 0 && spec[pos - 1] != '%' ) { spec.replace( pos, 2, ms ); }
269  pos = spec.find( "%f", pos + 1 ); // search for the next occurrence
270  }
271  }
272  const int MIN_BUF_SIZE = 128;
273  do {
274  // Guess how much we'll expand. If we go wrong, we'll expand again. (with a minimum)
275  result.resize( std::max<std::string::size_type>(
276  result.size() * 2, std::max<std::string::size_type>( spec.size() * 2, MIN_BUF_SIZE ) ),
277  0 );
278  length = ::strftime( &result[0], result.size(), spec.c_str(), &time );
279  } while ( !length );
280 
281  result.resize( length );
282  return result;
283 }
284 
295 std::string Time::nanoformat( size_t minwidth /* = 1 */, size_t maxwidth /* = 9 */ ) const {
296  TimeAssert( ( minwidth >= 1 ) && ( minwidth <= maxwidth ) && ( maxwidth <= 9 ),
297  "nanoformat options do not satisfy: 1 <= minwidth <= maxwidth <= 9" );
298 
299  // Calculate the nanosecond fraction. This will be < 1000000000.
300  int value = (int)( m_nsecs % SEC_NSECS );
301 
302  std::ostringstream buf;
303  (void)buf.fill( '0' );
304  buf.width( 9 );
305  buf << value;
306  std::string out = buf.str();
307  // Find the last non-0 char before maxwidth, but after minwidth
308  // (Note: -1 and +1 are to account for difference between position and size.
309  // moreover, npos + 1 == 0, so it is correct to say that 'not found' means size of 0)
310  size_t len = out.find_last_not_of( '0', maxwidth - 1 ) + 1;
311  // Truncate the output string to at least minwidth chars
312  out.resize( std::max( len, minwidth ) );
313  return out;
314 }
315 
317 unsigned Time::toDosDate( Time time ) {
318  // Use local time since DOS does too.
319  struct tm localtm = time.local();
320 
321  unsigned mday = localtm.tm_mday;
322  unsigned mon = localtm.tm_mon + 1;
323  unsigned year = ( localtm.tm_year > 80 ? localtm.tm_year - 80 : 0 );
324  unsigned sec = localtm.tm_sec / 2;
325  unsigned min = localtm.tm_min;
326  unsigned hour = localtm.tm_hour;
327  return ( mday << 16 | mon << 21 | year << 25 | sec | min << 5 | hour << 11 );
328 }
329 
331 Time Time::fromDosDate( unsigned dosDate ) {
332  // DOS times are generally local; treat it as UTC. This avoids
333  // any round-trip conversion and leaves only a presentation as an
334  // issue. Since not much can be known about the origin of the DOS
335  // times, it's generally best to present them as such (= in UTC).
336  struct tm localtm;
337  memset( &localtm, 0, sizeof( localtm ) );
338  localtm.tm_mday = ( dosDate >> 16 ) & 0x1f;
339  localtm.tm_mon = ( ( dosDate >> 21 ) & 0xf ) - 1;
340  localtm.tm_year = ( ( dosDate >> 25 ) & 0x7f ) + 80;
341  localtm.tm_hour = ( dosDate >> 11 ) & 0x1f;
342  localtm.tm_min = ( dosDate >> 5 ) & 0x3f;
343  localtm.tm_sec = ( dosDate & 0x1f ) * 2;
344  localtm.tm_isdst = -1;
345 
346  return Time( mktime( &localtm ), 0 );
347 }
Gaudi::Time::isdst
bool isdst(bool local) const
Check whether daylight savings is in effect.
Definition: Time.cpp:199
Gaudi::Time::year
int year(bool local) const
Get the year.
Definition: Time.cpp:169
Gaudi::Time::second
int second(bool local) const
Get the seconds, numbered [0,61] (allowing one or two leap seconds, years with leap seconds can have ...
Definition: Time.cpp:186
GaudiException
Definition: GaudiException.h:29
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
Gaudi::Time::Time
Time()=default
Initialize an empty (zero) time value.
Gaudi::Time::nanoformat
std::string nanoformat(size_t minwidth=1, size_t maxwidth=9) const
Format the nanosecond fractional part of the time as a string.
Definition: Time.cpp:295
Gaudi::Time::timezone
const char * timezone(int *daylight=0) const
Return the local timezone name that applies at this time value.
Definition: Time.cpp:241
Gaudi::Time::m_nsecs
ValueType m_nsecs
Definition: Time.h:335
Gaudi::Time::utcoffset
ValueType utcoffset(int *daylight=0) const
Return the number of nanoseconds that needs to be added to UTC to translate this time to the local ti...
Definition: Time.cpp:207
Gaudi::Time
Definition: Time.h:238
Gaudi::Units::ms
constexpr double ms
Definition: SystemOfUnits.h:153
Gaudi::TimeSpan
Definition: Time.h:59
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::Time::minute
int minute(bool local) const
Get the minute, numbered [0, 59].
Definition: Time.cpp:181
Gaudi::Time::format
std::string format(bool local, std::string spec="%c") const
Format the time using strftime.
Definition: Time.cpp:255
Gaudi::Time::toDosDate
static unsigned toDosDate(Time time)
Convert the Time t into a MS-DOS date format.
Definition: Time.cpp:317
Gaudi::Time::split
tm split(bool local, int *nsecpart=0) const
Break up the time to the standard representation, either in UTC (if local is false) or local time (if...
Definition: Time.cpp:144
TemplatedAlg
Definition: TemplatedAlg.cpp:22
Gaudi::Time::ValueType
long long ValueType
Definition: Time.h:242
time_r.h
Gaudi::Time::hour
int hour(bool local) const
Get the hour, numbered [0, 23].
Definition: Time.cpp:178
Gaudi::Time::weekday
int weekday(bool local) const
Get the day of week, numbered [0,6] and starting from Sunday.
Definition: Time.cpp:194
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
plotSpeedupsPyRoot.time
time
Definition: plotSpeedupsPyRoot.py:180
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Time.h
Gaudi::Time::SEC_NSECS
static const ValueType SEC_NSECS
Nanoseconds in one second.
Definition: Time.h:267
Gaudi::Time::current
static Time current()
Returns the current time.
Definition: Time.cpp:112
compareRootHistos.retval
retval
Definition: compareRootHistos.py:499
Gaudi::Time::local
tm local(int *nsecpart=0) const
Break up the time to the standard library representation, converting it first to local time.
Definition: Time.cpp:166
Gaudi::Time::month
int month(bool local) const
Get the month, numbered [0,11].
Definition: Time.cpp:172
Gaudi::Time::TimeAssert
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition: Time.h:338
check_ParticleID.base
base
Definition: check_ParticleID.py:24
t2
TemplatedAlg< double, bool > t2
Definition: TemplatedAlg.cpp:41
Gaudi::Time::nsecond
int nsecond() const
Get the nanoseconds.
Definition: Time.cpp:191
Gaudi::Time::day
int day(bool local) const
Get the day of month, numbered [1,31].
Definition: Time.cpp:175
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
Gaudi::Time::fromDosDate
static Time fromDosDate(unsigned dosDate)
Convert the MS-DOS date dosDate into a Time.
Definition: Time.cpp:331
Gaudi::Time::utc
tm utc(int *nsecpart=0) const
Break up the time to the standard library representation, keeping it in UTC.
Definition: Time.cpp:161
Gaudi::Time::build
static Time build(bool local, const tm &base, TimeSpan diff=0)
Construct a time from local time base and a delta diff.
Definition: Time.cpp:135
Gaudi::Functional::details::out
OptOut && out
Definition: details.h:196