Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
[emacs.git] / lib / stat-time.h
blob9e45e8556550852e4afbd82b996862dd99cde245
1 /* stat-related time functions.
3 Copyright (C) 2005, 2007, 2009-2017 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. */
20 #ifndef STAT_TIME_H
21 #define STAT_TIME_H 1
23 #include <sys/stat.h>
24 #include <time.h>
26 #ifndef _GL_INLINE_HEADER_BEGIN
27 #error "Please include config.h first."
28 #endif
29 _GL_INLINE_HEADER_BEGIN
30 #ifndef _GL_STAT_TIME_INLINE
31 # define _GL_STAT_TIME_INLINE _GL_INLINE
32 #endif
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
38 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
39 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
40 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
41 if available. ST_XTIM can be st_atim, st_ctim, st_mtim, or st_birthtim
42 for access, status change, data modification, or birth (creation)
43 time respectively.
45 These macros are private to stat-time.h. */
46 #if _GL_WINDOWS_STAT_TIMESPEC || defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
47 # if _GL_WINDOWS_STAT_TIMESPEC || defined TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
48 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
49 # else
50 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
51 # endif
52 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
53 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
54 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
55 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
56 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
57 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
58 #endif
60 /* Return the nanosecond component of *ST's access time. */
61 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
62 get_stat_atime_ns (struct stat const *st)
64 # if defined STAT_TIMESPEC
65 return STAT_TIMESPEC (st, st_atim).tv_nsec;
66 # elif defined STAT_TIMESPEC_NS
67 return STAT_TIMESPEC_NS (st, st_atim);
68 # else
69 return 0;
70 # endif
73 /* Return the nanosecond component of *ST's status change time. */
74 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
75 get_stat_ctime_ns (struct stat const *st)
77 # if defined STAT_TIMESPEC
78 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
79 # elif defined STAT_TIMESPEC_NS
80 return STAT_TIMESPEC_NS (st, st_ctim);
81 # else
82 return 0;
83 # endif
86 /* Return the nanosecond component of *ST's data modification time. */
87 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
88 get_stat_mtime_ns (struct stat const *st)
90 # if defined STAT_TIMESPEC
91 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
92 # elif defined STAT_TIMESPEC_NS
93 return STAT_TIMESPEC_NS (st, st_mtim);
94 # else
95 return 0;
96 # endif
99 /* Return the nanosecond component of *ST's birth time. */
100 _GL_STAT_TIME_INLINE long int _GL_ATTRIBUTE_PURE
101 get_stat_birthtime_ns (struct stat const *st)
103 # if defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC
104 return STAT_TIMESPEC (st, st_birthtim).tv_nsec;
105 # elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
106 return STAT_TIMESPEC_NS (st, st_birthtim);
107 # else
108 /* Avoid a "parameter unused" warning. */
109 (void) st;
110 return 0;
111 # endif
114 /* Return *ST's access time. */
115 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
116 get_stat_atime (struct stat const *st)
118 #ifdef STAT_TIMESPEC
119 return STAT_TIMESPEC (st, st_atim);
120 #else
121 struct timespec t;
122 t.tv_sec = st->st_atime;
123 t.tv_nsec = get_stat_atime_ns (st);
124 return t;
125 #endif
128 /* Return *ST's status change time. */
129 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
130 get_stat_ctime (struct stat const *st)
132 #ifdef STAT_TIMESPEC
133 return STAT_TIMESPEC (st, st_ctim);
134 #else
135 struct timespec t;
136 t.tv_sec = st->st_ctime;
137 t.tv_nsec = get_stat_ctime_ns (st);
138 return t;
139 #endif
142 /* Return *ST's data modification time. */
143 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
144 get_stat_mtime (struct stat const *st)
146 #ifdef STAT_TIMESPEC
147 return STAT_TIMESPEC (st, st_mtim);
148 #else
149 struct timespec t;
150 t.tv_sec = st->st_mtime;
151 t.tv_nsec = get_stat_mtime_ns (st);
152 return t;
153 #endif
156 /* Return *ST's birth time, if available; otherwise return a value
157 with tv_sec and tv_nsec both equal to -1. */
158 _GL_STAT_TIME_INLINE struct timespec _GL_ATTRIBUTE_PURE
159 get_stat_birthtime (struct stat const *st)
161 struct timespec t;
163 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
164 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
165 t = STAT_TIMESPEC (st, st_birthtim);
166 #elif defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC
167 t.tv_sec = st->st_birthtime;
168 t.tv_nsec = st->st_birthtimensec;
169 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
170 /* Native Windows platforms (but not Cygwin) put the "file creation
171 time" in st_ctime (!). See
172 <https://msdn.microsoft.com/en-us/library/14h5k7ff(VS.80).aspx>. */
173 # if _GL_WINDOWS_STAT_TIMESPEC
174 t = st->st_ctim;
175 # else
176 t.tv_sec = st->st_ctime;
177 t.tv_nsec = 0;
178 # endif
179 #else
180 /* Birth time is not supported. */
181 t.tv_sec = -1;
182 t.tv_nsec = -1;
183 /* Avoid a "parameter unused" warning. */
184 (void) st;
185 #endif
187 #if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \
188 || defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \
189 || defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
190 /* FreeBSD and NetBSD sometimes signal the absence of knowledge by
191 using zero. Attempt to work around this problem. Alas, this can
192 report failure even for valid timestamps. Also, NetBSD
193 sometimes returns junk in the birth time fields; work around this
194 bug if it is detected. */
195 if (! (t.tv_sec && 0 <= t.tv_nsec && t.tv_nsec < 1000000000))
197 t.tv_sec = -1;
198 t.tv_nsec = -1;
200 #endif
202 return t;
205 #ifdef __cplusplus
207 #endif
209 _GL_INLINE_HEADER_END
211 #endif