1 /* stat-related time functions.
3 Copyright (C) 2005, 2007, 2009-2012 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
25 #if defined (TIME_H_DEFINES_STRUCT_TIMESPEC)
27 #elif defined (SYS_TIME_H_DEFINES_STRUCT_TIMESPEC)
28 # include <sys/time.h>
29 #elif defined (PTHREAD_H_DEFINES_STRUCT_TIMESPEC)
33 #ifndef HAVE_STRUCT_TIMESPEC
41 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
42 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
43 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
44 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
45 for access, status change, data modification, or birth (creation)
48 These macros are private to stat-time.h. */
49 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
50 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
51 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
53 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
55 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
56 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
57 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
58 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
59 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
60 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
63 /* Return the nanosecond component of *ST's access time. */
64 static inline long int
65 get_stat_atime_ns (struct stat
const *st
)
67 # if defined STAT_TIMESPEC
68 return STAT_TIMESPEC (st
, st_atim
).tv_nsec
;
69 # elif defined STAT_TIMESPEC_NS
70 return STAT_TIMESPEC_NS (st
, st_atim
);
76 /* Return the nanosecond component of *ST's status change time. */
77 static inline long int
78 get_stat_ctime_ns (struct stat
const *st
)
80 # if defined STAT_TIMESPEC
81 return STAT_TIMESPEC (st
, st_ctim
).tv_nsec
;
82 # elif defined STAT_TIMESPEC_NS
83 return STAT_TIMESPEC_NS (st
, st_ctim
);
89 /* Return the nanosecond component of *ST's data modification time. */
90 static inline long int
91 get_stat_mtime_ns (struct stat
const *st
)
93 # if defined STAT_TIMESPEC
94 return STAT_TIMESPEC (st
, st_mtim
).tv_nsec
;
95 # elif defined STAT_TIMESPEC_NS
96 return STAT_TIMESPEC_NS (st
, st_mtim
);
102 /* Return the nanosecond component of *ST's birth time. */
103 static inline long int
104 get_stat_birthtime_ns (struct stat
const *st
)
106 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
107 return STAT_TIMESPEC (st
, st_birthtim
).tv_nsec
;
108 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
109 return STAT_TIMESPEC_NS (st
, st_birthtim
);
111 /* Avoid a "parameter unused" warning. */
117 /* Return *ST's access time. */
118 static inline struct timespec
119 get_stat_atime (struct stat
const *st
)
122 return STAT_TIMESPEC (st
, st_atim
);
125 t
.tv_sec
= st
->st_atime
;
126 t
.tv_nsec
= get_stat_atime_ns (st
);
131 /* Return *ST's status change time. */
132 static inline struct timespec
133 get_stat_ctime (struct stat
const *st
)
136 return STAT_TIMESPEC (st
, st_ctim
);
139 t
.tv_sec
= st
->st_ctime
;
140 t
.tv_nsec
= get_stat_ctime_ns (st
);
145 /* Return *ST's data modification time. */
146 static inline struct timespec
147 get_stat_mtime (struct stat
const *st
)
150 return STAT_TIMESPEC (st
, st_mtim
);
153 t
.tv_sec
= st
->st_mtime
;
154 t
.tv_nsec
= get_stat_mtime_ns (st
);
160 timespec_cmp (struct timespec a
, struct timespec b
)
162 return (a
.tv_sec
< b
.tv_sec
164 : (a
.tv_sec
> b
.tv_sec
166 : (int) (a
.tv_nsec
- b
.tv_nsec
)));
169 /* Return *ST's birth time, if available; otherwise return a value
170 with tv_sec and tv_nsec both equal to -1. */
171 static inline struct timespec
172 get_stat_birthtime (struct stat
const *st
)
176 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
177 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
178 t
= STAT_TIMESPEC (st
, st_birthtim
);
179 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
180 t
.tv_sec
= st
->st_birthtime
;
181 t
.tv_nsec
= st
->st_birthtimensec
;
182 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
183 /* Native Windows platforms (but not Cygwin) put the "file creation
184 time" in st_ctime (!). See
185 <http://msdn2.microsoft.com/de-de/library/14h5k7ff(VS.80).aspx>. */
186 t
.tv_sec
= st
->st_ctime
;
189 /* Birth time is not supported. */
192 /* Avoid a "parameter unused" warning. */
196 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
197 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
198 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
199 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
200 using zero. Attempt to work around this problem. Alas, this can
201 report failure even for valid time stamps. Also, NetBSD
202 sometimes returns junk in the birth time fields; work around this
203 bug if it is detected. */
204 if (! (t
.tv_sec
&& 0 <= t
.tv_nsec
&& t
.tv_nsec
< 1000000000))