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