2019-06-22 20:20:28 +02:00
|
|
|
module time
|
|
|
|
|
2021-02-03 09:17:13 +01:00
|
|
|
pub const (
|
2020-10-15 12:32:28 +02:00
|
|
|
days_string = 'MonTueWedThuFriSatSun'
|
|
|
|
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
|
|
months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec'
|
2019-12-19 22:29:37 +01: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.
|
2021-12-19 18:32:42 +01:00
|
|
|
absolute_zero_year = i64(-292277022399)
|
2019-12-19 22:29:37 +01:00
|
|
|
seconds_per_minute = 60
|
2021-01-25 10:26:20 +01:00
|
|
|
seconds_per_hour = 60 * seconds_per_minute
|
|
|
|
seconds_per_day = 24 * seconds_per_hour
|
|
|
|
seconds_per_week = 7 * seconds_per_day
|
2019-12-19 22:29:37 +01:00
|
|
|
days_per_400_years = 365 * 400 + 97
|
|
|
|
days_per_100_years = 365 * 100 + 24
|
2020-10-15 12:32:28 +02:00
|
|
|
days_per_4_years = 365 * 4 + 1
|
|
|
|
days_before = [
|
2020-04-24 07:33:25 +02: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,
|
|
|
|
]
|
2021-03-22 23:06:12 +01:00
|
|
|
long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
|
2021-09-21 15:20:09 +02:00
|
|
|
'Sunday']
|
2019-07-14 16:43:57 +02:00
|
|
|
)
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// Time contains various time units for a point in time.
|
2019-10-25 21:04:25 +02:00
|
|
|
pub struct Time {
|
2019-06-27 19:02:47 +02:00
|
|
|
pub:
|
2020-10-15 12:32:28 +02:00
|
|
|
year int
|
|
|
|
month int
|
|
|
|
day int
|
|
|
|
hour int
|
|
|
|
minute int
|
|
|
|
second int
|
|
|
|
microsecond int
|
2021-08-04 12:12:02 +02:00
|
|
|
unix i64
|
2022-03-31 23:11:17 +02:00
|
|
|
is_local bool // used to make time.now().local().local() == time.now().local()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// FormatDelimiter contains different time formats.
|
2019-11-01 00:13:03 +01:00
|
|
|
pub enum FormatTime {
|
2019-12-19 22:29:37 +01:00
|
|
|
hhmm12
|
|
|
|
hhmm24
|
|
|
|
hhmmss12
|
|
|
|
hhmmss24
|
2020-07-21 10:58:33 +02:00
|
|
|
hhmmss24_milli
|
|
|
|
hhmmss24_micro
|
2019-12-19 22:29:37 +01:00
|
|
|
no_time
|
2019-11-01 00:13:03 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// FormatDelimiter contains different date formats.
|
2019-11-01 00:13:03 +01:00
|
|
|
pub enum FormatDate {
|
2019-12-19 22:29:37 +01:00
|
|
|
ddmmyy
|
|
|
|
ddmmyyyy
|
|
|
|
mmddyy
|
|
|
|
mmddyyyy
|
|
|
|
mmmd
|
|
|
|
mmmdd
|
2021-04-01 12:04:59 +02:00
|
|
|
mmmddyy
|
2019-12-19 22:29:37 +01:00
|
|
|
mmmddyyyy
|
|
|
|
no_date
|
|
|
|
yyyymmdd
|
2021-04-01 12:04:59 +02:00
|
|
|
yymmdd
|
2019-11-01 00:13:03 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// FormatDelimiter contains different time/date delimiters.
|
2019-11-01 00:13:03 +01:00
|
|
|
pub enum FormatDelimiter {
|
2019-12-19 22:29:37 +01:00
|
|
|
dot
|
|
|
|
hyphen
|
|
|
|
slash
|
|
|
|
space
|
2020-05-08 20:23:26 +02:00
|
|
|
no_delimiter
|
2019-11-01 00:13:03 +01:00
|
|
|
}
|
2019-07-14 16:43:57 +02:00
|
|
|
|
2020-02-06 14:19:44 +01:00
|
|
|
// smonth returns month name.
|
2019-06-27 19:02:47 +02:00
|
|
|
pub fn (t Time) smonth() string {
|
2020-09-16 21:13:20 +02:00
|
|
|
if t.month <= 0 || t.month > 12 {
|
|
|
|
return '---'
|
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
i := t.month - 1
|
2021-01-24 10:01:10 +01:00
|
|
|
return time.months_string[i * 3..(i + 1) * 3]
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2020-07-26 12:10:56 +02:00
|
|
|
// unix_time returns Unix time.
|
|
|
|
[inline]
|
2021-08-04 12:12:02 +02:00
|
|
|
pub fn (t Time) unix_time() i64 {
|
|
|
|
return t.unix
|
2020-07-26 12:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// unix_time_milli returns Unix time with millisecond resolution.
|
|
|
|
[inline]
|
2021-08-04 12:12:02 +02:00
|
|
|
pub fn (t Time) unix_time_milli() i64 {
|
|
|
|
return t.unix * 1000 + (t.microsecond / 1000)
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 04:22:30 +01:00
|
|
|
// add returns a new time that duration is added
|
|
|
|
pub fn (t Time) add(d Duration) Time {
|
2021-08-04 12:12:02 +02:00
|
|
|
microseconds := i64(t.unix) * 1_000_000 + t.microsecond + d.microseconds()
|
|
|
|
unix := microseconds / 1_000_000
|
|
|
|
micro := microseconds % 1_000_000
|
2021-07-06 17:51:47 +02:00
|
|
|
return unix2(unix, int(micro))
|
2020-12-12 04:22:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 07:33:25 +02:00
|
|
|
// add_seconds returns a new time struct with an added number of seconds.
|
2019-06-27 19:02:47 +02:00
|
|
|
pub fn (t Time) add_seconds(seconds int) Time {
|
2021-01-24 10:01:10 +01:00
|
|
|
return t.add(seconds * time.second)
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:19:44 +01:00
|
|
|
// add_days returns a new time struct with an added number of days.
|
2019-11-11 15:18:32 +01:00
|
|
|
pub fn (t Time) add_days(days int) Time {
|
2021-01-24 10:01:10 +01:00
|
|
|
return t.add(days * 24 * time.hour)
|
2019-11-11 15:18:32 +01:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:36:28 +02:00
|
|
|
// since returns the time duration elapsed since a given time.
|
|
|
|
pub fn since(t Time) Duration {
|
|
|
|
return now() - t
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// relative returns a string representation of the difference between t
|
|
|
|
// and the current time.
|
2019-06-27 19:02:47 +02:00
|
|
|
pub fn (t Time) relative() string {
|
2020-10-15 12:32:28 +02:00
|
|
|
znow := now()
|
2020-07-01 00:53:53 +02:00
|
|
|
secs := znow.unix - t.unix
|
2020-06-11 20:26:46 +02: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 12:32:28 +02:00
|
|
|
m := secs / 60
|
2020-06-12 15:28:24 +02:00
|
|
|
if m == 1 {
|
|
|
|
return '1 minute ago'
|
|
|
|
}
|
|
|
|
return '$m minutes ago'
|
2020-06-11 20:26:46 +02:00
|
|
|
}
|
|
|
|
if secs < 3600 * 24 {
|
2020-10-15 12:32:28 +02:00
|
|
|
h := secs / 3600
|
2020-06-12 15:28:24 +02:00
|
|
|
if h == 1 {
|
|
|
|
return '1 hour ago'
|
|
|
|
}
|
|
|
|
return '$h hours ago'
|
2020-06-11 20:26:46 +02:00
|
|
|
}
|
|
|
|
if secs < 3600 * 24 * 5 {
|
2020-10-15 12:32:28 +02:00
|
|
|
d := secs / 3600 / 24
|
2020-06-12 15:28:24 +02:00
|
|
|
if d == 1 {
|
|
|
|
return '1 day ago'
|
|
|
|
}
|
|
|
|
return '$d days ago'
|
2020-06-11 20:26:46 +02:00
|
|
|
}
|
|
|
|
if secs > 3600 * 24 * 10000 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return t.md()
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01: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 20:26:46 +02:00
|
|
|
pub fn (t Time) relative_short() string {
|
2020-10-15 12:32:28 +02:00
|
|
|
znow := now()
|
2020-07-01 00:53:53 +02:00
|
|
|
secs := znow.unix - t.unix
|
2019-06-27 19:02:47 +02: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 20:20:28 +02:00
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
if secs < 3600 {
|
2020-10-15 22:12:59 +02:00
|
|
|
return '${secs / 60}m'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
if secs < 3600 * 24 {
|
2020-10-15 22:12:59 +02:00
|
|
|
return '${secs / 3600}h'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
if secs < 3600 * 24 * 5 {
|
2020-10-15 22:12:59 +02:00
|
|
|
return '${secs / 3600 / 24}d'
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
|
|
|
if secs > 3600 * 24 * 10000 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return t.md()
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:19:44 +01:00
|
|
|
// day_of_week returns the current day of a given year, month, and day,
|
|
|
|
// as an integer.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn day_of_week(y int, m int, d int) int {
|
2019-08-30 00:05:58 +02:00
|
|
|
// Sakomotho's algorithm is explained here:
|
2019-08-25 22:35:01 +02:00
|
|
|
// https://stackoverflow.com/a/6385934
|
|
|
|
t := [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
|
2019-08-30 00:05:58 +02:00
|
|
|
mut sy := y
|
2020-03-27 14:57:19 +01:00
|
|
|
if m < 3 {
|
2019-08-25 22:35:01 +02:00
|
|
|
sy = sy - 1
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
return (sy + sy / 4 - sy / 100 + sy / 400 + t[m - 1] + d - 1) % 7 + 1
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:19:44 +01:00
|
|
|
// day_of_week returns the current day as an integer.
|
2019-06-27 19:02:47 +02:00
|
|
|
pub fn (t Time) day_of_week() int {
|
|
|
|
return day_of_week(t.year, t.month, t.day)
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:19:44 +01:00
|
|
|
// weekday_str returns the current day as a string.
|
2019-06-27 19:02:47 +02:00
|
|
|
pub fn (t Time) weekday_str() string {
|
|
|
|
i := t.day_of_week() - 1
|
2021-01-24 10:01:10 +01:00
|
|
|
return time.days_string[i * 3..(i + 1) * 3]
|
2019-06-23 21:18:24 +02:00
|
|
|
}
|
2019-06-28 16:04:14 +02:00
|
|
|
|
2020-08-01 16:36:09 +02:00
|
|
|
// weekday_str returns the current day as a string.
|
|
|
|
pub fn (t Time) long_weekday_str() string {
|
|
|
|
i := t.day_of_week() - 1
|
2021-01-24 10:01:10 +01:00
|
|
|
return time.long_days[i]
|
2020-08-01 16:36:09 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:19:44 +01:00
|
|
|
// is_leap_year checks if a given a year is a leap year.
|
2019-07-03 18:55:07 +02:00
|
|
|
pub fn is_leap_year(year int) bool {
|
2019-12-19 22:29:37 +01:00
|
|
|
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)
|
2019-07-14 16:43:57 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:19:44 +01:00
|
|
|
// days_in_month returns a number of days in a given month.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn days_in_month(month int, year int) ?int {
|
2019-07-14 16:43:57 +02:00
|
|
|
if month > 12 || month < 1 {
|
|
|
|
return error('Invalid month: $month')
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
extra := if month == 2 && is_leap_year(year) { 1 } else { 0 }
|
2021-01-24 10:01:10 +01:00
|
|
|
res := time.month_days[month - 1] + extra
|
2019-08-23 22:55:45 +02:00
|
|
|
return res
|
2019-07-14 16:43:57 +02:00
|
|
|
}
|
2019-11-01 00:13:03 +01:00
|
|
|
|
2021-08-04 12:12:02 +02:00
|
|
|
// str returns time in the same format as `parse` expects ("YYYY-MM-DD HH:MM:SS").
|
|
|
|
pub fn (t Time) debug() string {
|
|
|
|
return 'Time{ year: ${t.year:04} month: ${t.month:02} day: ${t.day:02} hour: ${t.hour:02} minute: ${t.minute:02} second: ${t.second:02} microsecond: ${t.microsecond:06} unix: ${t.unix:07} }'
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// A lot of these are taken from the Go library.
|
2020-09-25 12:02:32 +02:00
|
|
|
pub type Duration = i64
|
2020-04-24 07:33:25 +02:00
|
|
|
|
2021-12-19 18:32:42 +01:00
|
|
|
pub const nanosecond = Duration(1)
|
|
|
|
|
|
|
|
pub const microsecond = Duration(1000 * nanosecond)
|
|
|
|
|
|
|
|
pub const millisecond = Duration(1000 * microsecond)
|
|
|
|
|
|
|
|
pub const second = Duration(1000 * millisecond)
|
|
|
|
|
|
|
|
pub const minute = Duration(60 * second)
|
|
|
|
|
|
|
|
pub const hour = Duration(60 * minute)
|
|
|
|
|
|
|
|
pub const infinite = Duration(i64(9223372036854775807))
|
2020-04-24 07:33:25 +02:00
|
|
|
|
|
|
|
// nanoseconds returns the duration as an integer number of nanoseconds.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (d Duration) nanoseconds() i64 {
|
|
|
|
return i64(d)
|
|
|
|
}
|
2020-04-24 07:33:25 +02:00
|
|
|
|
|
|
|
// microseconds returns the duration as an integer number of microseconds.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (d Duration) microseconds() i64 {
|
|
|
|
return i64(d) / 1000
|
|
|
|
}
|
2020-04-24 07:33:25 +02:00
|
|
|
|
|
|
|
// milliseconds returns the duration as an integer number of milliseconds.
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn (d Duration) milliseconds() i64 {
|
|
|
|
return i64(d) / 1000000
|
|
|
|
}
|
2020-04-24 07:33:25 +02: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 {
|
2021-01-24 10:01:10 +01:00
|
|
|
sec := d / time.second
|
|
|
|
nsec := d % time.second
|
2020-10-15 12:32:28 +02:00
|
|
|
return f64(sec) + f64(nsec) / 1e9
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// minutes returns the duration as a floating point number of minutes.
|
|
|
|
pub fn (d Duration) minutes() f64 {
|
2021-01-24 10:01:10 +01:00
|
|
|
min := d / time.minute
|
|
|
|
nsec := d % time.minute
|
2020-10-15 12:32:28 +02:00
|
|
|
return f64(min) + f64(nsec) / (60 * 1e9)
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// hours returns the duration as a floating point number of hours.
|
|
|
|
pub fn (d Duration) hours() f64 {
|
2021-01-24 10:01:10 +01:00
|
|
|
hr := d / time.hour
|
|
|
|
nsec := d % time.hour
|
2020-10-15 12:32:28 +02:00
|
|
|
return f64(hr) + f64(nsec) / (60 * 60 * 1e9)
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
2020-12-26 02:10:47 +01:00
|
|
|
|
2021-12-19 18:32:42 +01:00
|
|
|
// str pretty prints the duration.
|
|
|
|
pub fn (d Duration) str() string {
|
|
|
|
if d == time.infinite {
|
|
|
|
return 'inf'
|
|
|
|
}
|
|
|
|
mut t := i64(d)
|
|
|
|
hr := t / time.hour
|
|
|
|
t -= hr * time.hour
|
|
|
|
min := t / time.minute
|
|
|
|
t -= min * time.minute
|
|
|
|
sec := t / time.second
|
|
|
|
t -= sec * time.second
|
|
|
|
ms := t / time.millisecond
|
|
|
|
t -= ms * time.millisecond
|
|
|
|
us := t / time.microsecond
|
|
|
|
t -= us * time.microsecond
|
|
|
|
ns := t
|
|
|
|
|
|
|
|
if hr > 0 {
|
|
|
|
return '$hr:${min:02}:${sec:02}'
|
|
|
|
}
|
|
|
|
if min > 0 {
|
|
|
|
return '$min:${sec:02}.${ms:03}'
|
|
|
|
}
|
|
|
|
if sec > 0 {
|
|
|
|
return '${sec}.${ms:03}s'
|
|
|
|
}
|
|
|
|
if ms > 0 {
|
|
|
|
return '${ms}.${us:03}ms'
|
|
|
|
}
|
|
|
|
if us > 0 {
|
|
|
|
return '${us}.${ns:03}us'
|
|
|
|
}
|
|
|
|
return '${ns}ns'
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// offset returns time zone UTC offset in seconds.
|
2020-12-26 02:10:47 +01:00
|
|
|
pub fn offset() int {
|
2022-03-31 23:11:17 +02:00
|
|
|
t := utc()
|
2020-12-26 02:10:47 +01:00
|
|
|
local := t.local()
|
|
|
|
return int(local.unix - t.unix)
|
|
|
|
}
|