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 # ifdef TYPEOF_STRUCT_STAT_ST_ATIM_IS_STRUCT_TIMESPEC
35 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim)
37 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.tv_nsec)
39 #elif defined HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC
40 # define STAT_TIMESPEC(st, st_xtim) ((st)->st_xtim##espec)
41 #elif defined HAVE_STRUCT_STAT_ST_ATIMENSEC
42 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim##ensec)
43 #elif defined HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC
44 # define STAT_TIMESPEC_NS(st, st_xtim) ((st)->st_xtim.st__tim.tv_nsec)
47 /* Return the nanosecond component of *ST's access time. */
48 static inline long int
49 get_stat_atime_ns (struct stat const *st)
51 # if defined STAT_TIMESPEC
52 return STAT_TIMESPEC (st, st_atim).tv_nsec;
53 # elif defined STAT_TIMESPEC_NS
54 return STAT_TIMESPEC_NS (st, st_atim);
55 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
56 return st->st_spare1 * 1000;
62 /* Return the nanosecond component of *ST's status change time. */
63 static inline long int
64 get_stat_ctime_ns (struct stat const *st)
66 # if defined STAT_TIMESPEC
67 return STAT_TIMESPEC (st, st_ctim).tv_nsec;
68 # elif defined STAT_TIMESPEC_NS
69 return STAT_TIMESPEC_NS (st, st_ctim);
70 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
71 return st->st_spare3 * 1000;
77 /* Return the nanosecond component of *ST's data modification time. */
78 static inline long int
79 get_stat_mtime_ns (struct stat const *st)
81 # if defined STAT_TIMESPEC
82 return STAT_TIMESPEC (st, st_mtim).tv_nsec;
83 # elif defined STAT_TIMESPEC_NS
84 return STAT_TIMESPEC_NS (st, st_mtim);
85 # elif defined HAVE_STRUCT_STAT_ST_SPARE1
86 return st->st_spare2 * 1000;
92 /* Return *ST's access time. */
93 static inline struct timespec
94 get_stat_atime (struct stat const *st)
97 return STAT_TIMESPEC (st, st_atim);
100 t.tv_sec = st->st_atime;
101 t.tv_nsec = get_stat_atime_ns (st);
106 /* Return *ST's status change time. */
107 static inline struct timespec
108 get_stat_ctime (struct stat const *st)
111 return STAT_TIMESPEC (st, st_ctim);
114 t.tv_sec = st->st_ctime;
115 t.tv_nsec = get_stat_ctime_ns (st);
120 /* Return *ST's data modification time. */
121 static inline struct timespec
122 get_stat_mtime (struct stat const *st)
125 return STAT_TIMESPEC (st, st_mtim);
128 t.tv_sec = st->st_mtime;
129 t.tv_nsec = get_stat_mtime_ns (st);