1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov
2019-12-20 00:29:37 +03:00
parent b6fe2ebc0b
commit 6210984c97
54 changed files with 1757 additions and 1993 deletions

View File

@@ -1,49 +1,29 @@
// 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.
module time
import rand
const (
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,
]
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, ]
)
#include <time.h>
pub struct Time {
pub:
year int
@@ -56,38 +36,41 @@ pub:
}
pub enum FormatTime {
hhmm12
hhmm24
hhmmss12
hhmmss24
no_time
hhmm12
hhmm24
hhmmss12
hhmmss24
no_time
}
pub enum FormatDate {
ddmmyy
ddmmyyyy
mmddyy
mmddyyyy
mmmd
mmmdd
mmmddyyyy
no_date
yyyymmdd
ddmmyy
ddmmyyyy
mmddyy
mmddyyyy
mmmd
mmmdd
mmmddyyyy
no_date
yyyymmdd
}
pub enum FormatDelimiter {
dot
hyphen
slash
space
dot
hyphen
slash
space
}
fn C.localtime(int) &C.tm
fn remove_me_when_c_bug_is_fixed() { // TODO
fn remove_me_when_c_bug_is_fixed() {
// TODO
}
pub struct C.time_t {}
pub struct C.time_t {
}
struct C.tm {
tm_year int
@@ -100,6 +83,7 @@ struct C.tm {
fn C.time(int) C.time_t
pub fn now() Time {
t := C.time(0)
mut now := &C.tm(0)
@@ -113,82 +97,81 @@ pub fn random() Time {
return time.unix(rand_unix)
}
// Based on Go's time package.
// Copyright 2009 The Go Authors.
pub fn unix(abs int) Time {
// Split into time and day.
mut d := abs / seconds_per_day
// Account for 400 year cycles.
mut n := d / days_per_400_years
mut y := 400 * n
d -= days_per_400_years * n
// Cut off 100-year cycles.
// The last cycle has one extra leap year, so on the last day
// of that year, day / days_per_100_years will be 4 instead of 3.
// Cut it back down to 3 by subtracting n>>2.
n = d / days_per_100_years
n -= n >> 2
n -= n>>2
y += 100 * n
d -= days_per_100_years * n
// Cut off 4-year cycles.
// The last cycle has a missing leap year, which does not
// affect the computation.
n = d / days_per_4_years
y += 4 * n
d -= days_per_4_years * n
// Cut off years within a 4-year cycle.
// The last year is a leap year, so on the last day of that year,
// day / 365 will be 4 instead of 3. Cut it back down to 3
// by subtracting n>>2.
n = d / 365
n -= n >> 2
n -= n>>2
y += n
d -= 365 * n
yday := d
mut day := yday
year := abs / int(3.154e+7) + 1970 //int(i64(y) + absolute_zero_year)
hour := (abs%seconds_per_day) / seconds_per_hour
year := abs / int(3.154e+7) + 1970 // int(i64(y) + absolute_zero_year)
hour := (abs % seconds_per_day) / seconds_per_hour
minute := (abs % seconds_per_hour) / seconds_per_minute
second := (abs % seconds_per_minute)
if is_leap_year(year) {
// Leap year
if day > 31+29-1 {
if day > 31 + 29 - 1 {
// After leap day; pretend it wasn't there.
day--
} else if day == 31+29-1 {
}
else if day == 31 + 29 - 1 {
// Leap day.
day = 29
return Time{year:year, month:2, day:day, hour:hour, minute: minute, second: second}
return Time{
year: year
month: 2
day: day
hour: hour
minute: minute
second: second
}
}
}
// Estimate month on assumption that every month has 31 days.
// The estimate may be too low by at most one month, so adjust.
mut month := day / 31
mut begin := 0
end := (days_before[month+1])
end := (days_before[month + 1])
if day >= end {
month++
begin = end
} else {
}
else {
begin = (days_before[month])
}
month++ // because January is 1
day = day - begin + 1
return Time{
year:year
year: year
month: month
day:day
hour:hour
day: day
hour: hour
minute: minute
second: second
uni: abs
@@ -196,7 +179,7 @@ pub fn unix(abs int) Time {
}
pub fn convert_ctime(t tm) Time {
return Time {
return Time{
year: t.tm_year + 1900
month: t.tm_mon + 1
day: t.tm_mday
@@ -208,24 +191,23 @@ pub fn convert_ctime(t tm) Time {
}
// format_ss returns a string for t in a given format YYYY-MM-DD HH:MM:SS in
// 24h notation
// 24h notation
// @param
// @return string
// @example 1980-07-11 21:23:42
pub fn (t Time) format_ss() string {
return t.get_fmt_str(.hyphen, .hhmmss24, .yyyymmdd)
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
// notation
// notation
// @param
// @return string
// @example 1980-07-11 21:23
pub fn (t Time) format() string {
return t.get_fmt_str(.hyphen, .hhmm24, .yyyymmdd)
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]
@@ -234,7 +216,7 @@ pub fn (t Time) smonth() string {
// hhmm returns a string for t in the given format HH:MM in 24h notation
// @example 21:04
pub fn (t Time) hhmm() string {
return t.get_fmt_time_str(.hhmm24)
return t.get_fmt_time_str(.hhmm24)
}
/*
@@ -245,27 +227,27 @@ fn (t Time) hhmm_tmp() string {
// hhmm12 returns a string for t in the given format HH:MM in 12h notation
pub fn (t Time) hhmm12() string {
return t.get_fmt_time_str(.hhmm12)
return t.get_fmt_time_str(.hhmm12)
}
// hhmmss returns a string for t in the given format HH:MM:SS in 24h notation
pub fn (t Time) hhmmss() string {
return t.get_fmt_time_str(.hhmmss24)
return t.get_fmt_time_str(.hhmmss24)
}
// ymmdd returns a string for t in the given format YYYY-MM-DD
pub fn (t Time) ymmdd() string {
return t.get_fmt_date_str(.hyphen, .yyyymmdd)
return t.get_fmt_date_str(.hyphen, .yyyymmdd)
}
// ddmmy returns a string for t in the given format DD.MM.YYYY
pub fn (t Time) ddmmy() string {
return t.get_fmt_date_str(.dot, .ddmmyyyy)
return t.get_fmt_date_str(.dot, .ddmmyyyy)
}
// md returns a string for t in the given format MMM D
pub fn (t Time) md() string {
return t.get_fmt_date_str(.space, .mmmd)
return t.get_fmt_date_str(.space, .mmmd)
}
pub fn (t Time) clean() string {
@@ -275,7 +257,7 @@ pub fn (t Time) clean() string {
// }
// Today
if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day {
return t.get_fmt_time_str(.hhmm24)
return t.get_fmt_time_str(.hhmm24)
}
// This week
// if time.Since(t) < 24*7*time.Hour {
@@ -283,7 +265,7 @@ pub fn (t Time) clean() string {
// }
// This year
if t.year == nowe.year {
return t.get_fmt_str(.space, .hhmm24, .mmmd)
return t.get_fmt_str(.space, .hhmm24, .mmmd)
}
return t.format()
// return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm
@@ -296,7 +278,7 @@ pub fn (t Time) clean12() string {
// }
// Today
if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day {
return t.get_fmt_time_str(.hhmm12)
return t.get_fmt_time_str(.hhmm12)
}
// This week
// if time.Since(t) < 24*7*time.Hour {
@@ -304,12 +286,11 @@ pub fn (t Time) clean12() string {
// }
// This year
if t.year == nowe.year {
return t.get_fmt_str(.space, .hhmm12, .mmmd)
return t.get_fmt_str(.space, .hhmm12, .mmmd)
}
return t.format()
// return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm
}
// `parse` parses time in the following format: "2018-01-27 12:48:34"
pub fn parse(s string) Time {
// println('parse="$s"')
@@ -329,7 +310,7 @@ pub fn parse(s string) Time {
minute := hms[1]
second := hms[2]
// //////////
return new_time(Time {
return new_time(Time{
year: ymd[0].int()
month: ymd[1].int()
day: ymd[2].int()
@@ -340,20 +321,23 @@ pub fn parse(s string) Time {
}
pub fn new_time(t Time) Time {
return{t | uni: t.calc_unix()}
return {
t |
uni:t.calc_unix()
}
}
pub fn (t &Time) calc_unix() int {
if t.uni != 0 {
if t.uni != 0 {
return t.uni
}
tt := C.tm{
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
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
}
return C.mktime(&tt)
}
@@ -406,7 +390,7 @@ pub fn day_of_week(y, m, d int) int {
if (m < 3) {
sy = sy - 1
}
return ( sy + sy/4 - sy/100 + sy/400 + t[m-1] + d - 1) % 7 + 1
return (sy + sy / 4 - sy / 100 + sy / 400 + t[m - 1] + d - 1) % 7 + 1
}
pub fn (t Time) day_of_week() int {
@@ -419,8 +403,8 @@ pub fn (t Time) weekday_str() string {
return days_string[i * 3..(i + 1) * 3]
}
pub struct C.timeval {
tv_sec u64
pub struct C.timeval {
tv_sec u64
tv_usec u64
}
@@ -428,50 +412,47 @@ pub struct C.timeval {
pub fn ticks() i64 {
$if windows {
return C.GetTickCount()
}
$else {
ts := C.timeval{}
C.gettimeofday(&ts,0)
} $else {
ts := C.timeval{
}
C.gettimeofday(&ts, 0)
return i64(ts.tv_sec * u64(1000) + (ts.tv_usec / u64(1000)))
}
/*
/*
t := i64(C.mach_absolute_time())
# Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &t );
# return (double)(* (uint64_t *) &elapsedNano) / 1000000;
*/
}
pub fn sleep(seconds int) {
$if windows {
C.Sleep(seconds * 1000)
}
$else {
} $else {
C.sleep(seconds)
}
}
pub fn usleep(n int) {
$if windows {
//C._usleep(n)
}
$else {
C.usleep(n)
}
$if windows {
// C._usleep(n)
} $else {
C.usleep(n)
}
}
pub fn sleep_ms(n int) {
$if windows {
C.Sleep(n)
}
$else {
} $else {
C.usleep(n * 1000)
}
}
// Determine whether a year is a leap year.
pub fn is_leap_year(year int) bool {
return (year%4 == 0) && (year%100 != 0 || year%400 == 0)
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)
}
// Returns number of days in month
@@ -479,8 +460,8 @@ pub fn days_in_month(month, year int) ?int {
if month > 12 || month < 1 {
return error('Invalid month: $month')
}
extra := if month == 2 && is_leap_year(year) {1} else {0}
res := month_days[month-1] + extra
extra := if month == 2 && is_leap_year(year) { 1 } else { 0 }
res := month_days[month - 1] + extra
return res
}
@@ -489,31 +470,26 @@ pub fn days_in_month(month, year int) ?int {
// @return string
// @example 21:23:42
pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string {
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' }
}
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
@@ -521,30 +497,52 @@ pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string {
// @return string
// @example 11.07.1980
pub fn (t Time) get_fmt_date_str(fmt_dlmtr FormatDelimiter, fmt_date FormatDate) string {
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' }
})
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
@@ -552,21 +550,23 @@ pub fn (t Time) get_fmt_date_str(fmt_dlmtr FormatDelimiter, fmt_date 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 {
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)
}
}
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)
}
}
}