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