2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module time
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
#include <time.h>
|
2019-07-14 17:43:57 +03:00
|
|
|
const (
|
2020-10-15 13:32:28 +03:00
|
|
|
days_string = 'MonTueWedThuFriSatSun'
|
|
|
|
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
|
|
months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec'
|
2019-12-20 00:29:37 +03:00
|
|
|
// The unsigned zero year for internal calculations.
|
|
|
|
// Must be 1 mod 400, and times before it will not compute correctly,
|
|
|
|
// but otherwise can be changed at will.
|
2020-10-15 13:32:28 +03:00
|
|
|
absolute_zero_year = i64(-292277022399) // as i64
|
2019-12-20 00:29:37 +03:00
|
|
|
seconds_per_minute = 60
|
2020-10-15 13:32:28 +03:00
|
|
|
seconds_per_hour = 60 * seconds_per_minute
|
|
|
|
seconds_per_day = 24 * seconds_per_hour
|
|
|
|
seconds_per_week = 7 * seconds_per_day
|
2019-12-20 00:29:37 +03:00
|
|
|
days_per_400_years = 365 * 400 + 97
|
|
|
|
days_per_100_years = 365 * 100 + 24
|
2020-10-15 13:32:28 +03:00
|
|
|
days_per_4_years = 365 * 4 + 1
|
|
|
|
days_before = [
|
2020-04-24 08:33:25 +03:00
|
|
|
0,
|
|
|
|
31,
|
|
|
|
31 + 28,
|
|
|
|
31 + 28 + 31,
|
|
|
|
31 + 28 + 31 + 30,
|
|
|
|
31 + 28 + 31 + 30 + 31,
|
|
|
|
31 + 28 + 31 + 30 + 31 + 30,
|
|
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31,
|
|
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
|
|
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
|
|
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
|
|
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
|
|
|
|
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
|
|
|
|
]
|
2020-12-11 21:30:34 +03:00
|
|
|
long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
2019-07-14 17:43:57 +03:00
|
|
|
)
|
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// Time contains various time units for a point in time.
|
2019-10-25 22:04:25 +03:00
|
|
|
pub struct Time {
|
2019-06-27 20:02:47 +03:00
|
|
|
pub:
|
2020-10-15 13:32:28 +03:00
|
|
|
year int
|
|
|
|
month int
|
|
|
|
day int
|
|
|
|
hour int
|
|
|
|
minute int
|
|
|
|
second int
|
|
|
|
microsecond int
|
|
|
|
unix u64
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// FormatDelimiter contains different time formats.
|
2019-11-01 02:13:03 +03:00
|
|
|
pub enum FormatTime {
|
2019-12-20 00:29:37 +03:00
|
|
|
hhmm12
|
|
|
|
hhmm24
|
|
|
|
hhmmss12
|
|
|
|
hhmmss24
|
2020-07-21 11:58:33 +03:00
|
|
|
hhmmss24_milli
|
|
|
|
hhmmss24_micro
|
2019-12-20 00:29:37 +03:00
|
|
|
no_time
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// FormatDelimiter contains different date formats.
|
2019-11-01 02:13:03 +03:00
|
|
|
pub enum FormatDate {
|
2019-12-20 00:29:37 +03:00
|
|
|
ddmmyy
|
|
|
|
ddmmyyyy
|
|
|
|
mmddyy
|
|
|
|
mmddyyyy
|
|
|
|
mmmd
|
|
|
|
mmmdd
|
|
|
|
mmmddyyyy
|
|
|
|
no_date
|
|
|
|
yyyymmdd
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// FormatDelimiter contains different time/date delimiters.
|
2019-11-01 02:13:03 +03:00
|
|
|
pub enum FormatDelimiter {
|
2019-12-20 00:29:37 +03:00
|
|
|
dot
|
|
|
|
hyphen
|
|
|
|
slash
|
|
|
|
space
|
2020-05-08 21:23:26 +03:00
|
|
|
no_delimiter
|
2019-11-01 02:13:03 +03:00
|
|
|
}
|
2019-07-14 17:43:57 +03:00
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// C.timeval represents a C time value.
|
2020-02-06 16:19:44 +03:00
|
|
|
pub struct C.timeval {
|
|
|
|
tv_sec u64
|
|
|
|
tv_usec u64
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2020-05-18 22:38:06 +03:00
|
|
|
fn C.localtime(t &C.time_t) &C.tm
|
2019-09-15 19:07:40 +03:00
|
|
|
|
2020-10-15 13:32:28 +03:00
|
|
|
fn C.time(t &C.time_t) C.time_t
|
2020-06-10 12:14:55 +03:00
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// now returns current local time.
|
2019-06-27 20:02:47 +03:00
|
|
|
pub fn now() Time {
|
2020-06-07 16:19:09 +03:00
|
|
|
$if macos {
|
|
|
|
return darwin_now()
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
return win_now()
|
|
|
|
}
|
|
|
|
$if solaris {
|
|
|
|
return solaris_now()
|
|
|
|
}
|
2020-09-25 18:02:02 +03:00
|
|
|
$if linux || android {
|
2020-06-07 16:19:09 +03:00
|
|
|
return linux_now()
|
|
|
|
}
|
|
|
|
// defaults to most common feature, the microsecond precision is not available
|
|
|
|
// in this API call
|
2019-06-27 20:02:47 +03:00
|
|
|
t := C.time(0)
|
2020-05-18 22:38:06 +03:00
|
|
|
now := C.localtime(&t)
|
2020-06-07 16:19:09 +03:00
|
|
|
return convert_ctime(now, 0)
|
2019-06-27 20:02:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// utc returns the current UTC time.
|
2020-06-10 12:14:55 +03:00
|
|
|
pub fn utc() Time {
|
|
|
|
$if macos {
|
|
|
|
return darwin_utc()
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
return win_utc()
|
|
|
|
}
|
|
|
|
$if solaris {
|
|
|
|
return solaris_utc()
|
|
|
|
}
|
2020-09-25 18:02:02 +03:00
|
|
|
$if linux || android {
|
2020-06-10 12:14:55 +03:00
|
|
|
return linux_utc()
|
|
|
|
}
|
|
|
|
// defaults to most common feature, the microsecond precision is not available
|
|
|
|
// in this API call
|
|
|
|
t := C.time(0)
|
|
|
|
_ = C.time(&t)
|
|
|
|
return unix2(int(t), 0)
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// smonth returns month name.
|
2019-06-27 20:02:47 +03:00
|
|
|
pub fn (t Time) smonth() string {
|
2020-09-16 22:13:20 +03:00
|
|
|
if t.month <= 0 || t.month > 12 {
|
|
|
|
return '---'
|
|
|
|
}
|
2019-06-27 20:02:47 +03:00
|
|
|
i := t.month - 1
|
2019-10-27 10:03:15 +03:00
|
|
|
return months_string[i * 3..(i + 1) * 3]
|
2019-06-27 20:02:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// new_time returns a time struct with calculated Unix time.
|
2019-06-28 17:04:14 +03:00
|
|
|
pub fn new_time(t Time) Time {
|
2019-12-31 19:11:47 +03:00
|
|
|
if t.unix != 0 {
|
2020-07-26 13:10:56 +03:00
|
|
|
return t
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 20:02:47 +03:00
|
|
|
tt := C.tm{
|
2019-12-20 00:29:37 +03:00
|
|
|
tm_sec: t.second
|
|
|
|
tm_min: t.minute
|
|
|
|
tm_hour: t.hour
|
|
|
|
tm_mday: t.day
|
|
|
|
tm_mon: t.month - 1
|
|
|
|
tm_year: t.year - 1900
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-07-26 13:10:56 +03:00
|
|
|
utime := u64(make_unix_time(tt))
|
2020-10-15 13:32:28 +03:00
|
|
|
return {
|
|
|
|
t |
|
|
|
|
unix: utime
|
|
|
|
}
|
2020-07-26 13:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// unix_time returns Unix time.
|
|
|
|
[inline]
|
|
|
|
pub fn (t Time) unix_time() int {
|
|
|
|
return int(t.unix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unix_time_milli returns Unix time with millisecond resolution.
|
|
|
|
[inline]
|
|
|
|
pub fn (t Time) unix_time_milli() u64 {
|
2020-10-15 13:32:28 +03:00
|
|
|
return t.unix * 1000 + u64(t.microsecond / 1000)
|
2019-06-27 20:02:47 +03:00
|
|
|
}
|
|
|
|
|
2020-12-12 06:22:30 +03:00
|
|
|
// add returns a new time that duration is added
|
|
|
|
pub fn (t Time) add(d Duration) Time {
|
|
|
|
microseconds := i64(t.unix) * 1000 * 1000 + t.microsecond + d.microseconds()
|
|
|
|
unix := microseconds / (1000 * 1000)
|
|
|
|
micro := microseconds % (1000 * 1000)
|
|
|
|
return unix2(int(unix), int(micro))
|
|
|
|
}
|
|
|
|
|
2020-04-24 08:33:25 +03:00
|
|
|
// add_seconds returns a new time struct with an added number of seconds.
|
2019-06-27 20:02:47 +03:00
|
|
|
pub fn (t Time) add_seconds(seconds int) Time {
|
2020-12-12 06:22:30 +03:00
|
|
|
return t.add(seconds * second)
|
2019-06-27 20:02:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// add_days returns a new time struct with an added number of days.
|
2019-11-11 17:18:32 +03:00
|
|
|
pub fn (t Time) add_days(days int) Time {
|
2020-12-12 06:22:30 +03:00
|
|
|
return t.add(days * 24 * hour)
|
2019-11-11 17:18:32 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// since returns a number of seconds elapsed since a given time.
|
2019-06-27 20:02:47 +03:00
|
|
|
fn since(t Time) int {
|
2020-02-06 16:19:44 +03:00
|
|
|
// TODO Use time.Duration instead of seconds
|
2019-06-27 20:02:47 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// relative returns a string representation of the difference between t
|
|
|
|
// and the current time.
|
2019-06-27 20:02:47 +03:00
|
|
|
pub fn (t Time) relative() string {
|
2020-10-15 13:32:28 +03:00
|
|
|
znow := now()
|
2020-07-01 01:53:53 +03:00
|
|
|
secs := znow.unix - t.unix
|
2020-06-11 21:26:46 +03:00
|
|
|
if secs <= 30 {
|
|
|
|
// right now or in the future
|
|
|
|
// TODO handle time in the future
|
|
|
|
return 'now'
|
|
|
|
}
|
|
|
|
if secs < 60 {
|
|
|
|
return '1m'
|
|
|
|
}
|
|
|
|
if secs < 3600 {
|
2020-10-15 13:32:28 +03:00
|
|
|
m := secs / 60
|
2020-06-12 16:28:24 +03:00
|
|
|
if m == 1 {
|
|
|
|
return '1 minute ago'
|
|
|
|
}
|
|
|
|
return '$m minutes ago'
|
2020-06-11 21:26:46 +03:00
|
|
|
}
|
|
|
|
if secs < 3600 * 24 {
|
2020-10-15 13:32:28 +03:00
|
|
|
h := secs / 3600
|
2020-06-12 16:28:24 +03:00
|
|
|
if h == 1 {
|
|
|
|
return '1 hour ago'
|
|
|
|
}
|
|
|
|
return '$h hours ago'
|
2020-06-11 21:26:46 +03:00
|
|
|
}
|
|
|
|
if secs < 3600 * 24 * 5 {
|
2020-10-15 13:32:28 +03:00
|
|
|
d := secs / 3600 / 24
|
2020-06-12 16:28:24 +03:00
|
|
|
if d == 1 {
|
|
|
|
return '1 day ago'
|
|
|
|
}
|
|
|
|
return '$d days ago'
|
2020-06-11 21:26:46 +03:00
|
|
|
}
|
|
|
|
if secs > 3600 * 24 * 10000 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return t.md()
|
|
|
|
}
|
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// relative_short returns a string saying how long ago a time occured as follows:
|
|
|
|
// 0-30 seconds: `"now"`; 30-60 seconds: `"1m"`; anything else is rounded to the
|
|
|
|
// nearest minute, hour or day; anything higher than 10000 days (about 27 years)
|
|
|
|
// years returns an empty string.
|
|
|
|
// Some Examples:
|
|
|
|
// `0s -> 'now'`;
|
|
|
|
// `20s -> 'now'`;
|
|
|
|
// `47s -> '1m'`;
|
|
|
|
// `456s -> '7m'`;
|
|
|
|
// `1234s -> '20m'`;
|
|
|
|
// `16834s -> '4h'`;
|
|
|
|
// `1687440s -> '33d'`;
|
|
|
|
// `15842354871s -> ''`
|
2020-06-11 21:26:46 +03:00
|
|
|
pub fn (t Time) relative_short() string {
|
2020-10-15 13:32:28 +03:00
|
|
|
znow := now()
|
2020-07-01 01:53:53 +03:00
|
|
|
secs := znow.unix - t.unix
|
2019-06-27 20:02:47 +03:00
|
|
|
if secs <= 30 {
|
|
|
|
// right now or in the future
|
|
|
|
// TODO handle time in the future
|
|
|
|
return 'now'
|
|
|
|
}
|
|
|
|
if secs < 60 {
|
|
|
|
return '1m'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 20:02:47 +03:00
|
|
|
if secs < 3600 {
|
2020-10-15 23:12:59 +03:00
|
|
|
return '${secs / 60}m'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 20:02:47 +03:00
|
|
|
if secs < 3600 * 24 {
|
2020-10-15 23:12:59 +03:00
|
|
|
return '${secs / 3600}h'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-27 20:02:47 +03:00
|
|
|
if secs < 3600 * 24 * 5 {
|
2020-10-15 23:12:59 +03:00
|
|
|
return '${secs / 3600 / 24}d'
|
2019-06-27 20:02:47 +03:00
|
|
|
}
|
|
|
|
if secs > 3600 * 24 * 10000 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return t.md()
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// day_of_week returns the current day of a given year, month, and day,
|
|
|
|
// as an integer.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn day_of_week(y int, m int, d int) int {
|
2019-08-30 01:05:58 +03:00
|
|
|
// Sakomotho's algorithm is explained here:
|
2019-08-25 23:35:01 +03:00
|
|
|
// https://stackoverflow.com/a/6385934
|
|
|
|
t := [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
|
2019-08-30 01:05:58 +03:00
|
|
|
mut sy := y
|
2020-03-27 16:57:19 +03:00
|
|
|
if m < 3 {
|
2019-08-25 23:35:01 +03:00
|
|
|
sy = sy - 1
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
return (sy + sy / 4 - sy / 100 + sy / 400 + t[m - 1] + d - 1) % 7 + 1
|
2019-06-27 20:02:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// day_of_week returns the current day as an integer.
|
2019-06-27 20:02:47 +03:00
|
|
|
pub fn (t Time) day_of_week() int {
|
|
|
|
return day_of_week(t.year, t.month, t.day)
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// weekday_str returns the current day as a string.
|
2019-06-27 20:02:47 +03:00
|
|
|
pub fn (t Time) weekday_str() string {
|
|
|
|
i := t.day_of_week() - 1
|
2019-10-27 10:03:15 +03:00
|
|
|
return days_string[i * 3..(i + 1) * 3]
|
2019-06-23 22:18:24 +03:00
|
|
|
}
|
2019-06-28 17:04:14 +03:00
|
|
|
|
2020-08-01 17:36:09 +03:00
|
|
|
// weekday_str returns the current day as a string.
|
|
|
|
pub fn (t Time) long_weekday_str() string {
|
|
|
|
i := t.day_of_week() - 1
|
|
|
|
return long_days[i]
|
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// ticks returns a number of milliseconds elapsed since system start.
|
2019-08-23 23:55:45 +03:00
|
|
|
pub fn ticks() i64 {
|
|
|
|
$if windows {
|
2019-06-28 17:04:14 +03:00
|
|
|
return C.GetTickCount()
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2020-01-07 14:58:24 +03:00
|
|
|
ts := C.timeval{}
|
2019-12-20 00:29:37 +03:00
|
|
|
C.gettimeofday(&ts, 0)
|
2019-11-19 18:28:29 +03:00
|
|
|
return i64(ts.tv_sec * u64(1000) + (ts.tv_usec / u64(1000)))
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2020-02-06 16:19:44 +03:00
|
|
|
// t := i64(C.mach_absolute_time())
|
|
|
|
// # Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &t );
|
|
|
|
// # return (double)(* (uint64_t *) &elapsedNano) / 1000000;
|
2019-06-28 17:04:14 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// sleep makes the calling thread sleep for a given number of seconds.
|
2019-06-28 17:04:14 +03:00
|
|
|
pub fn sleep(seconds int) {
|
2019-08-23 23:55:45 +03:00
|
|
|
$if windows {
|
2019-11-16 02:30:50 +03:00
|
|
|
C.Sleep(seconds * 1000)
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2019-06-28 17:04:14 +03:00
|
|
|
C.sleep(seconds)
|
2019-08-23 23:55:45 +03:00
|
|
|
}
|
2019-06-28 17:04:14 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// sleep_ms makes the calling thread sleep for a given number of milliseconds.
|
2020-01-07 14:58:24 +03:00
|
|
|
pub fn sleep_ms(milliseconds int) {
|
2019-12-20 00:29:37 +03:00
|
|
|
$if windows {
|
2020-01-07 14:58:24 +03:00
|
|
|
C.Sleep(milliseconds)
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2020-01-07 14:58:24 +03:00
|
|
|
C.usleep(milliseconds * 1000)
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2019-06-28 17:04:14 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// usleep makes the calling thread sleep for a given number of microseconds.
|
2020-01-07 14:58:24 +03:00
|
|
|
pub fn usleep(microseconds int) {
|
2019-08-23 23:55:45 +03:00
|
|
|
$if windows {
|
2020-01-07 14:58:24 +03:00
|
|
|
milliseconds := microseconds / 1000
|
|
|
|
C.Sleep(milliseconds)
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2020-01-07 14:58:24 +03:00
|
|
|
C.usleep(microseconds)
|
2019-08-23 23:55:45 +03:00
|
|
|
}
|
2019-06-28 17:04:14 +03:00
|
|
|
}
|
2019-07-03 19:55:07 +03:00
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// is_leap_year checks if a given a year is a leap year.
|
2019-07-03 19:55:07 +03:00
|
|
|
pub fn is_leap_year(year int) bool {
|
2019-12-20 00:29:37 +03:00
|
|
|
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)
|
2019-07-14 17:43:57 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// days_in_month returns a number of days in a given month.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn days_in_month(month int, year int) ?int {
|
2019-07-14 17:43:57 +03:00
|
|
|
if month > 12 || month < 1 {
|
|
|
|
return error('Invalid month: $month')
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
extra := if month == 2 && is_leap_year(year) { 1 } else { 0 }
|
|
|
|
res := month_days[month - 1] + extra
|
2019-08-23 23:55:45 +03:00
|
|
|
return res
|
2019-07-14 17:43:57 +03:00
|
|
|
}
|
2019-11-01 02:13:03 +03:00
|
|
|
|
2020-02-06 16:19:44 +03:00
|
|
|
// str returns time in the same format as `parse` expects ("YYYY-MM-DD HH:MM:SS").
|
2020-01-20 17:06:15 +03:00
|
|
|
pub fn (t Time) str() string {
|
2020-02-06 16:19:44 +03:00
|
|
|
// TODO Define common default format for
|
|
|
|
// `str` and `parse` and use it in both ways
|
2020-01-20 17:06:15 +03:00
|
|
|
return t.format_ss()
|
|
|
|
}
|
2020-02-05 08:13:11 +03:00
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// convert_ctime converts a C time to V time.
|
2020-06-07 16:19:09 +03:00
|
|
|
fn convert_ctime(t C.tm, microsecond int) Time {
|
2020-02-05 08:13:11 +03:00
|
|
|
return Time{
|
|
|
|
year: t.tm_year + 1900
|
|
|
|
month: t.tm_mon + 1
|
|
|
|
day: t.tm_mday
|
|
|
|
hour: t.tm_hour
|
|
|
|
minute: t.tm_min
|
|
|
|
second: t.tm_sec
|
2020-06-07 16:19:09 +03:00
|
|
|
microsecond: microsecond
|
2020-05-27 06:42:48 +03:00
|
|
|
unix: u64(make_unix_time(t))
|
2020-02-05 08:13:11 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 08:33:25 +03:00
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// A lot of these are taken from the Go library.
|
2020-09-25 13:02:32 +03:00
|
|
|
pub type Duration = i64
|
2020-04-24 08:33:25 +03:00
|
|
|
|
2020-10-15 13:32:28 +03:00
|
|
|
pub const (
|
2020-04-24 08:33:25 +03:00
|
|
|
nanosecond = Duration(1)
|
2020-12-21 11:27:06 +03:00
|
|
|
microsecond = Duration(1000 * nanosecond)
|
|
|
|
millisecond = Duration(1000 * microsecond)
|
|
|
|
second = Duration(1000 * millisecond)
|
|
|
|
minute = Duration(60 * second)
|
|
|
|
hour = Duration(60 * minute)
|
2020-08-10 19:06:42 +03:00
|
|
|
infinite = Duration(-1)
|
2020-04-24 08:33:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// nanoseconds returns the duration as an integer number of nanoseconds.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn (d Duration) nanoseconds() i64 {
|
|
|
|
return i64(d)
|
|
|
|
}
|
2020-04-24 08:33:25 +03:00
|
|
|
|
|
|
|
// microseconds returns the duration as an integer number of microseconds.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn (d Duration) microseconds() i64 {
|
|
|
|
return i64(d) / 1000
|
|
|
|
}
|
2020-04-24 08:33:25 +03:00
|
|
|
|
|
|
|
// milliseconds returns the duration as an integer number of milliseconds.
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn (d Duration) milliseconds() i64 {
|
|
|
|
return i64(d) / 1000000
|
|
|
|
}
|
2020-04-24 08:33:25 +03:00
|
|
|
|
|
|
|
// The following functions return floating point numbers because it's common to
|
|
|
|
// consider all of them in sub-one intervals
|
|
|
|
// seconds returns the duration as a floating point number of seconds.
|
|
|
|
pub fn (d Duration) seconds() f64 {
|
|
|
|
sec := d / second
|
|
|
|
nsec := d % second
|
2020-10-15 13:32:28 +03:00
|
|
|
return f64(sec) + f64(nsec) / 1e9
|
2020-04-24 08:33:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// minutes returns the duration as a floating point number of minutes.
|
|
|
|
pub fn (d Duration) minutes() f64 {
|
|
|
|
min := d / minute
|
|
|
|
nsec := d % minute
|
2020-10-15 13:32:28 +03:00
|
|
|
return f64(min) + f64(nsec) / (60 * 1e9)
|
2020-04-24 08:33:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// hours returns the duration as a floating point number of hours.
|
|
|
|
pub fn (d Duration) hours() f64 {
|
|
|
|
hr := d / hour
|
|
|
|
nsec := d % hour
|
2020-10-15 13:32:28 +03:00
|
|
|
return f64(hr) + f64(nsec) / (60 * 60 * 1e9)
|
2020-04-24 08:33:25 +03:00
|
|
|
}
|
2020-12-26 04:10:47 +03:00
|
|
|
|
2021-01-13 17:30:54 +03:00
|
|
|
// offset returns time zone UTC offset in seconds.
|
2020-12-26 04:10:47 +03:00
|
|
|
pub fn offset() int {
|
|
|
|
t := now()
|
|
|
|
local := t.local()
|
|
|
|
return int(local.unix - t.unix)
|
|
|
|
}
|