2023-12-04 04:29:08 +03:00
/*
2007-08-29 17:07:03 +04:00
Unix SMB / CIFS implementation .
time utility functions
2011-06-10 16:59:35 +04:00
Copyright ( C ) Andrew Tridgell 1992 - 2004
Copyright ( C ) Stefan ( metze ) Metzmacher 2002
Copyright ( C ) Jeremy Allison 2007
Copyright ( C ) Andrew Bartlett 2011
2007-08-29 17:07:03 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
2023-12-04 04:29:08 +03:00
2007-08-29 17:07:03 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2023-12-04 04:29:08 +03:00
2007-08-29 17:07:03 +04:00
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifndef _SAMBA_TIME_H_
# define _SAMBA_TIME_H_
2014-09-22 13:38:18 +04:00
# include <stdbool.h>
# include <stdint.h>
# include <talloc.h>
2022-07-22 20:36:59 +03:00
# include <time.h>
2014-09-22 13:38:18 +04:00
2010-01-07 02:40:09 +03:00
# ifndef TIME_T_MIN
/* we use 0 here, because (time_t)-1 means error */
# define TIME_T_MIN 0
# endif
/*
* we use the INT32_MAX here as on 64 bit systems ,
* gmtime ( ) fails with INT64_MAX
*/
# ifndef TIME_T_MAX
# define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
# endif
2019-12-06 19:44:37 +03:00
/*
* According to Windows API FileTimeToSystemTime ( ) documentation the highest
2019-12-09 20:56:59 +03:00
* allowed value " ... must be less than 0x8000000000000000. " .
2019-12-06 19:44:37 +03:00
*/
# define NTTIME_MAX INT64_MAX
/*
* The lowest possible value when NTTIME = 0 is used as sentinel value .
*/
# define NTTIME_MIN 1
/*
* NTTIME_OMIT in a setinfo tells us to not modify the corresponding on - disk
* timestamp value .
*/
# define NTTIME_OMIT 0
/*
* Disable automatic timestamp updates , as described in MS - FSA . Samba doesn ' t
* implement this yet .
*/
# define NTTIME_FREEZE UINT64_MAX
2021-10-05 16:10:10 +03:00
# define NTTIME_THAW (UINT64_MAX - 1)
2019-12-06 19:44:37 +03:00
2019-11-27 14:28:30 +03:00
# define SAMBA_UTIME_NOW UTIME_NOW
# define SAMBA_UTIME_OMIT UTIME_OMIT
2010-01-07 02:40:09 +03:00
2007-08-29 17:07:03 +04:00
/* 64 bit time (100 nanosec) 1601 - cifs6.txt, section 3.5, page 30, 4 byte aligned */
typedef uint64_t NTTIME ;
2023-02-28 14:53:03 +03:00
# define NTTIME_USEC (10UL)
# define NTTIME_MSEC (1000UL * NTTIME_USEC)
2007-08-29 17:07:03 +04:00
/**
External access to time_t_min and time_t_max .
* */
2011-11-03 17:34:08 +04:00
time_t get_time_t_max ( void ) ;
2007-08-29 17:07:03 +04:00
/**
a gettimeofday wrapper
* */
2011-11-03 17:34:08 +04:00
void GetTimeOfDay ( struct timeval * tval ) ;
2007-08-29 17:07:03 +04:00
2010-08-30 14:10:29 +04:00
/**
a wrapper to preferably get the monotonic time
* */
2011-11-03 17:34:08 +04:00
void clock_gettime_mono ( struct timespec * tp ) ;
2010-08-30 14:10:29 +04:00
2010-09-06 22:27:24 +04:00
/**
a wrapper to preferably get the monotonic time in s
* */
2011-11-03 17:34:08 +04:00
time_t time_mono ( time_t * t ) ;
2010-09-06 22:27:24 +04:00
2007-08-29 17:07:03 +04:00
/**
interpret an 8 byte " filetime " structure to a time_t
It ' s originally in " 100ns units since jan 1st 1601 "
* */
2011-11-03 17:34:08 +04:00
time_t nt_time_to_unix ( NTTIME nt ) ;
2007-08-29 17:07:03 +04:00
/**
put a 8 byte filetime from a time_t
This takes GMT as input
* */
2011-11-03 17:34:08 +04:00
void unix_to_nt_time ( NTTIME * nt , time_t t ) ;
2007-08-29 17:07:03 +04:00
/**
check if it ' s a null unix time
* */
2011-11-03 17:34:08 +04:00
bool null_time ( time_t t ) ;
2007-08-29 17:07:03 +04:00
/**
check if it ' s a null NTTIME
* */
2011-11-03 17:34:08 +04:00
bool null_nttime ( NTTIME t ) ;
2007-08-29 17:07:03 +04:00
/**
put a dos date into a buffer ( time / date format )
This takes GMT time and puts local time in the buffer
* */
2011-11-03 17:34:08 +04:00
void push_dos_date ( uint8_t * buf , int offset , time_t unixdate , int zone_offset ) ;
2007-08-29 17:07:03 +04:00
/**
put a dos date into a buffer ( date / time format )
This takes GMT time and puts local time in the buffer
* */
2011-11-03 17:34:08 +04:00
void push_dos_date2 ( uint8_t * buf , int offset , time_t unixdate , int zone_offset ) ;
2007-08-29 17:07:03 +04:00
/**
put a dos 32 bit " unix like " date into a buffer . This routine takes
GMT and converts it to LOCAL time before putting it ( most SMBs assume
localtime for this sort of date )
* */
2011-11-03 17:34:08 +04:00
void push_dos_date3 ( uint8_t * buf , int offset , time_t unixdate , int zone_offset ) ;
2007-08-29 17:07:03 +04:00
/**
create a unix date ( int GMT ) from a dos date ( which is actually in
localtime )
* */
2011-11-03 17:34:08 +04:00
time_t pull_dos_date ( const uint8_t * date_ptr , int zone_offset ) ;
2007-08-29 17:07:03 +04:00
/**
like make_unix_date ( ) but the words are reversed
* */
2011-11-03 17:34:08 +04:00
time_t pull_dos_date2 ( const uint8_t * date_ptr , int zone_offset ) ;
2007-08-29 17:07:03 +04:00
/**
create a unix GMT date from a dos date in 32 bit " unix like " format
these generally arrive as localtimes , with corresponding DST
* */
2011-11-03 17:34:08 +04:00
time_t pull_dos_date3 ( const uint8_t * date_ptr , int zone_offset ) ;
2007-08-29 17:07:03 +04:00
2011-02-22 09:59:51 +03:00
/**
Return a date and time as a string ( optionally with microseconds )
format is % Y / % m / % d % H : % M : % S if strftime is available
* */
char * timeval_string ( TALLOC_CTX * ctx , const struct timeval * tp , bool hires ) ;
2019-11-27 18:31:50 +03:00
struct timeval_buf ;
const char * timespec_string_buf ( const struct timespec * tp ,
bool hires ,
struct timeval_buf * buf ) ;
2011-02-22 09:59:51 +03:00
/**
Return the current date and time as a string ( optionally with microseconds )
format is % Y / % m / % d % H : % M : % S if strftime is available
* */
char * current_timestring ( TALLOC_CTX * ctx , bool hires ) ;
2017-07-27 13:54:28 +03:00
/**
Return a date and time as a string ( optionally with microseconds )
format is % Y % m % d_ % H % M % S or % Y % m % d_ % H % M % S_ % us
* */
char * minimal_timeval_string ( TALLOC_CTX * ctx , const struct timeval * tp , bool hires ) ;
/**
Return the current date and time as a string ( optionally with microseconds )
format is % Y % m % d_ % H % M % S or % Y % m % d_ % H % M % S_ % us
* */
char * current_minimal_timestring ( TALLOC_CTX * ctx , bool hires ) ;
2007-08-29 17:07:03 +04:00
/**
return a HTTP / 1.0 time string
* */
2011-11-03 17:34:08 +04:00
char * http_timestring ( TALLOC_CTX * mem_ctx , time_t t ) ;
2007-08-29 17:07:03 +04:00
/**
Return the date and time as a string
2011-02-22 09:59:51 +03:00
format is % a % b % e % X % Y % Z
2007-08-29 17:07:03 +04:00
* */
2011-11-03 17:34:08 +04:00
char * timestring ( TALLOC_CTX * mem_ctx , time_t t ) ;
2007-08-29 17:07:03 +04:00
/**
return a talloced string representing a NTTIME for human consumption
*/
2011-11-03 17:34:08 +04:00
const char * nt_time_string ( TALLOC_CTX * mem_ctx , NTTIME nt ) ;
2007-08-29 17:07:03 +04:00
/**
put a NTTIME into a packet
*/
2011-11-03 17:34:08 +04:00
void push_nttime ( uint8_t * base , uint16_t offset , NTTIME t ) ;
2007-08-29 17:07:03 +04:00
/**
pull a NTTIME from a packet
*/
2011-11-03 17:34:08 +04:00
NTTIME pull_nttime ( uint8_t * base , uint16_t offset ) ;
2007-08-29 17:07:03 +04:00
/**
return ( tv1 - tv2 ) in microseconds
*/
2011-11-03 17:34:08 +04:00
int64_t usec_time_diff ( const struct timeval * tv1 , const struct timeval * tv2 ) ;
2007-08-29 17:07:03 +04:00
2010-08-30 19:56:37 +04:00
/**
return ( tp1 - tp2 ) in nanoseconds
*/
2011-11-03 17:34:08 +04:00
int64_t nsec_time_diff ( const struct timespec * tp1 , const struct timespec * tp2 ) ;
2010-08-30 19:56:37 +04:00
2007-08-29 17:07:03 +04:00
/**
return a zero timeval
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_zero ( void ) ;
2007-08-29 17:07:03 +04:00
/**
return true if a timeval is zero
*/
2011-11-03 17:34:08 +04:00
bool timeval_is_zero ( const struct timeval * tv ) ;
2007-08-29 17:07:03 +04:00
/**
return a timeval for the current time
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_current ( void ) ;
2007-08-29 17:07:03 +04:00
/**
return a timeval ofs microseconds after tv
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_add ( const struct timeval * tv ,
2007-08-29 17:07:03 +04:00
uint32_t secs , uint32_t usecs ) ;
/**
return the sum of two timeval structures
*/
struct timeval timeval_sum ( const struct timeval * tv1 ,
const struct timeval * tv2 ) ;
/**
return a timeval secs / usecs into the future
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_current_ofs ( uint32_t secs , uint32_t usecs ) ;
2007-08-29 17:07:03 +04:00
2011-06-01 05:51:15 +04:00
/**
return a timeval milliseconds into the future
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_current_ofs_msec ( uint32_t msecs ) ;
2011-06-01 05:51:15 +04:00
2011-06-01 05:54:51 +04:00
/**
return a timeval microseconds into the future
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_current_ofs_usec ( uint32_t usecs ) ;
2011-06-01 05:54:51 +04:00
2007-08-29 17:07:03 +04:00
/**
2023-12-04 04:29:08 +03:00
compare two timeval structures .
2007-08-29 17:07:03 +04:00
Return - 1 if tv1 < tv2
Return 0 if tv1 = = tv2
Return 1 if tv1 > tv2
*/
2011-11-03 17:34:08 +04:00
int timeval_compare ( const struct timeval * tv1 , const struct timeval * tv2 ) ;
2007-08-29 17:07:03 +04:00
/**
return true if a timer is in the past
*/
2011-11-03 17:34:08 +04:00
bool timeval_expired ( const struct timeval * tv ) ;
2007-08-29 17:07:03 +04:00
/**
return the number of seconds elapsed between two times
*/
2011-11-03 17:34:08 +04:00
double timeval_elapsed2 ( const struct timeval * tv1 , const struct timeval * tv2 ) ;
2007-08-29 17:07:03 +04:00
/**
return the number of seconds elapsed since a given time
*/
2011-11-03 17:34:08 +04:00
double timeval_elapsed ( const struct timeval * tv ) ;
2007-08-29 17:07:03 +04:00
2013-11-20 15:00:17 +04:00
/**
return the number of seconds elapsed between two times
*/
double timespec_elapsed2 ( const struct timespec * ts1 ,
const struct timespec * ts2 ) ;
/**
return the number of seconds elapsed since a given time
*/
double timespec_elapsed ( const struct timespec * ts ) ;
2007-08-29 17:07:03 +04:00
/**
return the lesser of two timevals
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_min ( const struct timeval * tv1 ,
2007-08-29 17:07:03 +04:00
const struct timeval * tv2 ) ;
/**
return the greater of two timevals
*/
2011-11-03 17:34:08 +04:00
struct timeval timeval_max ( const struct timeval * tv1 ,
2007-08-29 17:07:03 +04:00
const struct timeval * tv2 ) ;
/**
convert a timeval to a NTTIME
*/
2011-11-03 17:34:08 +04:00
NTTIME timeval_to_nttime ( const struct timeval * tv ) ;
2007-08-29 17:07:03 +04:00
/**
convert a NTTIME to a timeval
*/
2011-11-03 17:34:08 +04:00
void nttime_to_timeval ( struct timeval * tv , NTTIME t ) ;
2007-08-29 17:07:03 +04:00
/**
return the UTC offset in seconds west of UTC , or 0 if it cannot be determined
*/
2011-11-03 17:34:08 +04:00
int get_time_zone ( time_t t ) ;
2007-08-29 17:07:03 +04:00
2008-02-09 16:24:24 +03:00
/**
check if 2 NTTIMEs are equal .
*/
bool nt_time_equal ( NTTIME * t1 , NTTIME * t2 ) ;
2007-08-29 17:07:03 +04:00
2008-10-12 01:57:44 +04:00
void interpret_dos_date ( uint32_t date , int * year , int * month , int * day , int * hour , int * minute , int * second ) ;
2022-01-07 00:58:20 +03:00
struct timespec nt_time_to_unix_timespec_raw ( NTTIME nt ) ;
2014-06-18 16:21:06 +04:00
struct timespec nt_time_to_unix_timespec ( NTTIME nt ) ;
2008-10-12 01:57:44 +04:00
2008-10-20 20:59:51 +04:00
time_t convert_timespec_to_time_t ( struct timespec ts ) ;
struct timespec convert_time_t_to_timespec ( time_t t ) ;
bool null_timespec ( struct timespec ts ) ;
2011-09-22 22:33:22 +04:00
struct timespec convert_timeval_to_timespec ( const struct timeval tv ) ;
struct timeval convert_timespec_to_timeval ( const struct timespec ts ) ;
struct timespec timespec_current ( void ) ;
struct timespec timespec_min ( const struct timespec * ts1 ,
const struct timespec * ts2 ) ;
int timespec_compare ( const struct timespec * ts1 , const struct timespec * ts2 ) ;
void round_timespec_to_sec ( struct timespec * ts ) ;
void round_timespec_to_usec ( struct timespec * ts ) ;
2019-09-03 18:50:54 +03:00
void round_timespec_to_nttime ( struct timespec * ts ) ;
2014-06-18 16:21:06 +04:00
NTTIME unix_timespec_to_nt_time ( struct timespec ts ) ;
2019-01-17 13:06:26 +03:00
void normalize_timespec ( struct timespec * ts ) ;
2011-09-22 22:33:22 +04:00
2019-11-26 20:22:17 +03:00
/*
* Functions supporting the full range of time_t and struct timespec values ,
* including 0 , - 1 and all other negative values . These functions don ' t use 0 or
* - 1 values as sentinel to denote " unset " variables , but use the POSIX 2008
* define UTIME_OMIT from utimensat ( 2 ) .
*/
bool is_omit_timespec ( const struct timespec * ts ) ;
struct timespec make_omit_timespec ( void ) ;
2019-12-04 14:11:05 +03:00
NTTIME full_timespec_to_nt_time ( const struct timespec * ts ) ;
2019-12-05 17:00:19 +03:00
struct timespec nt_time_to_full_timespec ( NTTIME nt ) ;
2019-12-04 14:46:11 +03:00
time_t full_timespec_to_time_t ( const struct timespec * ts ) ;
2019-12-05 17:07:51 +03:00
time_t nt_time_to_full_time_t ( NTTIME nt ) ;
2019-12-04 17:05:19 +03:00
struct timespec time_t_to_full_timespec ( time_t t ) ;
2019-11-26 20:22:17 +03:00
2020-08-05 03:49:42 +03:00
/*
* Functions to get and set the number of nanoseconds for times in a stat field .
* If the stat has timestamp granularity less than nanosecond , then the set_ *
* operations will be lossy .
*/
struct stat ;
time_t get_atimensec ( const struct stat * ) ;
time_t get_mtimensec ( const struct stat * ) ;
time_t get_ctimensec ( const struct stat * ) ;
void set_atimensec ( struct stat * , time_t ) ;
void set_mtimensec ( struct stat * , time_t ) ;
void set_ctimensec ( struct stat * , time_t ) ;
/* These are convenience wrappers for the above getters. */
struct timespec get_atimespec ( const struct stat * ) ;
struct timespec get_mtimespec ( const struct stat * ) ;
struct timespec get_ctimespec ( const struct stat * ) ;
2007-08-29 17:07:03 +04:00
# endif /* _SAMBA_TIME_H_ */