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