1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_TIME_H
3*4882a593Smuzhiyun #define _LINUX_TIME_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun # include <linux/cache.h>
6*4882a593Smuzhiyun # include <linux/math64.h>
7*4882a593Smuzhiyun # include <linux/time64.h>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun extern struct timezone sys_tz;
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun int get_timespec64(struct timespec64 *ts,
12*4882a593Smuzhiyun const struct __kernel_timespec __user *uts);
13*4882a593Smuzhiyun int put_timespec64(const struct timespec64 *ts,
14*4882a593Smuzhiyun struct __kernel_timespec __user *uts);
15*4882a593Smuzhiyun int get_itimerspec64(struct itimerspec64 *it,
16*4882a593Smuzhiyun const struct __kernel_itimerspec __user *uit);
17*4882a593Smuzhiyun int put_itimerspec64(const struct itimerspec64 *it,
18*4882a593Smuzhiyun struct __kernel_itimerspec __user *uit);
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun extern time64_t mktime64(const unsigned int year, const unsigned int mon,
21*4882a593Smuzhiyun const unsigned int day, const unsigned int hour,
22*4882a593Smuzhiyun const unsigned int min, const unsigned int sec);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /* Some architectures do not supply their own clocksource.
25*4882a593Smuzhiyun * This is mainly the case in architectures that get their
26*4882a593Smuzhiyun * inter-tick times by reading the counter on their interval
27*4882a593Smuzhiyun * timer. Since these timers wrap every tick, they're not really
28*4882a593Smuzhiyun * useful as clocksources. Wrapping them to act like one is possible
29*4882a593Smuzhiyun * but not very efficient. So we provide a callout these arches
30*4882a593Smuzhiyun * can implement for use with the jiffies clocksource to provide
31*4882a593Smuzhiyun * finer then tick granular time.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
34*4882a593Smuzhiyun extern u32 (*arch_gettimeoffset)(void);
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #ifdef CONFIG_POSIX_TIMERS
38*4882a593Smuzhiyun extern void clear_itimer(void);
39*4882a593Smuzhiyun #else
clear_itimer(void)40*4882a593Smuzhiyun static inline void clear_itimer(void) {}
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun extern long do_utimes(int dfd, const char __user *filename, struct timespec64 *times, int flags);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * Similar to the struct tm in userspace <time.h>, but it needs to be here so
47*4882a593Smuzhiyun * that the kernel source is self contained.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun struct tm {
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * the number of seconds after the minute, normally in the range
52*4882a593Smuzhiyun * 0 to 59, but can be up to 60 to allow for leap seconds
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun int tm_sec;
55*4882a593Smuzhiyun /* the number of minutes after the hour, in the range 0 to 59*/
56*4882a593Smuzhiyun int tm_min;
57*4882a593Smuzhiyun /* the number of hours past midnight, in the range 0 to 23 */
58*4882a593Smuzhiyun int tm_hour;
59*4882a593Smuzhiyun /* the day of the month, in the range 1 to 31 */
60*4882a593Smuzhiyun int tm_mday;
61*4882a593Smuzhiyun /* the number of months since January, in the range 0 to 11 */
62*4882a593Smuzhiyun int tm_mon;
63*4882a593Smuzhiyun /* the number of years since 1900 */
64*4882a593Smuzhiyun long tm_year;
65*4882a593Smuzhiyun /* the number of days since Sunday, in the range 0 to 6 */
66*4882a593Smuzhiyun int tm_wday;
67*4882a593Smuzhiyun /* the number of days since January 1, in the range 0 to 365 */
68*4882a593Smuzhiyun int tm_yday;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun void time64_to_tm(time64_t totalsecs, int offset, struct tm *result);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun # include <linux/time32.h>
74*4882a593Smuzhiyun
itimerspec64_valid(const struct itimerspec64 * its)75*4882a593Smuzhiyun static inline bool itimerspec64_valid(const struct itimerspec64 *its)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun if (!timespec64_valid(&(its->it_interval)) ||
78*4882a593Smuzhiyun !timespec64_valid(&(its->it_value)))
79*4882a593Smuzhiyun return false;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return true;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /**
85*4882a593Smuzhiyun * time_after32 - compare two 32-bit relative times
86*4882a593Smuzhiyun * @a: the time which may be after @b
87*4882a593Smuzhiyun * @b: the time which may be before @a
88*4882a593Smuzhiyun *
89*4882a593Smuzhiyun * time_after32(a, b) returns true if the time @a is after time @b.
90*4882a593Smuzhiyun * time_before32(b, a) returns true if the time @b is before time @a.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * Similar to time_after(), compare two 32-bit timestamps for relative
93*4882a593Smuzhiyun * times. This is useful for comparing 32-bit seconds values that can't
94*4882a593Smuzhiyun * be converted to 64-bit values (e.g. due to disk format or wire protocol
95*4882a593Smuzhiyun * issues) when it is known that the times are less than 68 years apart.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun #define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
98*4882a593Smuzhiyun #define time_before32(b, a) time_after32(a, b)
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /**
101*4882a593Smuzhiyun * time_between32 - check if a 32-bit timestamp is within a given time range
102*4882a593Smuzhiyun * @t: the time which may be within [l,h]
103*4882a593Smuzhiyun * @l: the lower bound of the range
104*4882a593Smuzhiyun * @h: the higher bound of the range
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * time_before32(t, l, h) returns true if @l <= @t <= @h. All operands are
107*4882a593Smuzhiyun * treated as 32-bit integers.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * Equivalent to !(time_before32(@t, @l) || time_after32(@t, @h)).
110*4882a593Smuzhiyun */
111*4882a593Smuzhiyun #define time_between32(t, l, h) ((u32)(h) - (u32)(l) >= (u32)(t) - (u32)(l))
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun # include <vdso/time.h>
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun #endif
116