2019-06-23 04:21:30 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
module time
|
|
|
|
|
2019-06-27 12:02:47 +02:00
|
|
|
// TODO: nanosecond and monotonic
|
|
|
|
pub fn now() Time {
|
|
|
|
# time_t t = time(0);
|
|
|
|
|
|
|
|
res := Time{}
|
|
|
|
# res.sec = t;
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sleep(t Time) {
|
|
|
|
if t.sec > i64(0) || t.nsec > i32(0) {
|
|
|
|
C.Sleep(i64(1000)*t.sec+(i64(t.nsec)+i64(999999))/i64(1000000))
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 12:02:47 +02:00
|
|
|
// TODO: Thread safety
|
|
|
|
fn (t Time) local() Info {
|
|
|
|
info := Info{}
|
|
|
|
# struct tm *tm = localtime(&t.sec);
|
|
|
|
# info.year = tm->tm_year + 1900;
|
|
|
|
# info.month = tm->tm_mon + 1;
|
|
|
|
# info.day = tm->tm_mday;
|
|
|
|
# info.hour = tm->tm_hour;
|
|
|
|
# info.minute = tm->tm_min;
|
|
|
|
# info.second = tm->tm_sec;
|
|
|
|
# info.yday = tm->tm_yday;
|
|
|
|
# info.wday = tm->tm_wday;
|
|
|
|
return info
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 12:02:47 +02:00
|
|
|
// TODO: Thread safety
|
|
|
|
fn (t Time) utc() Info {
|
|
|
|
info := Info{}
|
|
|
|
# struct tm *tm = gmtime(&t.sec);
|
|
|
|
# info.year = tm->tm_year + 1900;
|
|
|
|
# info.month = tm->tm_mon + 1;
|
|
|
|
# info.day = tm->tm_mday;
|
|
|
|
# info.hour = tm->tm_hour;
|
|
|
|
# info.minute = tm->tm_min;
|
|
|
|
# info.second = tm->tm_sec;
|
|
|
|
# info.yday = tm->tm_yday;
|
|
|
|
# info.wday = tm->tm_wday;
|
|
|
|
return info
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 12:02:47 +02:00
|
|
|
// TODO: Thread safety
|
|
|
|
fn (t Time) format(fmt string) string {
|
|
|
|
res := ''
|
|
|
|
cfmt := fmt.cstr()
|
|
|
|
# char buf[1024];
|
|
|
|
# struct tm *tm = localtime(&t.sec);
|
|
|
|
# strftime(buf, 1024, cfmt, tm);
|
|
|
|
# res = tos2(buf);
|
|
|
|
return res
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 12:02:47 +02:00
|
|
|
// TODO: Not implemented yet
|
|
|
|
pub fn parse(s string) ?Time {
|
|
|
|
return Time{}
|
|
|
|
}
|