]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/src/engine/timestamp.cpp
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * Copyright 2001-2004 David Abrahams.
9 * Copyright 2020 Nikita Kniazev.
10 * Distributed under the Boost Software License, Version 1.0.
11 * (See accompanying file LICENSE.txt or
12 * https://www.bfgroup.xyz/b2/LICENSE.txt)
16 * timestamp.c - get the timestamp of a file or archive member
19 * timestamp_from_path() - return timestamp for a path, if present
20 * timestamp_done() - free timestamp tables
23 * free_timestamps() - worker function for freeing timestamp table contents
27 #include "timestamp.h"
33 #include "jam_strings.h"
38 * BINDING - all known files
41 typedef struct _binding
46 #define BIND_SCANNED 0x01 /* if directory or arch, has been scanned */
50 #define BIND_INIT 0 /* never seen */
51 #define BIND_NOENTRY 1 /* timestamp requested but file never found */
52 #define BIND_SPOTTED 2 /* file found but not timed yet */
53 #define BIND_MISSING 3 /* file found but can not get timestamp */
54 #define BIND_FOUND 4 /* file found and time stamped */
56 /* update time - cleared if the there is nothing to bind */
60 static struct hash
* bindhash
= 0;
65 * timestamp_from_filetime() - Windows FILETIME --> timestamp conversion
67 * Lifted shamelessly from the CPython implementation.
70 void timestamp_from_filetime( timestamp
* const t
, FILETIME
const * const ft
)
72 /* Seconds between 1.1.1601 and 1.1.1970 */
73 static __int64
const secs_between_epochs
= 11644473600;
75 /* We can not simply cast and dereference a FILETIME, since it might not be
76 * aligned properly. __int64 type variables are expected to be aligned to an
77 * 8 byte boundary while FILETIME structures may be aligned to any 4 byte
78 * boundary. Using an incorrectly aligned __int64 variable may cause a
79 * performance penalty on some platforms or even exceptions on others
80 * (documented on MSDN).
83 memcpy( &in
, ft
, sizeof( in
) );
85 /* FILETIME resolution: 100ns. */
86 timestamp_init( t
, (time_t)( ( in
/ 10000000 ) - secs_between_epochs
),
87 (int)( in
% 10000000 ) * 100 );
92 void timestamp_clear( timestamp
* const time
)
94 time
->secs
= time
->nsecs
= 0;
98 int timestamp_cmp( timestamp
const * const lhs
, timestamp
const * const rhs
)
101 lhs
->secs
== rhs
->secs
102 ? lhs
->nsecs
- rhs
->nsecs
103 : lhs
->secs
- rhs
->secs
);
107 void timestamp_copy( timestamp
* const target
, timestamp
const * const source
)
109 target
->secs
= source
->secs
;
110 target
->nsecs
= source
->nsecs
;
114 void timestamp_current( timestamp
* const t
)
117 /* GetSystemTimeAsFileTime()'s resolution seems to be about 15 ms on Windows
118 * XP and under a millisecond on Windows 7.
121 GetSystemTimeAsFileTime( &ft
);
122 timestamp_from_filetime( t
, &ft
);
123 #elif defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME) && \
124 (!defined(__GLIBC__) || (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17))
125 /* Some older versions of XCode define _POSIX_TIMERS, but don't actually
126 * have clock_gettime. Check CLOCK_REALTIME as well. Prior to glibc 2.17,
127 * clock_gettime requires -lrt. This is a non-critical feature, so
128 * we just disable it to keep bootstrapping simple.
131 clock_gettime( CLOCK_REALTIME
, &ts
);
132 timestamp_init( t
, ts
.tv_sec
, ts
.tv_nsec
);
134 timestamp_init( t
, time( 0 ), 0 );
139 int timestamp_empty( timestamp
const * const time
)
141 return !time
->secs
&& !time
->nsecs
;
146 * timestamp_from_path() - return timestamp for a path, if present
149 void timestamp_from_path( timestamp
* const time
, OBJECT
* const path
)
151 PROFILE_ENTER( timestamp
);
153 if ( file_time( path
, time
) < 0 )
154 timestamp_clear( time
);
156 PROFILE_EXIT( timestamp
);
160 void timestamp_init( timestamp
* const time
, time_t const secs
, int const nsecs
168 void timestamp_max( timestamp
* const max
, timestamp
const * const lhs
,
169 timestamp
const * const rhs
)
171 if ( timestamp_cmp( lhs
, rhs
) > 0 )
172 timestamp_copy( max
, lhs
);
174 timestamp_copy( max
, rhs
);
178 static char const * timestamp_formatstr( timestamp
const * const time
,
179 char const * const format
)
181 static char result1
[ 500 ];
182 static char result2
[ 500 ];
183 strftime( result1
, sizeof( result1
) / sizeof( *result1
), format
, gmtime(
185 sprintf( result2
, result1
, time
->nsecs
);
190 char const * timestamp_str( timestamp
const * const time
)
192 return timestamp_formatstr( time
, "%Y-%m-%d %H:%M:%S.%%09d +0000" );
196 char const * timestamp_timestr( timestamp
const * const time
)
198 return timestamp_formatstr( time
, "%H:%M:%S.%%09d" );
203 * free_timestamps() - worker function for freeing timestamp table contents
206 static void free_timestamps( void * xbinding
, void * data
)
208 object_free( ( (BINDING
*)xbinding
)->name
);
213 * timestamp_done() - free timestamp tables
216 void timestamp_done()
220 hashenumerate( bindhash
, free_timestamps
, 0 );
221 hashdone( bindhash
);
226 * timestamp_delta_seconds() - seconds from time a to b.
228 double timestamp_delta_seconds( timestamp
const * const a
, timestamp
const * const b
)
230 return difftime(b
->secs
, a
->secs
) + (b
->nsecs
- a
->nsecs
) * 1.0E-9;