mod.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. //! time implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
  2. use core::convert::{TryFrom, TryInto};
  3. use crate::{
  4. header::errno::{EIO, EOVERFLOW},
  5. platform::{self, types::*, Pal, Sys},
  6. };
  7. pub use self::constants::*;
  8. pub mod constants;
  9. mod strftime;
  10. #[repr(C)]
  11. #[derive(Default)]
  12. pub struct timespec {
  13. pub tv_sec: time_t,
  14. pub tv_nsec: c_long,
  15. }
  16. #[cfg(target_os = "redox")]
  17. impl<'a> From<&'a timespec> for syscall::TimeSpec {
  18. fn from(tp: &timespec) -> Self {
  19. Self {
  20. tv_sec: tp.tv_sec,
  21. tv_nsec: tp.tv_nsec as i32,
  22. }
  23. }
  24. }
  25. #[repr(C)]
  26. pub struct tm {
  27. pub tm_sec: c_int,
  28. pub tm_min: c_int,
  29. pub tm_hour: c_int,
  30. pub tm_mday: c_int,
  31. pub tm_mon: c_int,
  32. pub tm_year: c_int,
  33. pub tm_wday: c_int,
  34. pub tm_yday: c_int,
  35. pub tm_isdst: c_int,
  36. pub tm_gmtoff: c_long,
  37. pub tm_zone: *const c_char,
  38. }
  39. unsafe impl Sync for tm {}
  40. // The C Standard says that localtime and gmtime return the same pointer.
  41. static mut TM: tm = tm {
  42. tm_sec: 0,
  43. tm_min: 0,
  44. tm_hour: 0,
  45. tm_mday: 0,
  46. tm_mon: 0,
  47. tm_year: 0,
  48. tm_wday: 0,
  49. tm_yday: 0,
  50. tm_isdst: 0,
  51. tm_gmtoff: 0,
  52. tm_zone: UTC,
  53. };
  54. // The C Standard says that ctime and asctime return the same pointer.
  55. static mut ASCTIME: [c_char; 26] = [0; 26];
  56. #[repr(C)]
  57. pub struct itimerspec {
  58. pub it_interval: timespec,
  59. pub it_value: timespec,
  60. }
  61. pub struct sigevent;
  62. #[no_mangle]
  63. pub unsafe extern "C" fn asctime(timeptr: *const tm) -> *mut c_char {
  64. asctime_r(timeptr, &mut ASCTIME as *mut [i8; 26] as *mut i8)
  65. }
  66. #[no_mangle]
  67. pub unsafe extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_char {
  68. let tm = &*tm;
  69. let result = core::fmt::write(
  70. &mut platform::UnsafeStringWriter(buf as *mut u8),
  71. format_args!(
  72. "{:.3} {:.3}{:3} {:02}:{:02}:{:02} {}\n",
  73. DAY_NAMES[tm.tm_wday as usize],
  74. MON_NAMES[tm.tm_mon as usize],
  75. tm.tm_mday as usize,
  76. tm.tm_hour as usize,
  77. tm.tm_min as usize,
  78. tm.tm_sec as usize,
  79. (1900 + tm.tm_year)
  80. ),
  81. );
  82. match result {
  83. Ok(_) => buf,
  84. Err(_) => {
  85. platform::errno = EIO;
  86. core::ptr::null_mut()
  87. }
  88. }
  89. }
  90. #[no_mangle]
  91. pub extern "C" fn clock() -> clock_t {
  92. let mut ts = core::mem::MaybeUninit::<timespec>::uninit();
  93. if clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ts.as_mut_ptr()) != 0 {
  94. return -1;
  95. }
  96. let ts = unsafe { ts.assume_init() };
  97. if ts.tv_sec > time_t::max_value() / CLOCKS_PER_SEC
  98. || ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)
  99. > time_t::max_value() - CLOCKS_PER_SEC * ts.tv_sec
  100. {
  101. return -1;
  102. }
  103. ts.tv_sec * CLOCKS_PER_SEC + ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)
  104. }
  105. // #[no_mangle]
  106. pub extern "C" fn clock_getres(clock_id: clockid_t, res: *mut timespec) -> c_int {
  107. unimplemented!();
  108. }
  109. #[no_mangle]
  110. pub extern "C" fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int {
  111. Sys::clock_gettime(clock_id, tp)
  112. }
  113. // #[no_mangle]
  114. pub extern "C" fn clock_settime(clock_id: clockid_t, tp: *const timespec) -> c_int {
  115. unimplemented!();
  116. }
  117. #[no_mangle]
  118. pub unsafe extern "C" fn ctime(clock: *const time_t) -> *mut c_char {
  119. asctime(localtime(clock))
  120. }
  121. #[no_mangle]
  122. pub unsafe extern "C" fn ctime_r(clock: *const time_t, buf: *mut c_char) -> *mut c_char {
  123. // Using MaybeUninit<tm> seems to cause a panic during the build process
  124. let mut tm1 = tm {
  125. tm_sec: 0,
  126. tm_min: 0,
  127. tm_hour: 0,
  128. tm_mday: 0,
  129. tm_mon: 0,
  130. tm_year: 0,
  131. tm_wday: 0,
  132. tm_yday: 0,
  133. tm_isdst: 0,
  134. tm_gmtoff: 0,
  135. tm_zone: core::ptr::null_mut(),
  136. };
  137. localtime_r(clock, &mut tm1);
  138. asctime_r(&tm1, buf)
  139. }
  140. #[no_mangle]
  141. pub extern "C" fn difftime(time1: time_t, time0: time_t) -> c_double {
  142. (time1 - time0) as c_double
  143. }
  144. // #[no_mangle]
  145. pub extern "C" fn getdate(string: *const c_char) -> tm {
  146. unimplemented!();
  147. }
  148. #[no_mangle]
  149. pub unsafe extern "C" fn gmtime(timer: *const time_t) -> *mut tm {
  150. gmtime_r(timer, &mut TM)
  151. }
  152. const MONTH_DAYS: [[c_int; 12]; 2] = [
  153. // Non-leap years:
  154. [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  155. // Leap years:
  156. [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  157. ];
  158. #[inline(always)]
  159. fn leap_year(year: c_int) -> bool {
  160. year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
  161. }
  162. #[no_mangle]
  163. pub unsafe extern "C" fn gmtime_r(clock: *const time_t, result: *mut tm) -> *mut tm {
  164. /* For the details of the algorithm used here, see
  165. * http://howardhinnant.github.io/date_algorithms.html#civil_from_days
  166. * Note that we need 0-based months here, though.
  167. * Overall, this implementation should generate correct results as
  168. * long as the tm_year value will fit in a c_int. */
  169. const SECS_PER_DAY: time_t = 24 * 60 * 60;
  170. const DAYS_PER_ERA: time_t = 146097;
  171. let unix_secs = *clock;
  172. /* Day number here is possibly negative, remainder will always be
  173. * nonnegative when using Euclidean division */
  174. let unix_days: time_t = unix_secs.div_euclid(SECS_PER_DAY);
  175. /* In range [0, 86399]. Needs a u32 since this is larger (at least
  176. * theoretically) than the guaranteed range of c_int */
  177. let secs_of_day: u32 = unix_secs.rem_euclid(SECS_PER_DAY).try_into().unwrap();
  178. /* Shift origin from 1970-01-01 to 0000-03-01 and find out where we
  179. * are in terms of 400-year eras since then */
  180. let days_since_origin = unix_days + 719468;
  181. let era = days_since_origin.div_euclid(DAYS_PER_ERA);
  182. let day_of_era = days_since_origin.rem_euclid(DAYS_PER_ERA);
  183. let year_of_era =
  184. (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / 365;
  185. /* "transformed" here refers to dates in a calendar where years
  186. * start on March 1 */
  187. let year_transformed = year_of_era + 400 * era; // retain large range, don't convert to c_int yet
  188. let day_of_year_transformed: c_int = (day_of_era
  189. - (365 * year_of_era + year_of_era / 4 - year_of_era / 100))
  190. .try_into()
  191. .unwrap();
  192. let month_transformed: c_int = (5 * day_of_year_transformed + 2) / 153;
  193. // Convert back to calendar with year starting on January 1
  194. let month: c_int = (month_transformed + 2) % 12; // adapted to 0-based months
  195. let year: time_t = if month < 2 {
  196. year_transformed + 1
  197. } else {
  198. year_transformed
  199. };
  200. /* Subtract 1900 *before* converting down to c_int in order to
  201. * maximize the range of input timestamps that will succeed */
  202. match c_int::try_from(year - 1900) {
  203. Ok(year_less_1900) => {
  204. let mday: c_int = (day_of_year_transformed - (153 * month_transformed + 2) / 5 + 1)
  205. .try_into()
  206. .unwrap();
  207. /* 1970-01-01 was a Thursday. Again, Euclidean division is
  208. * used to ensure a nonnegative remainder (range [0, 6]). */
  209. let wday: c_int = ((unix_days + 4).rem_euclid(7)).try_into().unwrap();
  210. /* Yes, duplicated code for now (to work on non-c_int-values
  211. * so that we are not constrained by the subtraction of
  212. * 1900) */
  213. let is_leap_year: bool = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  214. /* For dates that are March 1 or later, we can use day-of-
  215. * year in the transformed calendar. For January and
  216. * February, that value is sensitive to whether the previous
  217. * year is a leap year. Therefore, we use the already
  218. * computed date for those two months. */
  219. let yday: c_int = match month {
  220. 0 => mday - 1, // January
  221. 1 => 31 + mday - 1, // February
  222. _ => day_of_year_transformed + if is_leap_year { 60 } else { 59 },
  223. };
  224. let hour: c_int = (secs_of_day / (60 * 60)).try_into().unwrap();
  225. let min: c_int = ((secs_of_day / 60) % 60).try_into().unwrap();
  226. let sec: c_int = (secs_of_day % 60).try_into().unwrap();
  227. *result = tm {
  228. tm_sec: sec,
  229. tm_min: min,
  230. tm_hour: hour,
  231. tm_mday: mday,
  232. tm_mon: month,
  233. tm_year: year_less_1900,
  234. tm_wday: wday,
  235. tm_yday: yday,
  236. tm_isdst: 0,
  237. tm_gmtoff: 0,
  238. tm_zone: UTC,
  239. };
  240. result
  241. }
  242. Err(_) => {
  243. platform::errno = EOVERFLOW;
  244. core::ptr::null_mut()
  245. }
  246. }
  247. }
  248. #[no_mangle]
  249. pub unsafe extern "C" fn localtime(clock: *const time_t) -> *mut tm {
  250. localtime_r(clock, &mut TM)
  251. }
  252. #[no_mangle]
  253. pub unsafe extern "C" fn localtime_r(clock: *const time_t, t: *mut tm) -> *mut tm {
  254. // TODO: Change tm_isdst, tm_gmtoff, tm_zone
  255. gmtime_r(clock, t)
  256. }
  257. #[no_mangle]
  258. pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
  259. let mut year = (*t).tm_year + 1900;
  260. let mut month = (*t).tm_mon;
  261. let mut day = (*t).tm_mday as i64 - 1;
  262. let leap = if leap_year(year) { 1 } else { 0 };
  263. if year < 1970 {
  264. day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day;
  265. while year < 1969 {
  266. year += 1;
  267. day += if leap_year(year) { 366 } else { 365 };
  268. }
  269. while month < 11 {
  270. month += 1;
  271. day += MONTH_DAYS[leap][month as usize] as i64;
  272. }
  273. -(day * (60 * 60 * 24)
  274. - (((*t).tm_hour as i64) * (60 * 60) + ((*t).tm_min as i64) * 60 + (*t).tm_sec as i64))
  275. } else {
  276. while year > 1970 {
  277. year -= 1;
  278. day += if leap_year(year) { 366 } else { 365 };
  279. }
  280. while month > 0 {
  281. month -= 1;
  282. day += MONTH_DAYS[leap][month as usize] as i64;
  283. }
  284. (day * (60 * 60 * 24)
  285. + ((*t).tm_hour as i64) * (60 * 60)
  286. + ((*t).tm_min as i64) * 60
  287. + (*t).tm_sec as i64)
  288. }
  289. }
  290. #[no_mangle]
  291. pub extern "C" fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
  292. Sys::nanosleep(rqtp, rmtp)
  293. }
  294. #[no_mangle]
  295. pub unsafe extern "C" fn strftime(
  296. s: *mut c_char,
  297. maxsize: size_t,
  298. format: *const c_char,
  299. timeptr: *const tm,
  300. ) -> size_t {
  301. let ret = strftime::strftime(
  302. &mut platform::StringWriter(s as *mut u8, maxsize),
  303. format,
  304. timeptr,
  305. );
  306. if ret < maxsize {
  307. ret
  308. } else {
  309. 0
  310. }
  311. }
  312. // #[no_mangle]
  313. pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut tm) -> *mut c_char {
  314. unimplemented!();
  315. }
  316. #[no_mangle]
  317. pub unsafe extern "C" fn time(tloc: *mut time_t) -> time_t {
  318. let mut ts = timespec::default();
  319. Sys::clock_gettime(CLOCK_REALTIME, &mut ts);
  320. if !tloc.is_null() {
  321. *tloc = ts.tv_sec
  322. };
  323. ts.tv_sec
  324. }
  325. #[no_mangle]
  326. pub unsafe extern "C" fn timelocal(tm: *mut tm) -> time_t {
  327. //TODO: timezone
  328. timegm(tm)
  329. }
  330. #[no_mangle]
  331. pub unsafe extern "C" fn timegm(tm: *mut tm) -> time_t {
  332. let mut y = (*tm).tm_year as time_t + 1900;
  333. let mut m = (*tm).tm_mon as time_t + 1;
  334. if m <= 2 {
  335. y -= 1;
  336. m += 12;
  337. }
  338. let d = (*tm).tm_mday as time_t;
  339. let h = (*tm).tm_hour as time_t;
  340. let mi = (*tm).tm_min as time_t;
  341. let s = (*tm).tm_sec as time_t;
  342. (365 * y + y / 4 - y / 100 + y / 400 + 3 * (m + 1) / 5 + 30 * m + d - 719561) * 86400
  343. + 3600 * h
  344. + 60 * mi
  345. + s
  346. }
  347. // #[no_mangle]
  348. pub extern "C" fn timer_create(
  349. clock_id: clockid_t,
  350. evp: *mut sigevent,
  351. timerid: *mut timer_t,
  352. ) -> c_int {
  353. unimplemented!();
  354. }
  355. // #[no_mangle]
  356. pub extern "C" fn timer_delete(timerid: timer_t) -> c_int {
  357. unimplemented!();
  358. }
  359. // #[no_mangle]
  360. pub extern "C" fn tzset() {
  361. unimplemented!();
  362. }
  363. // #[no_mangle]
  364. pub extern "C" fn timer_settime(
  365. timerid: timer_t,
  366. flags: c_int,
  367. value: *const itimerspec,
  368. ovalue: *mut itimerspec,
  369. ) -> c_int {
  370. unimplemented!();
  371. }
  372. // #[no_mangle]
  373. pub extern "C" fn timer_gettime(timerid: timer_t, value: *mut itimerspec) -> c_int {
  374. unimplemented!();
  375. }
  376. // #[no_mangle]
  377. pub extern "C" fn timer_getoverrun(timerid: timer_t) -> c_int {
  378. unimplemented!();
  379. }
  380. /*
  381. #[no_mangle]
  382. pub extern "C" fn func(args) -> c_int {
  383. unimplemented!();
  384. }
  385. */