1 /* stat-related time functions.
3 Copyright (C) 2005 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert. */
26 /* STAT_TIMESPEC (ST, ST_XTIM) is the ST_XTIM member for *ST of type
27 struct timespec, if available. If not, then STAT_TIMESPEC_NS (ST,
28 ST_XTIM) is the nanosecond component of the ST_XTIM member for *ST,
29 if available. ST_XTIM can be st_atim, st_ctim, or st_mtim for
30 access, status change, or data modification time, respectively.
32 These macros are private to stat-time.h. */
33 #if defined HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
34 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
35 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
36 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
37 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
38 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
39 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
40 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
43 /* Return the nanosecond component of *ST's access time. */
44 static inline long int
45 get_stat_atime_ns (struct stat const *st)
47 # if defined STAT_TIMESPEC
48 return STAT_TIMESPEC (st, st_atim).tv_nsec;
49 # elif defined STAT_TIMESPEC_NS
50 return STAT_TIMESPEC_NS (st, st_atim);
51 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
52 return st->st_spare1 * 1000;
58 /* Return the nanosecond component of *ST's status change time. */
59 static inline long int
60 get_stat_ctime_ns (struct stat const *st)
62 # if defined STAT_TIMESPEC
63 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
64 # elif defined STAT_TIMESPEC_NS
65 return STAT_TIMESPEC_NS (st, st_ctim);
66 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
67 return st->st_spare3 * 1000;
73 /* Return the nanosecond component of *ST's data modification time. */
74 static inline long int
75 get_stat_mtime_ns (struct stat const *st)
77 # if defined STAT_TIMESPEC
78 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
79 # elif defined STAT_TIMESPEC_NS
80 return STAT_TIMESPEC_NS (st, st_mtim);
81 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
82 return st->st_spare2 * 1000;
88 /* Return *ST's access time. */
89 static inline struct timespec
90 get_stat_atime (struct stat const *st)
93 return STAT_TIMESPEC (st, st_atim);
96 t.tv_sec = st->st_atime;
97 t.tv_nsec = get_stat_atime_ns (st);
102 /* Return *ST's status change time. */
103 static inline struct timespec
104 get_stat_ctime (struct stat const *st)
107 return STAT_TIMESPEC (st, st_ctim);
110 t.tv_sec = st->st_ctime;
111 t.tv_nsec = get_stat_ctime_ns (st);
116 /* Return *ST's data modification time. */
117 static inline struct timespec
118 get_stat_mtime (struct stat const *st)
121 return STAT_TIMESPEC (st, st_mtim);
124 t.tv_sec = st->st_mtime;
125 t.tv_nsec = get_stat_mtime_ns (st);