xref: /openbmc/u-boot/include/linux/time.h (revision 57efeb04)
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 
97     /* compute hour, min, and sec */
98     res->tm_hour = (int) (rem / SECSPERHOUR);
99     rem %= SECSPERHOUR;
100     res->tm_min = (int) (rem / SECSPERMIN);
101     res->tm_sec = (int) (rem % SECSPERMIN);
102 
103     /* compute day of week */
104     if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
105 	res->tm_wday += DAYSPERWEEK;
106 
107     /* compute year & day of year */
108     y = EPOCH_YEAR;
109     if (days >= 0)
110     {
111 	for (;;)
112 	{
113 	    yleap = isleap(y);
114 	    if (days < year_lengths[yleap])
115 		break;
116 	    y++;
117 	    days -= year_lengths[yleap];
118 	}
119     }
120     else
121     {
122 	do
123 	{
124 	    --y;
125 	    yleap = isleap(y);
126 	    days += year_lengths[yleap];
127 	} while (days < 0);
128     }
129 
130     res->tm_year = y - YEAR_BASE;
131     res->tm_yday = days;
132     ip = mon_lengths[yleap];
133     for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
134 	days -= ip[res->tm_mon];
135     res->tm_mday = days + 1;
136 
137     /* set daylight saving time flag */
138     res->tm_isdst = -1;
139 
140     return (res);
141 }
142 
143 static inline char *
144 _DEFUN (ctime_r, (tim_p, result),
145 	_CONST time_t * tim_p _AND
146 	char * result)
147 
148 {
149     struct tm tm;
150     return asctime_r (localtime_r (tim_p, &tm), result);
151 }
152 
153 #endif
154