xref: /relibc/src/header/time/mod.rs (revision 928b18b3062e4fe797d6dd548823e9e65fc2d508)
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::{EIO, 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,
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         OUT_OF_RANGE_MESSAGE
128     );
129     assert!(0 <= tm_wday && tm_wday <= 6, OUT_OF_RANGE_MESSAGE);
130 
131     // At this point, we can safely use the values as given.
132     let write_result = core::fmt::write(
133         // buf may be either `*mut u8` or `*mut i8`
134         &mut platform::UnsafeStringWriter(buf.cast()),
135         format_args!(
136             "{:.3} {:.3}{:3} {:02}:{:02}:{:02} {}\n",
137             DAY_NAMES[usize::try_from(tm_wday).unwrap()],
138             MON_NAMES[usize::try_from(tm_mon).unwrap()],
139             tm_mday,
140             tm_hour,
141             tm_min,
142             tm_sec,
143             1900 + tm_year
144         ),
145     );
146     match write_result {
147         Ok(_) => buf,
148         Err(_) => {
149             /* asctime()/asctime_r() or the equivalent sprintf() call
150              * have no defined errno setting */
151             core::ptr::null_mut()
152         }
153     }
154 }
155 
156 #[no_mangle]
157 pub extern "C" fn clock() -> clock_t {
158     let mut ts = core::mem::MaybeUninit::<timespec>::uninit();
159 
160     if clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ts.as_mut_ptr()) != 0 {
161         return -1;
162     }
163     let ts = unsafe { ts.assume_init() };
164 
165     if ts.tv_sec > time_t::max_value() / CLOCKS_PER_SEC
166         || ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)
167             > time_t::max_value() - CLOCKS_PER_SEC * ts.tv_sec
168     {
169         return -1;
170     }
171 
172     ts.tv_sec * CLOCKS_PER_SEC + ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)
173 }
174 
175 // #[no_mangle]
176 pub extern "C" fn clock_getres(clock_id: clockid_t, res: *mut timespec) -> c_int {
177     unimplemented!();
178 }
179 
180 #[no_mangle]
181 pub extern "C" fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int {
182     Sys::clock_gettime(clock_id, tp)
183 }
184 
185 // #[no_mangle]
186 pub extern "C" fn clock_settime(clock_id: clockid_t, tp: *const timespec) -> c_int {
187     unimplemented!();
188 }
189 
190 #[no_mangle]
191 pub unsafe extern "C" fn ctime(clock: *const time_t) -> *mut c_char {
192     asctime(localtime(clock))
193 }
194 
195 #[no_mangle]
196 pub unsafe extern "C" fn ctime_r(clock: *const time_t, buf: *mut c_char) -> *mut c_char {
197     // Using MaybeUninit<tm> seems to cause a panic during the build process
198     let mut tm1 = tm {
199         tm_sec: 0,
200         tm_min: 0,
201         tm_hour: 0,
202         tm_mday: 0,
203         tm_mon: 0,
204         tm_year: 0,
205         tm_wday: 0,
206         tm_yday: 0,
207         tm_isdst: 0,
208         tm_gmtoff: 0,
209         tm_zone: core::ptr::null_mut(),
210     };
211     localtime_r(clock, &mut tm1);
212     asctime_r(&tm1, buf)
213 }
214 
215 #[no_mangle]
216 pub extern "C" fn difftime(time1: time_t, time0: time_t) -> c_double {
217     (time1 - time0) as c_double
218 }
219 
220 // #[no_mangle]
221 pub extern "C" fn getdate(string: *const c_char) -> tm {
222     unimplemented!();
223 }
224 
225 #[no_mangle]
226 pub unsafe extern "C" fn gmtime(timer: *const time_t) -> *mut tm {
227     gmtime_r(timer, &mut TM)
228 }
229 
230 const MONTH_DAYS: [[c_int; 12]; 2] = [
231     // Non-leap years:
232     [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
233     // Leap years:
234     [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
235 ];
236 
237 #[inline(always)]
238 fn leap_year(year: c_int) -> bool {
239     year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
240 }
241 
242 #[no_mangle]
243 pub unsafe extern "C" fn gmtime_r(clock: *const time_t, result: *mut tm) -> *mut tm {
244     /* For the details of the algorithm used here, see
245      * http://howardhinnant.github.io/date_algorithms.html#civil_from_days
246      * Note that we need 0-based months here, though.
247      * Overall, this implementation should generate correct results as
248      * long as the tm_year value will fit in a c_int. */
249     const SECS_PER_DAY: time_t = 24 * 60 * 60;
250     const DAYS_PER_ERA: time_t = 146097;
251 
252     let unix_secs = *clock;
253 
254     /* Day number here is possibly negative, remainder will always be
255      * nonnegative when using Euclidean division */
256     let unix_days: time_t = unix_secs.div_euclid(SECS_PER_DAY);
257 
258     /* In range [0, 86399]. Needs a u32 since this is larger (at least
259      * theoretically) than the guaranteed range of c_int */
260     let secs_of_day: u32 = unix_secs.rem_euclid(SECS_PER_DAY).try_into().unwrap();
261 
262     /* Shift origin from 1970-01-01 to 0000-03-01 and find out where we
263      * are in terms of 400-year eras since then */
264     let days_since_origin = unix_days + 719468;
265     let era = days_since_origin.div_euclid(DAYS_PER_ERA);
266     let day_of_era = days_since_origin.rem_euclid(DAYS_PER_ERA);
267     let year_of_era =
268         (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / 365;
269 
270     /* "transformed" here refers to dates in a calendar where years
271      * start on March 1 */
272     let year_transformed = year_of_era + 400 * era; // retain large range, don't convert to c_int yet
273     let day_of_year_transformed: c_int = (day_of_era
274         - (365 * year_of_era + year_of_era / 4 - year_of_era / 100))
275         .try_into()
276         .unwrap();
277     let month_transformed: c_int = (5 * day_of_year_transformed + 2) / 153;
278 
279     // Convert back to calendar with year starting on January 1
280     let month: c_int = (month_transformed + 2) % 12; // adapted to 0-based months
281     let year: time_t = if month < 2 {
282         year_transformed + 1
283     } else {
284         year_transformed
285     };
286 
287     /* Subtract 1900 *before* converting down to c_int in order to
288      * maximize the range of input timestamps that will succeed */
289     match c_int::try_from(year - 1900) {
290         Ok(year_less_1900) => {
291             let mday: c_int = (day_of_year_transformed - (153 * month_transformed + 2) / 5 + 1)
292                 .try_into()
293                 .unwrap();
294 
295             /* 1970-01-01 was a Thursday. Again, Euclidean division is
296              * used to ensure a nonnegative remainder (range [0, 6]). */
297             let wday: c_int = ((unix_days + 4).rem_euclid(7)).try_into().unwrap();
298 
299             /* Yes, duplicated code for now (to work on non-c_int-values
300              * so that we are not constrained by the subtraction of
301              * 1900) */
302             let is_leap_year: bool = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
303 
304             /* For dates that are March 1 or later, we can use day-of-
305              * year in the transformed calendar. For January and
306              * February, that value is sensitive to whether the previous
307              * year is a leap year. Therefore, we use the already
308              * computed date for those two months. */
309             let yday: c_int = match month {
310                 0 => mday - 1,      // January
311                 1 => 31 + mday - 1, // February
312                 _ => day_of_year_transformed + if is_leap_year { 60 } else { 59 },
313             };
314 
315             let hour: c_int = (secs_of_day / (60 * 60)).try_into().unwrap();
316             let min: c_int = ((secs_of_day / 60) % 60).try_into().unwrap();
317             let sec: c_int = (secs_of_day % 60).try_into().unwrap();
318 
319             *result = tm {
320                 tm_sec: sec,
321                 tm_min: min,
322                 tm_hour: hour,
323                 tm_mday: mday,
324                 tm_mon: month,
325                 tm_year: year_less_1900,
326                 tm_wday: wday,
327                 tm_yday: yday,
328                 tm_isdst: 0,
329                 tm_gmtoff: 0,
330                 tm_zone: UTC,
331             };
332 
333             result
334         }
335         Err(_) => {
336             platform::errno = EOVERFLOW;
337             core::ptr::null_mut()
338         }
339     }
340 }
341 
342 #[no_mangle]
343 pub unsafe extern "C" fn localtime(clock: *const time_t) -> *mut tm {
344     localtime_r(clock, &mut TM)
345 }
346 
347 #[no_mangle]
348 pub unsafe extern "C" fn localtime_r(clock: *const time_t, t: *mut tm) -> *mut tm {
349     // TODO: Change tm_isdst, tm_gmtoff, tm_zone
350     gmtime_r(clock, t)
351 }
352 
353 #[no_mangle]
354 pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
355     let mut year = (*t).tm_year + 1900;
356     let mut month = (*t).tm_mon;
357     let mut day = (*t).tm_mday as i64 - 1;
358 
359     let leap = if leap_year(year) { 1 } else { 0 };
360 
361     if year < 1970 {
362         day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day;
363 
364         while year < 1969 {
365             year += 1;
366             day += if leap_year(year) { 366 } else { 365 };
367         }
368 
369         while month < 11 {
370             month += 1;
371             day += MONTH_DAYS[leap][month as usize] as i64;
372         }
373 
374         -(day * (60 * 60 * 24)
375             - (((*t).tm_hour as i64) * (60 * 60) + ((*t).tm_min as i64) * 60 + (*t).tm_sec as i64))
376     } else {
377         while year > 1970 {
378             year -= 1;
379             day += if leap_year(year) { 366 } else { 365 };
380         }
381 
382         while month > 0 {
383             month -= 1;
384             day += MONTH_DAYS[leap][month as usize] as i64;
385         }
386 
387         (day * (60 * 60 * 24)
388             + ((*t).tm_hour as i64) * (60 * 60)
389             + ((*t).tm_min as i64) * 60
390             + (*t).tm_sec as i64)
391     }
392 }
393 
394 #[no_mangle]
395 pub extern "C" fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
396     Sys::nanosleep(rqtp, rmtp)
397 }
398 
399 #[no_mangle]
400 pub unsafe extern "C" fn strftime(
401     s: *mut c_char,
402     maxsize: size_t,
403     format: *const c_char,
404     timeptr: *const tm,
405 ) -> size_t {
406     let ret = strftime::strftime(
407         &mut platform::StringWriter(s as *mut u8, maxsize),
408         format,
409         timeptr,
410     );
411     if ret < maxsize {
412         ret
413     } else {
414         0
415     }
416 }
417 
418 // #[no_mangle]
419 pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut tm) -> *mut c_char {
420     unimplemented!();
421 }
422 
423 #[no_mangle]
424 pub unsafe extern "C" fn time(tloc: *mut time_t) -> time_t {
425     let mut ts = timespec::default();
426     Sys::clock_gettime(CLOCK_REALTIME, &mut ts);
427     if !tloc.is_null() {
428         *tloc = ts.tv_sec
429     };
430     ts.tv_sec
431 }
432 
433 #[no_mangle]
434 pub unsafe extern "C" fn timelocal(tm: *mut tm) -> time_t {
435     //TODO: timezone
436     timegm(tm)
437 }
438 
439 #[no_mangle]
440 pub unsafe extern "C" fn timegm(tm: *mut tm) -> time_t {
441     let mut y = (*tm).tm_year as time_t + 1900;
442     let mut m = (*tm).tm_mon as time_t + 1;
443     if m <= 2 {
444         y -= 1;
445         m += 12;
446     }
447     let d = (*tm).tm_mday as time_t;
448     let h = (*tm).tm_hour as time_t;
449     let mi = (*tm).tm_min as time_t;
450     let s = (*tm).tm_sec as time_t;
451     (365 * y + y / 4 - y / 100 + y / 400 + 3 * (m + 1) / 5 + 30 * m + d - 719561) * 86400
452         + 3600 * h
453         + 60 * mi
454         + s
455 }
456 
457 // #[no_mangle]
458 pub extern "C" fn timer_create(
459     clock_id: clockid_t,
460     evp: *mut sigevent,
461     timerid: *mut timer_t,
462 ) -> c_int {
463     unimplemented!();
464 }
465 
466 // #[no_mangle]
467 pub extern "C" fn timer_delete(timerid: timer_t) -> c_int {
468     unimplemented!();
469 }
470 
471 // #[no_mangle]
472 pub extern "C" fn tzset() {
473     unimplemented!();
474 }
475 
476 // #[no_mangle]
477 pub extern "C" fn timer_settime(
478     timerid: timer_t,
479     flags: c_int,
480     value: *const itimerspec,
481     ovalue: *mut itimerspec,
482 ) -> c_int {
483     unimplemented!();
484 }
485 
486 // #[no_mangle]
487 pub extern "C" fn timer_gettime(timerid: timer_t, value: *mut itimerspec) -> c_int {
488     unimplemented!();
489 }
490 
491 // #[no_mangle]
492 pub extern "C" fn timer_getoverrun(timerid: timer_t) -> c_int {
493     unimplemented!();
494 }
495 
496 /*
497 #[no_mangle]
498 pub extern "C" fn func(args) -> c_int {
499     unimplemented!();
500 }
501 */
502