2
0

mod.rs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //! time implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
  2. use crate::{
  3. header::errno::EIO,
  4. platform::{self, types::*, Pal, Sys},
  5. };
  6. use self::constants::*;
  7. pub mod constants;
  8. mod strftime;
  9. #[repr(C)]
  10. #[derive(Default)]
  11. pub struct timespec {
  12. pub tv_sec: time_t,
  13. pub tv_nsec: c_long,
  14. }
  15. #[cfg(target_os = "redox")]
  16. impl<'a> From<&'a timespec> for syscall::TimeSpec {
  17. fn from(tp: &timespec) -> Self {
  18. Self {
  19. tv_sec: tp.tv_sec,
  20. tv_nsec: tp.tv_nsec as i32,
  21. }
  22. }
  23. }
  24. #[repr(C)]
  25. pub struct tm {
  26. pub tm_sec: c_int,
  27. pub tm_min: c_int,
  28. pub tm_hour: c_int,
  29. pub tm_mday: c_int,
  30. pub tm_mon: c_int,
  31. pub tm_year: c_int,
  32. pub tm_wday: c_int,
  33. pub tm_yday: c_int,
  34. pub tm_isdst: c_int,
  35. pub tm_gmtoff: c_long,
  36. pub tm_zone: *const c_char,
  37. }
  38. unsafe impl Sync for tm {}
  39. // The C Standard says that localtime and gmtime return the same pointer.
  40. static mut TM: tm = tm {
  41. tm_sec: 0,
  42. tm_min: 0,
  43. tm_hour: 0,
  44. tm_mday: 0,
  45. tm_mon: 0,
  46. tm_year: 0,
  47. tm_wday: 0,
  48. tm_yday: 0,
  49. tm_isdst: 0,
  50. tm_gmtoff: 0,
  51. tm_zone: UTC,
  52. };
  53. // The C Standard says that ctime and asctime return the same pointer.
  54. static mut ASCTIME: [c_char; 26] = [0; 26];
  55. #[repr(C)]
  56. pub struct itimerspec {
  57. pub it_interval: timespec,
  58. pub it_value: timespec,
  59. }
  60. pub struct sigevent;
  61. #[no_mangle]
  62. pub unsafe extern "C" fn asctime(timeptr: *const tm) -> *mut c_char {
  63. asctime_r(timeptr, &mut ASCTIME as *mut [i8; 26] as *mut i8)
  64. }
  65. #[no_mangle]
  66. pub unsafe extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_char {
  67. let tm = &*tm;
  68. let result = core::fmt::write(
  69. &mut platform::UnsafeStringWriter(buf as *mut u8),
  70. format_args!(
  71. "{:.3} {:.3}{:3} {:02}:{:02}:{:02} {}\n",
  72. DAY_NAMES[tm.tm_wday as usize],
  73. MON_NAMES[tm.tm_mon as usize],
  74. tm.tm_mday as usize,
  75. tm.tm_hour as usize,
  76. tm.tm_min as usize,
  77. tm.tm_sec as usize,
  78. (1900 + tm.tm_year)
  79. ),
  80. );
  81. match result {
  82. Ok(_) => buf,
  83. Err(_) => {
  84. platform::errno = EIO;
  85. core::ptr::null_mut()
  86. }
  87. }
  88. }
  89. #[no_mangle]
  90. pub extern "C" fn clock() -> clock_t {
  91. let mut ts = core::mem::MaybeUninit::<timespec>::uninit();
  92. if clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ts.as_mut_ptr()) != 0 {
  93. return -1;
  94. }
  95. let ts = unsafe { ts.assume_init() };
  96. if ts.tv_sec > time_t::max_value() / CLOCKS_PER_SEC
  97. || ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)
  98. > time_t::max_value() - CLOCKS_PER_SEC * ts.tv_sec
  99. {
  100. return -1;
  101. }
  102. ts.tv_sec * CLOCKS_PER_SEC + ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)
  103. }
  104. // #[no_mangle]
  105. pub extern "C" fn clock_getres(clock_id: clockid_t, res: *mut timespec) -> c_int {
  106. unimplemented!();
  107. }
  108. #[no_mangle]
  109. pub extern "C" fn clock_gettime(clock_id: clockid_t, tp: *mut timespec) -> c_int {
  110. Sys::clock_gettime(clock_id, tp)
  111. }
  112. // #[no_mangle]
  113. pub extern "C" fn clock_settime(clock_id: clockid_t, tp: *const timespec) -> c_int {
  114. unimplemented!();
  115. }
  116. #[no_mangle]
  117. pub unsafe extern "C" fn ctime(clock: *const time_t) -> *mut c_char {
  118. asctime(localtime(clock))
  119. }
  120. #[no_mangle]
  121. pub unsafe extern "C" fn ctime_r(clock: *const time_t, buf: *mut c_char) -> *mut c_char {
  122. let mut tm1: tm = core::mem::uninitialized();
  123. localtime_r(clock, &mut tm1);
  124. asctime_r(&mut tm1, buf)
  125. }
  126. #[no_mangle]
  127. pub extern "C" fn difftime(time1: time_t, time0: time_t) -> c_double {
  128. (time1 - time0) as c_double
  129. }
  130. // #[no_mangle]
  131. pub extern "C" fn getdate(string: *const c_char) -> tm {
  132. unimplemented!();
  133. }
  134. #[no_mangle]
  135. pub unsafe extern "C" fn gmtime(timer: *const time_t) -> *mut tm {
  136. gmtime_r(timer, &mut TM)
  137. }
  138. const MONTH_DAYS: [[c_int; 12]; 2] = [
  139. // Non-leap years:
  140. [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  141. // Leap years:
  142. [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  143. ];
  144. #[inline(always)]
  145. fn leap_year(year: c_int) -> bool {
  146. year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
  147. }
  148. #[no_mangle]
  149. pub unsafe extern "C" fn gmtime_r(clock: *const time_t, t: *mut tm) -> *mut tm {
  150. let clock = *clock;
  151. let mut day = (clock / (60 * 60 * 24)) as c_int;
  152. if clock < 0 && clock % (60 * 60 * 24) != 0 {
  153. // -1 because for negative values round upwards
  154. // -0.3 == 0, but we want -1
  155. day -= 1;
  156. }
  157. (*t).tm_sec = (clock % 60) as c_int;
  158. (*t).tm_min = ((clock / 60) % 60) as c_int;
  159. (*t).tm_hour = ((clock / (60 * 60)) % 24) as c_int;
  160. while (*t).tm_sec < 0 {
  161. (*t).tm_sec += 60;
  162. (*t).tm_min -= 1;
  163. }
  164. while (*t).tm_min < 0 {
  165. (*t).tm_min += 60;
  166. (*t).tm_hour -= 1;
  167. }
  168. while (*t).tm_hour < 0 {
  169. (*t).tm_hour += 24;
  170. }
  171. // Jan 1th was a thursday, 4th of a zero-indexed week.
  172. (*t).tm_wday = (day + 4) % 7;
  173. if (*t).tm_wday < 0 {
  174. (*t).tm_wday += 7;
  175. }
  176. let mut year = 1970;
  177. if day < 0 {
  178. while day < 0 {
  179. let days_in_year = if leap_year(year) { 366 } else { 365 };
  180. day += days_in_year;
  181. year -= 1;
  182. }
  183. (*t).tm_year = year - 1900;
  184. (*t).tm_yday = day + 1;
  185. } else {
  186. loop {
  187. let days_in_year = if leap_year(year) { 366 } else { 365 };
  188. if day < days_in_year {
  189. break;
  190. }
  191. day -= days_in_year;
  192. year += 1;
  193. }
  194. (*t).tm_year = year - 1900;
  195. (*t).tm_yday = day;
  196. }
  197. let leap = if leap_year(year) { 1 } else { 0 };
  198. (*t).tm_mon = 0;
  199. loop {
  200. let days_in_month = MONTH_DAYS[leap][(*t).tm_mon as usize];
  201. if day < days_in_month {
  202. break;
  203. }
  204. day -= days_in_month;
  205. (*t).tm_mon += 1;
  206. }
  207. (*t).tm_mday = 1 + day as c_int;
  208. (*t).tm_isdst = 0;
  209. (*t).tm_gmtoff = 0;
  210. (*t).tm_zone = UTC;
  211. t
  212. }
  213. #[no_mangle]
  214. pub unsafe extern "C" fn localtime(clock: *const time_t) -> *mut tm {
  215. localtime_r(clock, &mut TM)
  216. }
  217. #[no_mangle]
  218. pub unsafe extern "C" fn localtime_r(clock: *const time_t, t: *mut tm) -> *mut tm {
  219. // TODO: Change tm_isdst, tm_gmtoff, tm_zone
  220. gmtime_r(clock, t)
  221. }
  222. #[no_mangle]
  223. pub unsafe extern "C" fn mktime(t: *mut tm) -> time_t {
  224. let mut year = (*t).tm_year + 1900;
  225. let mut month = (*t).tm_mon;
  226. let mut day = (*t).tm_mday as i64 - 1;
  227. let leap = if leap_year(year) { 1 } else { 0 };
  228. if year < 1970 {
  229. day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day;
  230. while year < 1969 {
  231. year += 1;
  232. day += if leap_year(year) { 366 } else { 365 };
  233. }
  234. while month < 11 {
  235. month += 1;
  236. day += MONTH_DAYS[leap][month as usize] as i64;
  237. }
  238. -(day * (60 * 60 * 24)
  239. - (((*t).tm_hour as i64) * (60 * 60) + ((*t).tm_min as i64) * 60 + (*t).tm_sec as i64))
  240. } else {
  241. while year > 1970 {
  242. year -= 1;
  243. day += if leap_year(year) { 366 } else { 365 };
  244. }
  245. while month > 0 {
  246. month -= 1;
  247. day += MONTH_DAYS[leap][month as usize] as i64;
  248. }
  249. (day * (60 * 60 * 24)
  250. + ((*t).tm_hour as i64) * (60 * 60)
  251. + ((*t).tm_min as i64) * 60
  252. + (*t).tm_sec as i64)
  253. }
  254. }
  255. #[no_mangle]
  256. pub extern "C" fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
  257. Sys::nanosleep(rqtp, rmtp)
  258. }
  259. #[no_mangle]
  260. pub unsafe extern "C" fn strftime(
  261. s: *mut c_char,
  262. maxsize: size_t,
  263. format: *const c_char,
  264. timeptr: *const tm,
  265. ) -> size_t {
  266. let ret = strftime::strftime(
  267. &mut platform::StringWriter(s as *mut u8, maxsize),
  268. format,
  269. timeptr,
  270. );
  271. if ret < maxsize {
  272. ret
  273. } else {
  274. 0
  275. }
  276. }
  277. // #[no_mangle]
  278. pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut tm) -> *mut c_char {
  279. unimplemented!();
  280. }
  281. #[no_mangle]
  282. pub unsafe extern "C" fn time(tloc: *mut time_t) -> time_t {
  283. let mut ts = timespec::default();
  284. Sys::clock_gettime(CLOCK_REALTIME, &mut ts);
  285. if !tloc.is_null() {
  286. *tloc = ts.tv_sec
  287. };
  288. ts.tv_sec
  289. }
  290. #[no_mangle]
  291. pub unsafe extern "C" fn timelocal(tm: *mut tm) -> time_t {
  292. //TODO: timezone
  293. timegm(tm)
  294. }
  295. #[no_mangle]
  296. pub unsafe extern "C" fn timegm(tm: *mut tm) -> time_t {
  297. let mut y = (*tm).tm_year as time_t + 1900;
  298. let mut m = (*tm).tm_mon as time_t + 1;
  299. if m <= 2 {
  300. y -= 1;
  301. m += 12;
  302. }
  303. let d = (*tm).tm_mday as time_t;
  304. let h = (*tm).tm_hour as time_t;
  305. let mi = (*tm).tm_min as time_t;
  306. let s = (*tm).tm_sec as time_t;
  307. (365 * y + y / 4 - y / 100 + y / 400 + 3 * (m + 1) / 5 + 30 * m + d - 719561) * 86400
  308. + 3600 * h
  309. + 60 * mi
  310. + s
  311. }
  312. // #[no_mangle]
  313. pub extern "C" fn timer_create(
  314. clock_id: clockid_t,
  315. evp: *mut sigevent,
  316. timerid: *mut timer_t,
  317. ) -> c_int {
  318. unimplemented!();
  319. }
  320. // #[no_mangle]
  321. pub extern "C" fn timer_delete(timerid: timer_t) -> c_int {
  322. unimplemented!();
  323. }
  324. // #[no_mangle]
  325. pub extern "C" fn tzset() {
  326. unimplemented!();
  327. }
  328. // #[no_mangle]
  329. pub extern "C" fn timer_settime(
  330. timerid: timer_t,
  331. flags: c_int,
  332. value: *const itimerspec,
  333. ovalue: *mut itimerspec,
  334. ) -> c_int {
  335. unimplemented!();
  336. }
  337. // #[no_mangle]
  338. pub extern "C" fn timer_gettime(timerid: timer_t, value: *mut itimerspec) -> c_int {
  339. unimplemented!();
  340. }
  341. // #[no_mangle]
  342. pub extern "C" fn timer_getoverrun(timerid: timer_t) -> c_int {
  343. unimplemented!();
  344. }
  345. /*
  346. #[no_mangle]
  347. pub extern "C" fn func(args) -> c_int {
  348. unimplemented!();
  349. }
  350. */