xref: /drstd/dlibc/src/unix/header/time/mod.rs (revision 86982c5e9b2eaa583327251616ee822c36288824)
1 //! time implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
2 
3 use core::convert::{TryFrom, TryInto};
4 
5 use crate::unix::{
6     header::errno::EOVERFLOW,
7     platform,
8 };
9 
10 pub use self::constants::*;
11 use clock_gettime;
12 pub mod constants;
13 mod strftime;
14 
15 #[repr(C)]
16 pub struct tm {
17     pub tm_sec: ::c_int,
18     pub tm_min: ::c_int,
19     pub tm_hour: ::c_int,
20     pub tm_mday: ::c_int,
21     pub tm_mon: ::c_int,
22     pub tm_year: ::c_int,
23     pub tm_wday: ::c_int,
24     pub tm_yday: ::c_int,
25     pub tm_isdst: ::c_int,
26     pub tm_gmtoff: ::c_long,
27     pub tm_zone: *const ::c_char,
28 }
29 
30 unsafe impl Sync for tm {}
31 
32 // The C Standard says that localtime and gmtime return the same pointer.
33 static mut TM: tm = tm {
34     tm_sec: 0,
35     tm_min: 0,
36     tm_hour: 0,
37     tm_mday: 0,
38     tm_mon: 0,
39     tm_year: 0,
40     tm_wday: 0,
41     tm_yday: 0,
42     tm_isdst: 0,
43     tm_gmtoff: 0,
44     tm_zone: UTC,
45 };
46 
47 // The C Standard says that ctime and asctime return the same pointer.
48 static mut ASCTIME: [::c_char; 26] = [0; 26];
49 
50 #[repr(C)]
51 pub struct itimerspec {
52     pub it_interval: ::timespec,
53     pub it_value: ::timespec,
54 }
55 
56 pub struct sigevent;
57 
58 #[no_mangle]
59 pub unsafe extern "C" fn asctime(timeptr: *const tm) -> *mut ::c_char {
60     asctime_r(timeptr, ASCTIME.as_mut_ptr().cast())
61 }
62 
63 #[no_mangle]
64 pub unsafe extern "C" fn asctime_r(tm: *const tm, buf: *mut ::c_char) -> *mut ::c_char {
65     let tm_sec = (*tm).tm_sec;
66     let tm_min = (*tm).tm_min;
67     let tm_hour = (*tm).tm_hour;
68     let tm_mday = (*tm).tm_mday;
69     let tm_mon = (*tm).tm_mon;
70     let tm_year = (*tm).tm_year;
71     let tm_wday = (*tm).tm_wday;
72 
73     /* Panic when we run into undefined behavior.
74      *
75      * POSIX says (since issue 7) that asctime()/asctime_r() cause UB
76      * when the tm member values would cause out-of-bounds array access
77      * or overflow the output buffer. This contrasts with ISO C11+,
78      * which specifies UB for any tm members being outside their normal
79      * ranges. While POSIX explicitly defers to the C standard in case
80      * of contradictions, the assertions below follow the interpretation
81      * that POSIX simply defines some of C's undefined behavior, rather
82      * than conflict with the ISO standard.
83      *
84      * Note that C's "%.2d" formatting, unlike Rust's "{:02}"
85      * formatting, does not count a minus sign against the two digits to
86      * print, meaning that we must reject all negative values for
87      * seconds, minutes and hours. However, C's "%3d" (for day-of-month)
88      * is similar to Rust's "{:3}".
89      *
90      * To avoid year overflow problems (in Rust, where numeric overflow
91      * is considered an error), we subtract 1900 from the endpoints,
92      * rather than adding to the tm_year value. POSIX' requirement that
93      * tm_year be at most {INT_MAX}-1990 is satisfied for all legal
94      * values of {INT_MAX} through the max-4-digit requirement on the
95      * year.
96      *
97      * The tm_mon and tm_wday fields are used for array access and thus
98      * will already cause a panic in Rust code when out of range.
99      * However, using the assertions below allows a consistent error
100      * message for all fields. */
101     const OUT_OF_RANGE_MESSAGE: &str = "tm member out of range";
102 
103     assert!(0 <= tm_sec && tm_sec <= 99, "{}", OUT_OF_RANGE_MESSAGE);
104     assert!(0 <= tm_min && tm_min <= 99, "{}", OUT_OF_RANGE_MESSAGE);
105     assert!(0 <= tm_hour && tm_hour <= 99, "{}", OUT_OF_RANGE_MESSAGE);
106     assert!(-99 <= tm_mday && tm_mday <= 999, "{}", OUT_OF_RANGE_MESSAGE);
107     assert!(0 <= tm_mon && tm_mon <= 11, "{}", OUT_OF_RANGE_MESSAGE);
108     assert!(
109         -999 - 1900 <= tm_year && tm_year <= 9999 - 1900,
110         "{}",
111         OUT_OF_RANGE_MESSAGE
112     );
113     assert!(0 <= tm_wday && tm_wday <= 6, "{}", OUT_OF_RANGE_MESSAGE);
114 
115     // At this point, we can safely use the values as given.
116     let write_result = core::fmt::write(
117         // buf may be either `*mut u8` or `*mut i8`
118         &mut platform::UnsafeStringWriter(buf.cast()),
119         format_args!(
120             "{:.3} {:.3}{:3} {:02}:{:02}:{:02} {}\n",
121             DAY_NAMES[usize::try_from(tm_wday).unwrap()],
122             MON_NAMES[usize::try_from(tm_mon).unwrap()],
123             tm_mday,
124             tm_hour,
125             tm_min,
126             tm_sec,
127             1900 + tm_year
128         ),
129     );
130     match write_result {
131         Ok(_) => buf,
132         Err(_) => {
133             /* asctime()/asctime_r() or the equivalent sprintf() call
134              * have no defined errno setting */
135             core::ptr::null_mut()
136         }
137     }
138 }
139 
140 #[no_mangle]
141 pub extern "C" fn clock() -> ::clock_t {
142     let mut ts = core::mem::MaybeUninit::<::timespec>::uninit();
143 
144     if unsafe{clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ts.as_mut_ptr())} != 0 {
145         return -1;
146     }
147     let ts = unsafe { ts.assume_init() };
148 
149     let clocks =
150         ts.tv_sec * CLOCKS_PER_SEC as i64 + (ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)) as i64;
151     match ::clock_t::try_from(clocks) {
152         Ok(ok) => ok,
153         Err(_err) => -1,
154     }
155 }
156 
157 #[no_mangle]
158 pub extern "C" fn clock_getres(clock_id: ::clockid_t, res: *mut ::timespec) -> ::c_int {
159     unimplemented!();
160 }
161 
162 // #[no_mangle]
163 // pub extern "C" fn clock_gettime(clock_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int {
164 //     unsafe{platform::pal::clock_gettime(clock_id, tp)}
165 // }
166 
167 // #[no_mangle]
168 pub extern "C" fn clock_settime(clock_id: ::clockid_t, tp: *const ::timespec) -> ::c_int {
169     unimplemented!();
170 }
171 
172 #[no_mangle]
173 pub unsafe extern "C" fn ctime(clock: *const ::time_t) -> *mut ::c_char {
174     asctime(localtime(clock))
175 }
176 
177 #[no_mangle]
178 pub unsafe extern "C" fn ctime_r(clock: *const ::time_t, buf: *mut ::c_char) -> *mut ::c_char {
179     // Using MaybeUninit<tm> seems to cause a panic during the build process
180     let mut tm1 = tm {
181         tm_sec: 0,
182         tm_min: 0,
183         tm_hour: 0,
184         tm_mday: 0,
185         tm_mon: 0,
186         tm_year: 0,
187         tm_wday: 0,
188         tm_yday: 0,
189         tm_isdst: 0,
190         tm_gmtoff: 0,
191         tm_zone: core::ptr::null_mut(),
192     };
193     localtime_r(clock, &mut tm1);
194     asctime_r(&tm1, buf)
195 }
196 
197 #[no_mangle]
198 pub extern "C" fn difftime(time1: ::time_t, time0: ::time_t) -> ::c_double {
199     (time1 - time0) as ::c_double
200 }
201 
202 // #[no_mangle]
203 pub extern "C" fn getdate(string: *const ::c_char) -> tm {
204     unimplemented!();
205 }
206 
207 #[no_mangle]
208 pub unsafe extern "C" fn gmtime(timer: *const ::time_t) -> *mut tm {
209     gmtime_r(timer, &mut TM)
210 }
211 
212 const MONTH_DAYS: [[::c_int; 12]; 2] = [
213     // Non-leap years:
214     [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
215     // Leap years:
216     [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
217 ];
218 
219 #[inline(always)]
220 fn leap_year(year: ::c_int) -> bool {
221     year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
222 }
223 
224 #[no_mangle]
225 pub unsafe extern "C" fn gmtime_r(clock: *const ::time_t, result: *mut tm) -> *mut tm {
226     /* For the details of the algorithm used here, see
227      * http://howardhinnant.github.io/date_algorithms.html#civil_from_days
228      * Note that we need 0-based months here, though.
229      * Overall, this implementation should generate correct results as
230      * long as the tm_year value will fit in a ::c_int. */
231     const SECS_PER_DAY: ::time_t = 24 * 60 * 60;
232     const DAYS_PER_ERA: ::time_t = 146097;
233 
234     let unix_secs = *clock;
235 
236     /* Day number here is possibly negative, remainder will always be
237      * nonnegative when using Euclidean division */
238     let unix_days: ::time_t = unix_secs.div_euclid(SECS_PER_DAY);
239 
240     /* In range [0, 86399]. Needs a u32 since this is larger (at least
241      * theoretically) than the guaranteed range of ::c_int */
242     let secs_of_day: u32 = unix_secs.rem_euclid(SECS_PER_DAY).try_into().unwrap();
243 
244     /* Shift origin from 1970-01-01 to 0000-03-01 and find out where we
245      * are in terms of 400-year eras since then */
246     let days_since_origin = unix_days + 719468;
247     let era = days_since_origin.div_euclid(DAYS_PER_ERA);
248     let day_of_era = days_since_origin.rem_euclid(DAYS_PER_ERA);
249     let year_of_era =
250         (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / 365;
251 
252     /* "transformed" here refers to dates in a calendar where years
253      * start on March 1 */
254     let year_transformed = year_of_era + 400 * era; // retain large range, don't convert to ::c_int yet
255     let day_of_year_transformed: ::c_int = (day_of_era
256         - (365 * year_of_era + year_of_era / 4 - year_of_era / 100))
257         .try_into()
258         .unwrap();
259     let month_transformed: ::c_int = (5 * day_of_year_transformed + 2) / 153;
260 
261     // Convert back to calendar with year starting on January 1
262     let month: ::c_int = (month_transformed + 2) % 12; // adapted to 0-based months
263     let year: ::time_t = if month < 2 {
264         year_transformed + 1
265     } else {
266         year_transformed
267     };
268 
269     /* Subtract 1900 *before* converting down to ::c_int in order to
270      * maximize the range of input timestamps that will succeed */
271     match ::c_int::try_from(year - 1900) {
272         Ok(year_less_1900) => {
273             let mday: ::c_int = (day_of_year_transformed - (153 * month_transformed + 2) / 5 + 1)
274                 .try_into()
275                 .unwrap();
276 
277             /* 1970-01-01 was a Thursday. Again, Euclidean division is
278              * used to ensure a nonnegative remainder (range [0, 6]). */
279             let wday: ::c_int = ((unix_days + 4).rem_euclid(7)).try_into().unwrap();
280 
281             /* Yes, duplicated code for now (to work on non-c_int-values
282              * so that we are not constrained by the subtraction of
283              * 1900) */
284             let is_leap_year: bool = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
285 
286             /* For dates in January or February, we use the fact that
287              * January 1 is always 306 days after March 1 in the
288              * previous year. */
289             let yday: ::c_int = if month < 2 {
290                 day_of_year_transformed - 306
291             } else {
292                 day_of_year_transformed + if is_leap_year { 60 } else { 59 }
293             };
294 
295             let hour: ::c_int = (secs_of_day / (60 * 60)).try_into().unwrap();
296             let min: ::c_int = ((secs_of_day / 60) % 60).try_into().unwrap();
297             let sec: ::c_int = (secs_of_day % 60).try_into().unwrap();
298 
299             *result = tm {
300                 tm_sec: sec,
301                 tm_min: min,
302                 tm_hour: hour,
303                 tm_mday: mday,
304                 tm_mon: month,
305                 tm_year: year_less_1900,
306                 tm_wday: wday,
307                 tm_yday: yday,
308                 tm_isdst: 0,
309                 tm_gmtoff: 0,
310                 tm_zone: UTC,
311             };
312 
313             result
314         }
315         Err(_) => {
316             platform::errno = EOVERFLOW;
317             core::ptr::null_mut()
318         }
319     }
320 }
321 
322 #[no_mangle]
323 pub unsafe extern "C" fn localtime(clock: *const ::time_t) -> *mut tm {
324     localtime_r(clock, &mut TM)
325 }
326 
327 #[no_mangle]
328 pub unsafe extern "C" fn localtime_r(clock: *const ::time_t, t: *mut tm) -> *mut tm {
329     // TODO: Change tm_isdst, tm_gmtoff, tm_zone
330     gmtime_r(clock, t)
331 }
332 
333 #[no_mangle]
334 pub unsafe extern "C" fn mktime(t: *mut tm) -> ::time_t {
335     let mut year = (*t).tm_year + 1900;
336     let mut month = (*t).tm_mon;
337     let mut day = (*t).tm_mday as i64 - 1;
338 
339     let leap = if leap_year(year) { 1 } else { 0 };
340 
341     if year < 1970 {
342         day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day;
343 
344         while year < 1969 {
345             year += 1;
346             day += if leap_year(year) { 366 } else { 365 };
347         }
348 
349         while month < 11 {
350             month += 1;
351             day += MONTH_DAYS[leap][month as usize] as i64;
352         }
353 
354         (-(day * (60 * 60 * 24)
355             - (((*t).tm_hour as i64) * (60 * 60) + ((*t).tm_min as i64) * 60 + (*t).tm_sec as i64)))
356             as ::time_t
357     } else {
358         while year > 1970 {
359             year -= 1;
360             day += if leap_year(year) { 366 } else { 365 };
361         }
362 
363         while month > 0 {
364             month -= 1;
365             day += MONTH_DAYS[leap][month as usize] as i64;
366         }
367 
368         (day * (60 * 60 * 24)
369             + ((*t).tm_hour as i64) * (60 * 60)
370             + ((*t).tm_min as i64) * 60
371             + (*t).tm_sec as i64) as ::time_t
372     }
373 }
374 
375 // #[no_mangle]
376 // pub extern "C" fn nanosleep(rqtp: *const ::timespec, rmtp: *mut ::timespec) -> ::c_int {
377 //     platform::pal::nanosleep(rqtp, rmtp)
378 // }
379 
380 #[no_mangle]
381 pub unsafe extern "C" fn strftime(
382     s: *mut ::c_char,
383     maxsize: ::size_t,
384     format: *const ::c_char,
385     timeptr: *const tm,
386 ) -> ::size_t {
387     let ret = strftime::strftime(
388         &mut platform::StringWriter(s as *mut u8, maxsize),
389         format,
390         timeptr,
391     );
392     if ret < maxsize {
393         ret
394     } else {
395         0
396     }
397 }
398 
399 // #[no_mangle]
400 // pub extern "C" fn strptime(buf: *const ::c_char, format: *const ::c_char, tm: *mut tm) -> *mut ::c_char {
401 //     unimplemented!();
402 // }
403 
404 #[no_mangle]
405 pub unsafe extern "C" fn time(tloc: *mut ::time_t) -> ::time_t {
406     let mut ts = ::timespec::default();
407     platform::pal::clock_gettime(::CLOCK_REALTIME, &mut ts);
408     if !tloc.is_null() {
409         *tloc = ts.tv_sec
410     };
411     ts.tv_sec
412 }
413 
414 #[no_mangle]
415 pub unsafe extern "C" fn timelocal(tm: *mut tm) -> ::time_t {
416     //TODO: timezone
417     timegm(tm)
418 }
419 
420 #[no_mangle]
421 pub unsafe extern "C" fn timegm(tm: *mut tm) -> ::time_t {
422     let mut y = (*tm).tm_year as ::time_t + 1900;
423     let mut m = (*tm).tm_mon as ::time_t + 1;
424     if m <= 2 {
425         y -= 1;
426         m += 12;
427     }
428     let d = (*tm).tm_mday as ::time_t;
429     let h = (*tm).tm_hour as ::time_t;
430     let mi = (*tm).tm_min as ::time_t;
431     let s = (*tm).tm_sec as ::time_t;
432     (365 * y + y / 4 - y / 100 + y / 400 + 3 * (m + 1) / 5 + 30 * m + d - 719561) * 86400
433         + 3600 * h
434         + 60 * mi
435         + s
436 }
437 
438 // #[no_mangle]
439 pub extern "C" fn timer_create(
440     clock_id: ::clockid_t,
441     evp: *mut sigevent,
442     timerid: *mut ::timer_t,
443 ) -> ::c_int {
444     unimplemented!();
445 }
446 
447 // #[no_mangle]
448 pub extern "C" fn timer_delete(timerid: ::timer_t) -> ::c_int {
449     unimplemented!();
450 }
451 
452 // #[no_mangle]
453 pub extern "C" fn tzset() {
454     unimplemented!();
455 }
456 
457 // #[no_mangle]
458 pub extern "C" fn timer_settime(
459     timerid: ::timer_t,
460     flags: ::c_int,
461     value: *const itimerspec,
462     ovalue: *mut itimerspec,
463 ) -> ::c_int {
464     unimplemented!();
465 }
466 
467 // #[no_mangle]
468 pub extern "C" fn timer_gettime(timerid: ::timer_t, value: *mut itimerspec) -> ::c_int {
469     unimplemented!();
470 }
471 
472 // #[no_mangle]
473 pub extern "C" fn timer_getoverrun(timerid: ::timer_t) -> ::c_int {
474     unimplemented!();
475 }
476 
477 /*
478 #[no_mangle]
479 pub extern "C" fn func(args) -> ::c_int {
480     unimplemented!();
481 }
482 */
483