blob: 0de44cff8d7d887bc6507a7c50f8563b5b9f9974 [file] [log] [blame]
wdenk012771d2002-03-08 21:31:05 +00001#ifndef _LINUX_TIME_H
2#define _LINUX_TIME_H
3
AKASHI Takahiro80a75bc2019-11-13 09:44:50 +09004#include <rtc.h>
AKASHI Takahiro9825d552019-11-13 09:44:52 +09005#include <vsprintf.h>
wdenk012771d2002-03-08 21:31:05 +00006#include <linux/types.h>
7
8#define _DEFUN(a,b,c) a(c)
9#define _CONST const
10#define _AND ,
11
12#define _REENT_ONLY
13
Igor Prusov700a06c2023-11-09 20:10:02 +030014#define MSEC_PER_SEC 1000L
15#define USEC_PER_MSEC 1000L
16#define NSEC_PER_USEC 1000L
17#define NSEC_PER_MSEC 1000000L
18#define USEC_PER_SEC 1000000L
19#define NSEC_PER_SEC 1000000000L
20#define PSEC_PER_SEC 1000000000000LL
21#define FSEC_PER_SEC 1000000000000000LL
22
wdenk012771d2002-03-08 21:31:05 +000023#define SECSPERMIN 60L
24#define MINSPERHOUR 60L
25#define HOURSPERDAY 24L
26#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
27#define SECSPERDAY (SECSPERHOUR * HOURSPERDAY)
28#define DAYSPERWEEK 7
29#define MONSPERYEAR 12
30
31#define YEAR_BASE 1900
32#define EPOCH_YEAR 1970
33#define EPOCH_WDAY 4
34
35#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
36
wdenk012771d2002-03-08 21:31:05 +000037/* Used by other time functions. */
38struct tm {
39 int tm_sec; /* Seconds. [0-60] (1 leap second) */
40 int tm_min; /* Minutes. [0-59] */
41 int tm_hour; /* Hours. [0-23] */
42 int tm_mday; /* Day. [1-31] */
43 int tm_mon; /* Month. [0-11] */
44 int tm_year; /* Year - 1900. */
45 int tm_wday; /* Day of week. [0-6] */
46 int tm_yday; /* Days in year.[0-365] */
47 int tm_isdst; /* DST. [-1/0/1]*/
48
49# ifdef __USE_BSD
50 long int tm_gmtoff; /* Seconds east of UTC. */
51 __const char *tm_zone; /* Timezone abbreviation. */
52# else
53 long int __tm_gmtoff; /* Seconds east of UTC. */
54 __const char *__tm_zone; /* Timezone abbreviation. */
55# endif
56};
57
58static inline char *
59_DEFUN (asctime_r, (tim_p, result),
60 _CONST struct tm *tim_p _AND
61 char *result)
62{
63 static _CONST char day_name[7][3] = {
64 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
65 };
66 static _CONST char mon_name[12][3] = {
67 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
68 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
69 };
70
71 sprintf (result, "%.3s %.3s %.2d %.2d:%.2d:%.2d %d\n",
72 day_name[tim_p->tm_wday],
73 mon_name[tim_p->tm_mon],
74 tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min,
75 tim_p->tm_sec, 1900 + tim_p->tm_year);
76 return result;
77}
78
79static inline struct tm *
80_DEFUN (localtime_r, (tim_p, res),
81 _CONST time_t * tim_p _AND
82 struct tm *res)
83{
84 static _CONST int mon_lengths[2][MONSPERYEAR] = {
85 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
86 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
87 } ;
88
89 static _CONST int year_lengths[2] = {
90 365,
91 366
92 } ;
93
94 long days, rem;
95 int y;
96 int yleap;
97 _CONST int *ip;
98
99 days = ((long) *tim_p) / SECSPERDAY;
100 rem = ((long) *tim_p) % SECSPERDAY;
101 while (rem < 0)
102 {
103 rem += SECSPERDAY;
104 --days;
105 }
wdenk012771d2002-03-08 21:31:05 +0000106
107 /* compute hour, min, and sec */
108 res->tm_hour = (int) (rem / SECSPERHOUR);
109 rem %= SECSPERHOUR;
110 res->tm_min = (int) (rem / SECSPERMIN);
111 res->tm_sec = (int) (rem % SECSPERMIN);
112
113 /* compute day of week */
114 if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
115 res->tm_wday += DAYSPERWEEK;
116
117 /* compute year & day of year */
118 y = EPOCH_YEAR;
119 if (days >= 0)
120 {
121 for (;;)
122 {
123 yleap = isleap(y);
124 if (days < year_lengths[yleap])
125 break;
126 y++;
127 days -= year_lengths[yleap];
128 }
129 }
130 else
131 {
132 do
133 {
134 --y;
135 yleap = isleap(y);
136 days += year_lengths[yleap];
137 } while (days < 0);
138 }
139
140 res->tm_year = y - YEAR_BASE;
141 res->tm_yday = days;
142 ip = mon_lengths[yleap];
143 for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
144 days -= ip[res->tm_mon];
145 res->tm_mday = days + 1;
146
147 /* set daylight saving time flag */
148 res->tm_isdst = -1;
149
150 return (res);
151}
152
153static inline char *
154_DEFUN (ctime_r, (tim_p, result),
155 _CONST time_t * tim_p _AND
156 char * result)
157
158{
159 struct tm tm;
160 return asctime_r (localtime_r (tim_p, &tm), result);
161}
162
AKASHI Takahiro80a75bc2019-11-13 09:44:50 +0900163#ifdef CONFIG_LIB_DATE
164time64_t mktime64(const unsigned int year, const unsigned int mon,
165 const unsigned int day, const unsigned int hour,
166 const unsigned int min, const unsigned int sec);
167#endif
168
wdenk012771d2002-03-08 21:31:05 +0000169#endif