v/vlib/time/time.v

268 lines
6.1 KiB
V
Raw Normal View History

2020-01-23 21:04:46 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-06-23 04:21:30 +02:00
// 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
2020-02-06 14:19:44 +01:00
#include <time.h>
2019-07-14 16:43:57 +02:00
const (
2019-12-19 22:29:37 +01:00
days_string = 'MonTueWedThuFriSatSun'
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec'
// 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-02-07 14:49:14 +01:00
absolute_zero_year = i64(-292277022399 )//as i64
2019-12-19 22:29:37 +01:00
seconds_per_minute = 60
seconds_per_hour = 60 * seconds_per_minute
seconds_per_day = 24 * seconds_per_hour
seconds_per_week = 7 * seconds_per_day
days_per_400_years = 365 * 400 + 97
days_per_100_years = 365 * 100 + 24
days_per_4_years = 365 * 4 + 1
days_before = [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, ]
2019-07-14 16:43:57 +02:00
)
2019-10-25 21:04:25 +02:00
pub struct Time {
pub:
year int
month int
day int
hour int
minute int
second int
unix int
2019-06-22 20:20:28 +02:00
}
pub enum FormatTime {
2019-12-19 22:29:37 +01:00
hhmm12
hhmm24
hhmmss12
hhmmss24
no_time
}
pub enum FormatDate {
2019-12-19 22:29:37 +01:00
ddmmyy
ddmmyyyy
mmddyy
mmddyyyy
mmmd
mmmdd
mmmddyyyy
no_date
yyyymmdd
}
pub enum FormatDelimiter {
2019-12-19 22:29:37 +01:00
dot
hyphen
slash
space
}
2019-07-14 16:43:57 +02:00
2020-03-31 15:18:25 +02:00
// TODO: C.time_t. works in v2
type time_t voidptr
2020-02-06 14:19:44 +01:00
pub struct C.timeval {
tv_sec u64
tv_usec u64
}
2019-12-19 22:29:37 +01:00
2020-02-06 14:19:44 +01:00
fn C.localtime(int) &C.tm
2019-10-07 00:31:01 +02:00
2020-03-31 15:18:25 +02:00
fn C.time(int) time_t
2019-09-15 18:07:40 +02:00
2020-02-06 14:19:44 +01:00
// now returns current local time.
pub fn now() Time {
t := C.time(0)
2019-12-04 10:19:32 +01:00
mut now := &C.tm(0)
now = C.localtime(&t)
return convert_ctime(now)
}
2020-02-06 14:19:44 +01:00
// smonth returns month name.
pub fn (t Time) smonth() string {
i := t.month - 1
return months_string[i * 3..(i + 1) * 3]
}
2020-02-06 14:19:44 +01:00
// new_time returns a time struct with calculated Unix time.
2019-06-28 16:04:14 +02:00
pub fn new_time(t Time) Time {
2019-12-31 17:11:47 +01:00
return Time{
year: t.year
month: t.month
day: t.day
hour: t.hour
minute: t.minute
second: t.second
2020-03-22 19:43:59 +01:00
unix: t.unix_time()
2019-12-31 17:11:47 +01:00
}
2020-02-06 14:19:44 +01:00
// TODO Use the syntax below when it works with reserved keywords like `unix`
// return {
// t |
2020-03-22 19:43:59 +01:00
// unix:t.unix_time()
2020-02-06 14:19:44 +01:00
// }
2019-06-22 20:20:28 +02:00
}
2020-03-22 19:43:59 +01:00
// unix_time returns Unix time.
2020-04-04 12:10:56 +02:00
pub fn (t Time) unix_time() int {
2019-12-31 17:11:47 +01:00
if t.unix != 0 {
return t.unix
2019-06-22 20:20:28 +02:00
}
tt := C.tm{
2019-12-19 22:29:37 +01: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 20:20:28 +02:00
}
2020-02-04 12:17:04 +01:00
return make_unix_time(tt)
}
2020-02-06 14:19:44 +01:00
// add_days returns a new time struct with an added number of seconds.
pub fn (t Time) add_seconds(seconds int) Time {
2020-02-06 14:19:44 +01:00
// TODO Add(d time.Duration)
2019-12-31 17:11:47 +01:00
return unix(t.unix + seconds)
}
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 {
2019-12-31 17:11:47 +01:00
return unix(t.unix + days * 3600 * 24)
2019-11-11 15:18:32 +01:00
}
2020-02-06 14:19:44 +01:00
// since returns a number of seconds elapsed since a given time.
fn since(t Time) int {
2020-02-06 14:19:44 +01:00
// TODO Use time.Duration instead of seconds
return 0
}
2020-02-06 14:19:44 +01:00
// relative returns a string representation of difference between time
// and current time.
pub fn (t Time) relative() string {
now := time.now()
2019-12-31 17:11:47 +01:00
secs := now.unix - t.unix
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
}
if secs < 3600 {
return '${secs/60}m'
2019-06-22 20:20:28 +02:00
}
if secs < 3600 * 24 {
return '${secs/3600}h'
2019-06-22 20:20:28 +02:00
}
if secs < 3600 * 24 * 5 {
return '${secs/3600/24}d'
}
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.
2019-06-28 16:04:14 +02:00
pub fn day_of_week(y, m, d int) int {
2019-08-30 00:05:58 +02:00
// Sakomotho's algorithm is explained here:
// 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
if m < 3 {
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
}
2020-02-06 14:19:44 +01:00
// day_of_week returns the current day as an integer.
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.
pub fn (t Time) weekday_str() string {
i := t.day_of_week() - 1
return days_string[i * 3..(i + 1) * 3]
2019-06-23 21:18:24 +02:00
}
2019-06-28 16:04:14 +02:00
2020-02-06 14:19:44 +01:00
// ticks returns a number of milliseconds elapsed since system start.
pub fn ticks() i64 {
$if windows {
2019-06-28 16:04:14 +02:00
return C.GetTickCount()
2019-12-19 22:29:37 +01:00
} $else {
ts := C.timeval{}
2019-12-19 22:29:37 +01:00
C.gettimeofday(&ts, 0)
return i64(ts.tv_sec * u64(1000) + (ts.tv_usec / u64(1000)))
}
2020-02-06 14:19:44 +01:00
// t := i64(C.mach_absolute_time())
// # Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &t );
// # return (double)(* (uint64_t *) &elapsedNano) / 1000000;
2019-06-28 16:04:14 +02:00
}
2020-02-06 14:19:44 +01:00
// sleep makes the calling thread sleep for a given number of seconds.
2019-06-28 16:04:14 +02:00
pub fn sleep(seconds int) {
$if windows {
2019-11-16 00:30:50 +01:00
C.Sleep(seconds * 1000)
2019-12-19 22:29:37 +01:00
} $else {
2019-06-28 16:04:14 +02:00
C.sleep(seconds)
}
2019-06-28 16:04:14 +02:00
}
2020-02-06 14:19:44 +01:00
// sleep_ms makes the calling thread sleep for a given number of milliseconds.
pub fn sleep_ms(milliseconds int) {
2019-12-19 22:29:37 +01:00
$if windows {
C.Sleep(milliseconds)
2019-12-19 22:29:37 +01:00
} $else {
C.usleep(milliseconds * 1000)
2019-12-19 22:29:37 +01:00
}
2019-06-28 16:04:14 +02:00
}
2020-02-06 14:19:44 +01:00
// usleep makes the calling thread sleep for a given number of microseconds.
pub fn usleep(microseconds int) {
$if windows {
milliseconds := microseconds / 1000
C.Sleep(milliseconds)
2019-12-19 22:29:37 +01:00
} $else {
C.usleep(microseconds)
}
2019-06-28 16:04:14 +02:00
}
2019-07-03 18:55:07 +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.
2019-07-14 16:43:57 +02:00
pub fn days_in_month(month, year int) ?int {
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 }
res := month_days[month - 1] + extra
return res
2019-07-14 16:43:57 +02:00
}
2020-02-06 14:19:44 +01:00
// str returns time in the same format as `parse` expects ("YYYY-MM-DD HH:MM:SS").
2020-01-20 15:06:15 +01:00
pub fn (t Time) str() string {
2020-02-06 14:19:44 +01:00
// TODO Define common default format for
// `str` and `parse` and use it in both ways
2020-01-20 15:06:15 +01:00
return t.format_ss()
}
fn convert_ctime(t C.tm) Time {
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
unix: make_unix_time(t)
}
}