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

all: switch to the new fn arg syntax everywhere; add lots of vfmt -verify tests

This commit is contained in:
Alexander Medvednikov
2020-10-15 12:32:28 +02:00
parent 982056894e
commit 7da1afa140
37 changed files with 382 additions and 404 deletions

View File

@ -4,23 +4,22 @@
module time
#include <time.h>
const (
days_string = 'MonTueWedThuFriSatSun'
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec'
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 )//as i64
absolute_zero_year = i64(-292277022399) // as i64
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
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 = [
days_per_4_years = 365 * 4 + 1
days_before = [
0,
31,
31 + 28,
@ -35,19 +34,19 @@ const (
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31,
]
long_days= ['Monday', 'Tuesday', 'Wednesday', 'Thusday', 'Friday', 'Saturday', 'Sunday']
long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thusday', 'Friday', 'Saturday', 'Sunday']
)
pub struct Time {
pub:
year int
month int
day int
hour int
minute int
second int
microsecond int
unix u64
year int
month int
day int
hour int
minute int
second int
microsecond int
unix u64
}
pub enum FormatTime {
@ -86,8 +85,8 @@ pub struct C.timeval {
}
fn C.localtime(t &C.time_t) &C.tm
fn C.time(t &C.time_t) C.time_t
fn C.time(t &C.time_t) C.time_t
// now returns current local time.
pub fn now() Time {
@ -154,7 +153,10 @@ pub fn new_time(t Time) Time {
tm_year: t.year - 1900
}
utime := u64(make_unix_time(tt))
return { t | unix: utime }
return {
t |
unix: utime
}
}
// unix_time returns Unix time.
@ -166,7 +168,7 @@ pub fn (t Time) unix_time() int {
// unix_time_milli returns Unix time with millisecond resolution.
[inline]
pub fn (t Time) unix_time_milli() u64 {
return t.unix * 1000 + u64(t.microsecond/1000)
return t.unix * 1000 + u64(t.microsecond / 1000)
}
// add_seconds returns a new time struct with an added number of seconds.
@ -189,7 +191,7 @@ fn since(t Time) int {
// relative returns a string representation of difference between time
// and current time.
pub fn (t Time) relative() string {
znow := time.now()
znow := now()
secs := znow.unix - t.unix
if secs <= 30 {
// right now or in the future
@ -200,21 +202,21 @@ pub fn (t Time) relative() string {
return '1m'
}
if secs < 3600 {
m := secs/60
m := secs / 60
if m == 1 {
return '1 minute ago'
}
return '$m minutes ago'
}
if secs < 3600 * 24 {
h := secs/3600
h := secs / 3600
if h == 1 {
return '1 hour ago'
}
return '$h hours ago'
}
if secs < 3600 * 24 * 5 {
d:=secs/3600/24
d := secs / 3600 / 24
if d == 1 {
return '1 day ago'
}
@ -227,7 +229,7 @@ pub fn (t Time) relative() string {
}
pub fn (t Time) relative_short() string {
znow := time.now()
znow := now()
secs := znow.unix - t.unix
if secs <= 30 {
// right now or in the future
@ -254,7 +256,7 @@ pub fn (t Time) relative_short() string {
// day_of_week returns the current day of a given year, month, and day,
// as an integer.
pub fn day_of_week(y, m, d int) int {
pub fn day_of_week(y int, m int, d int) int {
// Sakomotho's algorithm is explained here:
// https://stackoverflow.com/a/6385934
t := [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
@ -330,7 +332,7 @@ pub fn is_leap_year(year int) bool {
}
// days_in_month returns a number of days in a given month.
pub fn days_in_month(month, year int) ?int {
pub fn days_in_month(month int, year int) ?int {
if month > 12 || month < 1 {
return error('Invalid month: $month')
}
@ -362,7 +364,7 @@ fn convert_ctime(t C.tm, microsecond int) Time {
// A lot of these are taken from the Go library
pub type Duration = i64
pub const(
pub const (
nanosecond = Duration(1)
microsecond = Duration(1000) * nanosecond
millisecond = Duration(1000) * microsecond
@ -373,34 +375,39 @@ pub const(
)
// nanoseconds returns the duration as an integer number of nanoseconds.
pub fn (d Duration) nanoseconds() i64 { return i64(d) }
pub fn (d Duration) nanoseconds() i64 {
return i64(d)
}
// microseconds returns the duration as an integer number of microseconds.
pub fn (d Duration) microseconds() i64 { return i64(d) / 1000 }
pub fn (d Duration) microseconds() i64 {
return i64(d) / 1000
}
// milliseconds returns the duration as an integer number of milliseconds.
pub fn (d Duration) milliseconds() i64 { return i64(d) / 1_000_000 }
pub fn (d Duration) milliseconds() i64 {
return i64(d) / 1000000
}
// 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
return f64(sec) + f64(nsec)/1e9
return f64(sec) + f64(nsec) / 1e9
}
// minutes returns the duration as a floating point number of minutes.
pub fn (d Duration) minutes() f64 {
min := d / minute
nsec := d % minute
return f64(min) + f64(nsec)/(60*1e9)
return f64(min) + f64(nsec) / (60 * 1e9)
}
// hours returns the duration as a floating point number of hours.
pub fn (d Duration) hours() f64 {
hr := d / hour
nsec := d % hour
return f64(hr) + f64(nsec)/(60*60*1e9)
return f64(hr) + f64(nsec) / (60 * 60 * 1e9)
}