1 #ifndef _LINUX_TIME_H 2 #define _LINUX_TIME_H 3 4 # include <linux/cache.h> 5 # include <linux/seqlock.h> 6 # include <linux/math64.h> 7 # include <linux/time64.h> 8 9 extern struct timezone sys_tz; 10 11 int get_timespec64(struct timespec64 *ts, 12 const struct timespec __user *uts); 13 int put_timespec64(const struct timespec64 *ts, 14 struct timespec __user *uts); 15 int get_itimerspec64(struct itimerspec64 *it, 16 const struct itimerspec __user *uit); 17 int put_itimerspec64(const struct itimerspec64 *it, 18 struct itimerspec __user *uit); 19 20 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) 21 22 static inline int timespec_equal(const struct timespec *a, 23 const struct timespec *b) 24 { 25 return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); 26 } 27 28 /* 29 * lhs < rhs: return <0 30 * lhs == rhs: return 0 31 * lhs > rhs: return >0 32 */ 33 static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs) 34 { 35 if (lhs->tv_sec < rhs->tv_sec) 36 return -1; 37 if (lhs->tv_sec > rhs->tv_sec) 38 return 1; 39 return lhs->tv_nsec - rhs->tv_nsec; 40 } 41 42 extern time64_t mktime64(const unsigned int year, const unsigned int mon, 43 const unsigned int day, const unsigned int hour, 44 const unsigned int min, const unsigned int sec); 45 46 /** 47 * Deprecated. Use mktime64(). 48 */ 49 static inline unsigned long mktime(const unsigned int year, 50 const unsigned int mon, const unsigned int day, 51 const unsigned int hour, const unsigned int min, 52 const unsigned int sec) 53 { 54 return mktime64(year, mon, day, hour, min, sec); 55 } 56 57 extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); 58 59 static inline struct timespec timespec_add(struct timespec lhs, 60 struct timespec rhs) 61 { 62 struct timespec ts_delta; 63 set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec, 64 lhs.tv_nsec + rhs.tv_nsec); 65 return ts_delta; 66 } 67 68 /* 69 * sub = lhs - rhs, in normalized form 70 */ 71 static inline struct timespec timespec_sub(struct timespec lhs, 72 struct timespec rhs) 73 { 74 struct timespec ts_delta; 75 set_normalized_timespec(&ts_delta, lhs.tv_sec - rhs.tv_sec, 76 lhs.tv_nsec - rhs.tv_nsec); 77 return ts_delta; 78 } 79 80 /* 81 * Returns true if the timespec is norm, false if denorm: 82 */ 83 static inline bool timespec_valid(const struct timespec *ts) 84 { 85 /* Dates before 1970 are bogus */ 86 if (ts->tv_sec < 0) 87 return false; 88 /* Can't have more nanoseconds then a second */ 89 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) 90 return false; 91 return true; 92 } 93 94 static inline bool timespec_valid_strict(const struct timespec *ts) 95 { 96 if (!timespec_valid(ts)) 97 return false; 98 /* Disallow values that could overflow ktime_t */ 99 if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) 100 return false; 101 return true; 102 } 103 104 static inline bool timeval_valid(const struct timeval *tv) 105 { 106 /* Dates before 1970 are bogus */ 107 if (tv->tv_sec < 0) 108 return false; 109 110 /* Can't have more microseconds then a second */ 111 if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC) 112 return false; 113 114 return true; 115 } 116 117 extern struct timespec timespec_trunc(struct timespec t, unsigned gran); 118 119 /* Some architectures do not supply their own clocksource. 120 * This is mainly the case in architectures that get their 121 * inter-tick times by reading the counter on their interval 122 * timer. Since these timers wrap every tick, they're not really 123 * useful as clocksources. Wrapping them to act like one is possible 124 * but not very efficient. So we provide a callout these arches 125 * can implement for use with the jiffies clocksource to provide 126 * finer then tick granular time. 127 */ 128 #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET 129 extern u32 (*arch_gettimeoffset)(void); 130 #endif 131 132 struct itimerval; 133 extern int do_setitimer(int which, struct itimerval *value, 134 struct itimerval *ovalue); 135 extern int do_getitimer(int which, struct itimerval *value); 136 137 extern long do_utimes(int dfd, const char __user *filename, struct timespec64 *times, int flags); 138 139 /* 140 * Similar to the struct tm in userspace <time.h>, but it needs to be here so 141 * that the kernel source is self contained. 142 */ 143 struct tm { 144 /* 145 * the number of seconds after the minute, normally in the range 146 * 0 to 59, but can be up to 60 to allow for leap seconds 147 */ 148 int tm_sec; 149 /* the number of minutes after the hour, in the range 0 to 59*/ 150 int tm_min; 151 /* the number of hours past midnight, in the range 0 to 23 */ 152 int tm_hour; 153 /* the day of the month, in the range 1 to 31 */ 154 int tm_mday; 155 /* the number of months since January, in the range 0 to 11 */ 156 int tm_mon; 157 /* the number of years since 1900 */ 158 long tm_year; 159 /* the number of days since Sunday, in the range 0 to 6 */ 160 int tm_wday; 161 /* the number of days since January 1, in the range 0 to 365 */ 162 int tm_yday; 163 }; 164 165 void time64_to_tm(time64_t totalsecs, int offset, struct tm *result); 166 167 /** 168 * time_to_tm - converts the calendar time to local broken-down time 169 * 170 * @totalsecs the number of seconds elapsed since 00:00:00 on January 1, 1970, 171 * Coordinated Universal Time (UTC). 172 * @offset offset seconds adding to totalsecs. 173 * @result pointer to struct tm variable to receive broken-down time 174 */ 175 static inline void time_to_tm(time_t totalsecs, int offset, struct tm *result) 176 { 177 time64_to_tm(totalsecs, offset, result); 178 } 179 180 /** 181 * timespec_to_ns - Convert timespec to nanoseconds 182 * @ts: pointer to the timespec variable to be converted 183 * 184 * Returns the scalar nanosecond representation of the timespec 185 * parameter. 186 */ 187 static inline s64 timespec_to_ns(const struct timespec *ts) 188 { 189 return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; 190 } 191 192 /** 193 * timeval_to_ns - Convert timeval to nanoseconds 194 * @ts: pointer to the timeval variable to be converted 195 * 196 * Returns the scalar nanosecond representation of the timeval 197 * parameter. 198 */ 199 static inline s64 timeval_to_ns(const struct timeval *tv) 200 { 201 return ((s64) tv->tv_sec * NSEC_PER_SEC) + 202 tv->tv_usec * NSEC_PER_USEC; 203 } 204 205 /** 206 * ns_to_timespec - Convert nanoseconds to timespec 207 * @nsec: the nanoseconds value to be converted 208 * 209 * Returns the timespec representation of the nsec parameter. 210 */ 211 extern struct timespec ns_to_timespec(const s64 nsec); 212 213 /** 214 * ns_to_timeval - Convert nanoseconds to timeval 215 * @nsec: the nanoseconds value to be converted 216 * 217 * Returns the timeval representation of the nsec parameter. 218 */ 219 extern struct timeval ns_to_timeval(const s64 nsec); 220 221 /** 222 * timespec_add_ns - Adds nanoseconds to a timespec 223 * @a: pointer to timespec to be incremented 224 * @ns: unsigned nanoseconds value to be added 225 * 226 * This must always be inlined because its used from the x86-64 vdso, 227 * which cannot call other kernel functions. 228 */ 229 static __always_inline void timespec_add_ns(struct timespec *a, u64 ns) 230 { 231 a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); 232 a->tv_nsec = ns; 233 } 234 235 static inline bool itimerspec64_valid(const struct itimerspec64 *its) 236 { 237 if (!timespec64_valid(&(its->it_interval)) || 238 !timespec64_valid(&(its->it_value))) 239 return false; 240 241 return true; 242 } 243 244 /** 245 * time_after32 - compare two 32-bit relative times 246 * @a: the time which may be after @b 247 * @b: the time which may be before @a 248 * 249 * time_after32(a, b) returns true if the time @a is after time @b. 250 * time_before32(b, a) returns true if the time @b is before time @a. 251 * 252 * Similar to time_after(), compare two 32-bit timestamps for relative 253 * times. This is useful for comparing 32-bit seconds values that can't 254 * be converted to 64-bit values (e.g. due to disk format or wire protocol 255 * issues) when it is known that the times are less than 68 years apart. 256 */ 257 #define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0) 258 #define time_before32(b, a) time_after32(a, b) 259 #endif 260