1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/time/time.v

486 lines
11 KiB
V
Raw Normal View History

2019-06-23 05:21:30 +03: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 21:20:28 +03:00
module time
2019-07-14 17:43:57 +03:00
const (
2019-12-20 00:29:37 +03: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.
absolute_zero_year = i64(-292277022399)
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 17:43:57 +03:00
)
#include <time.h>
2019-10-25 22:04:25 +03:00
pub struct Time {
pub:
year int
month int
day int
hour int
minute int
second int
unix int
2019-06-22 21:20:28 +03:00
}
pub enum FormatTime {
2019-12-20 00:29:37 +03:00
hhmm12
hhmm24
hhmmss12
hhmmss24
no_time
}
pub enum FormatDate {
2019-12-20 00:29:37 +03:00
ddmmyy
ddmmyyyy
mmddyy
mmddyyyy
mmmd
mmmdd
mmmddyyyy
no_date
yyyymmdd
}
pub enum FormatDelimiter {
2019-12-20 00:29:37 +03:00
dot
hyphen
slash
space
}
2019-07-14 17:43:57 +03:00
2019-09-01 22:51:16 +03:00
fn C.localtime(int) &C.tm
2019-12-20 00:29:37 +03:00
pub struct C.time_t {}
2019-10-07 01:31:01 +03:00
fn C.time(int) C.time_t
2019-09-15 19:07:40 +03:00
2019-12-20 00:29:37 +03:00
pub fn now() Time {
t := C.time(0)
2019-12-04 12:19:32 +03:00
mut now := &C.tm(0)
now = C.localtime(&t)
return convert_ctime(now)
}
// format_ss returns a string for t in a given format YYYY-MM-DD HH:MM:SS in
2019-12-20 00:29:37 +03:00
// 24h notation
// @param
// @return string
// @example 1980-07-11 21:23:42
2019-06-28 17:04:14 +03:00
pub fn (t Time) format_ss() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_str(.hyphen, .hhmmss24, .yyyymmdd)
}
// format_ss returns a string for t in a given format YYYY-MM-DD HH:MM in 24h
2019-12-20 00:29:37 +03:00
// notation
// @param
// @return string
// @example 1980-07-11 21:23
pub fn (t Time) format() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_str(.hyphen, .hhmm24, .yyyymmdd)
}
pub fn (t Time) smonth() string {
i := t.month - 1
return months_string[i * 3..(i + 1) * 3]
}
// hhmm returns a string for t in the given format HH:MM in 24h notation
// @example 21:04
pub fn (t Time) hhmm() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_time_str(.hhmm24)
}
/*
fn (t Time) hhmm_tmp() string {
return '${t.hour:02d}:${t.minute:02d}'
}
*/
// hhmm12 returns a string for t in the given format HH:MM in 12h notation
pub fn (t Time) hhmm12() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_time_str(.hhmm12)
2019-06-22 21:20:28 +03:00
}
// hhmmss returns a string for t in the given format HH:MM:SS in 24h notation
2019-06-28 17:04:14 +03:00
pub fn (t Time) hhmmss() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_time_str(.hhmmss24)
2019-06-22 21:20:28 +03:00
}
// ymmdd returns a string for t in the given format YYYY-MM-DD
2019-06-28 17:04:14 +03:00
pub fn (t Time) ymmdd() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_date_str(.hyphen, .yyyymmdd)
2019-06-22 21:20:28 +03:00
}
2019-06-27 13:02:47 +03:00
// ddmmy returns a string for t in the given format DD.MM.YYYY
pub fn (t Time) ddmmy() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_date_str(.dot, .ddmmyyyy)
}
// md returns a string for t in the given format MMM D
2019-06-28 17:04:14 +03:00
pub fn (t Time) md() string {
2019-12-20 00:29:37 +03:00
return t.get_fmt_date_str(.space, .mmmd)
2019-06-22 21:20:28 +03:00
}
pub fn (t Time) clean() string {
nowe := time.now()
// if amtime {
// hm = t.Format("3:04 pm")
// }
// Today
if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day {
2019-12-20 00:29:37 +03:00
return t.get_fmt_time_str(.hhmm24)
}
// This week
// if time.Since(t) < 24*7*time.Hour {
// return t.Weekday().String()[:3] + " " + hm
// }
// This year
if t.year == nowe.year {
2019-12-20 00:29:37 +03:00
return t.get_fmt_str(.space, .hhmm24, .mmmd)
}
return t.format()
// return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm
2019-06-22 21:20:28 +03:00
}
2019-06-28 17:04:14 +03:00
pub fn (t Time) clean12() string {
nowe := time.now()
// if amtime {
// hm = t.Format("3:04 pm")
// }
// Today
if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day {
2019-12-20 00:29:37 +03:00
return t.get_fmt_time_str(.hhmm12)
}
// This week
// if time.Since(t) < 24*7*time.Hour {
// return t.Weekday().String()[:3] + " " + hm
// }
// This year
if t.year == nowe.year {
2019-12-20 00:29:37 +03:00
return t.get_fmt_str(.space, .hhmm12, .mmmd)
}
return t.format()
// return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm
2019-06-22 21:20:28 +03:00
}
// `parse` parses time in the following format: "2018-01-27 12:48:34"
pub fn parse(s string) Time {
// println('parse="$s"')
pos := s.index(' ') or {
println('bad time format')
return now()
}
symd := s[..pos]
ymd := symd.split('-')
if ymd.len != 3 {
println('bad time format')
return now()
}
shms := s[pos..]
hms := shms.split(':')
2019-12-23 13:25:44 +03:00
hour := hms[0][1..]
minute := hms[1]
second := hms[2]
// //////////
2019-12-20 00:29:37 +03:00
return new_time(Time{
year: ymd[0].int()
month: ymd[1].int()
day: ymd[2].int()
hour: hour.int()
minute: minute.int()
second: second.int()
})
2019-06-22 21:20:28 +03:00
}
2019-12-23 13:36:51 +03:00
// `parse_iso` parses time in the following format: "Thu, 12 Dec 2019 06:07:45 GMT"
pub fn parse_iso(s string) Time {
fields := s.split(' ')
if fields.len < 5 {
return Time{}
}
pos := months_string.index(fields[2]) or {
return Time{}
}
mm := pos / 3 + 1
tmstr := malloc(s.len * 2)
count := int(C.sprintf(charptr(tmstr), '%s-%02d-%s %s'.str, fields[3].str, mm, fields[1].str, fields[4].str))
2019-12-23 13:36:51 +03:00
return parse(tos(tmstr, count))
}
2019-06-28 17:04:14 +03:00
pub fn new_time(t Time) Time {
2019-12-31 19:11:47 +03:00
return Time{
year: t.year
month: t.month
day: t.day
hour: t.hour
minute: t.minute
second: t.second
2019-12-31 19:11:47 +03:00
unix: t.calc_unix()
}
// TODO: Use the syntax below when it works with reserved keywords like `unix`
2019-12-31 19:11:47 +03:00
/*
2019-12-20 00:29:37 +03:00
return {
t |
2019-12-31 19:11:47 +03:00
unix:t.calc_unix()
2019-12-20 00:29:37 +03:00
}
2019-12-31 19:11:47 +03:00
*/
2019-06-22 21:20:28 +03:00
}
2019-06-28 17:04:14 +03:00
pub fn (t &Time) calc_unix() int {
2019-12-31 19:11:47 +03:00
if t.unix != 0 {
return t.unix
2019-06-22 21:20:28 +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
}
return C.mktime(&tt)
}
// TODO add(d time.Duration)
pub fn (t Time) add_seconds(seconds int) Time {
2019-12-31 19:11:47 +03:00
return unix(t.unix + seconds)
}
2019-11-11 17:18:32 +03:00
pub fn (t Time) add_days(days int) Time {
2019-12-31 19:11:47 +03:00
return unix(t.unix + days * 3600 * 24)
2019-11-11 17:18:32 +03:00
}
// TODO use time.Duration instead of seconds
fn since(t Time) int {
return 0
}
pub fn (t Time) relative() string {
now := time.now()
2019-12-31 19:11:47 +03: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 21:20:28 +03:00
}
if secs < 3600 {
return '${secs/60}m'
2019-06-22 21:20:28 +03:00
}
if secs < 3600 * 24 {
return '${secs/3600}h'
2019-06-22 21:20:28 +03:00
}
if secs < 3600 * 24 * 5 {
return '${secs/3600/24}d'
}
if secs > 3600 * 24 * 10000 {
return ''
}
return t.md()
}
2019-06-28 17:04:14 +03:00
pub fn day_of_week(y, m, d int) int {
2019-08-30 01:05:58 +03: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 01:05:58 +03:00
mut sy := y
if (m < 3) {
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
}
pub fn (t Time) day_of_week() int {
return day_of_week(t.year, t.month, t.day)
}
// weekday_str() returns the current day in string (upto 3 characters)
pub fn (t Time) weekday_str() string {
i := t.day_of_week() - 1
return days_string[i * 3..(i + 1) * 3]
2019-06-23 22:18:24 +03:00
}
2019-06-28 17:04:14 +03:00
2019-12-20 00:29:37 +03:00
pub struct C.timeval {
tv_sec u64
tv_usec u64
}
2019-06-30 15:35:26 +03:00
2019-06-28 17:04:14 +03:00
// in ms
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 {
ts := C.timeval{}
2019-12-20 00:29:37 +03:00
C.gettimeofday(&ts, 0)
return i64(ts.tv_sec * u64(1000) + (ts.tv_usec / u64(1000)))
}
2019-12-20 00:29:37 +03:00
/*
2019-06-28 17:04:14 +03:00
t := i64(C.mach_absolute_time())
# Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &t );
# return (double)(* (uint64_t *) &elapsedNano) / 1000000;
*/
2019-12-20 00:29:37 +03:00
2019-06-28 17:04:14 +03:00
}
pub fn sleep(seconds int) {
$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-06-28 17:04:14 +03:00
}
pub fn sleep_ms(milliseconds int) {
2019-12-20 00:29:37 +03:00
$if windows {
C.Sleep(milliseconds)
2019-12-20 00:29:37 +03:00
} $else {
C.usleep(milliseconds * 1000)
2019-12-20 00:29:37 +03:00
}
2019-06-28 17:04:14 +03:00
}
pub fn usleep(microseconds int) {
$if windows {
milliseconds := microseconds / 1000
C.Sleep(milliseconds)
2019-12-20 00:29:37 +03:00
} $else {
C.usleep(microseconds)
}
2019-06-28 17:04:14 +03:00
}
2019-07-03 19:55:07 +03:00
// Determine whether a year is a leap year.
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
}
// Returns number of days in month
pub fn days_in_month(month, year int) ?int {
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
return res
2019-07-14 17:43:57 +03:00
}
// get_fmt_time_str returns a string for time t in a given format
// @param FormatTime
// @return string
// @example 21:23:42
pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string {
2019-12-20 00:29:37 +03:00
if fmt_time == .no_time {
return ''
}
tp := if t.hour > 11 { 'p.m.' } else { 'a.m.' }
hour := if t.hour > 12 { t.hour - 12 } else if t.hour == 0 { 12 } else { t.hour }
return match fmt_time {
.hhmm12{
'$hour:${t.minute:02d} $tp'
}
.hhmm24{
'${t.hour:02d}:${t.minute:02d}'
}
.hhmmss12{
'$hour:${t.minute:02d}:${t.second:02d} $tp'
}
.hhmmss24{
'${t.hour:02d}:${t.minute:02d}:${t.second:02d}'
}
else {
'unknown enumeration $fmt_time'}
}
}
// get_fmt_date_str returns a string for t in a given date format
// @param FormatDelimiter, FormatDate
// @return string
// @example 11.07.1980
pub fn (t Time) get_fmt_date_str(fmt_dlmtr FormatDelimiter, fmt_date FormatDate) string {
2019-12-20 00:29:37 +03:00
if fmt_date == .no_date {
return ''
}
month := '${t.smonth()}'
year := t.year.str()[2..]
return match fmt_date {
.ddmmyy{
'${t.day:02d}|${t.month:02d}|$year'
}
.ddmmyyyy{
'${t.day:02d}|${t.month:02d}|${t.year}'
}
.mmddyy{
'${t.month:02d}|${t.day:02d}|$year'
}
.mmddyyyy{
'${t.month:02d}|${t.day:02d}|${t.year}'
}
.mmmd{
'$month|${t.day}'
}
.mmmdd{
'$month|${t.day:02d}'
}
.mmmddyyyy{
'$month|${t.day:02d}|${t.year}'
}
.yyyymmdd{
'${t.year}|${t.month:02d}|${t.day:02d}'
}
else {
'unknown enumeration $fmt_date'}}.replace('|', match fmt_dlmtr {
.dot{
'.'
}
.hyphen{
'-'
}
.slash{
'/'
}
.space{
' '
}
else {
'unknown enumeration $fmt_dlmtr'}})
}
// get_fmt_str returns a string for t in a given format for time and date
// @param FormatDelimiter, FormatTime, FormatDate
// @return string
// @example 11.07.1980 21:23:42
pub fn (t Time) get_fmt_str(fmt_dlmtr FormatDelimiter, fmt_time FormatTime, fmt_date FormatDate) string {
2019-12-20 00:29:37 +03:00
if fmt_date == .no_date {
if fmt_time == .no_time {
// saving one function call although it's checked in
// t.get_fmt_time_str(fmt_time) in the beginning
return ''
}
else {
return t.get_fmt_time_str(fmt_time)
}
}
else {
if fmt_time != .no_time {
return t.get_fmt_date_str(fmt_dlmtr, fmt_date) + ' ' + t.get_fmt_time_str(fmt_time)
}
else {
return t.get_fmt_date_str(fmt_dlmtr, fmt_date)
}
}
}