Skip to content

Commit

Permalink
time: update unix time acces, fix issues related to deviating unix ti…
Browse files Browse the repository at this point in the history
…mes (#21293)
  • Loading branch information
ttytm committed Apr 16, 2024
1 parent 9d117fc commit 1363cc8
Show file tree
Hide file tree
Showing 55 changed files with 251 additions and 193 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/macos_ci.yml
Expand Up @@ -82,7 +82,7 @@ jobs:
.github/workflows/retry.sh git clone --depth 1 https://github.com/vlang/ved
cd ved && ../v -o ved .
../v -autofree .
../v -prod .
# ../v -prod . # NOTE: temporary disabled due to deprecations. Enable after resolving github.com/vlang/ved/pull/181
cd ..
- name: Build V UI examples
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/v_apps_and_modules_compile_ci.yml
Expand Up @@ -74,7 +74,7 @@ jobs:
.github/workflows/retry.sh git clone --depth 1 https://github.com/vlang/ved
cd ved && ../v -o ved .
../v -autofree .
../v -prod .
# ../v -prod . # NOTE: temporary disabled due to deprecations. Enable after resolving github.com/vlang/ved/pull/181
cd ..
- name: Build vlang/pdf
Expand Down Expand Up @@ -124,8 +124,8 @@ jobs:
v install
echo "Build v-analyzer debug"
v build.vsh debug
echo "Build v-analyzer release"
v build.vsh release
# echo "Build v-analyzer release"
# v build.vsh release # NOTE: temporary disabled due to deprecations.
- name: Format vlang/v-analyzer
run: |
Expand Down
2 changes: 1 addition & 1 deletion doc/docs.md
Expand Up @@ -1590,7 +1590,7 @@ fn main() {
month: 12
day: 25
}
println(time.new_time(my_time).utc_string())
println(time.new(my_time).utc_string())
println('Century: ${my_time.century()}')
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/2048/2048.v
Expand Up @@ -733,7 +733,7 @@ fn (mut app App) handle_swipe() {
adx, ady := math.abs(dx), math.abs(dy)
dmin := if math.min(adx, ady) > 0 { math.min(adx, ady) } else { 1 }
dmax := if math.max(adx, ady) > 0 { math.max(adx, ady) } else { 1 }
tdiff := int(e.time.unix_time_milli() - s.time.unix_time_milli())
tdiff := int(e.time.unix_milli() - s.time.unix_milli())
// TODO: make this calculation more accurate (don't use arbitrary numbers)
min_swipe_distance := int(math.sqrt(math.min(w, h) * tdiff / 100)) + 20
if dmax < min_swipe_distance {
Expand Down
4 changes: 2 additions & 2 deletions vlib/context/onecontext/onecontext.v
Expand Up @@ -43,13 +43,13 @@ pub fn (octx OneContext) deadline() ?time.Time {

for ctx in octx.ctxs {
if deadline := ctx.deadline() {
if min.unix_time() == 0 || deadline < min {
if min.unix() == 0 || deadline < min {
min = deadline
}
}
}

if min.unix_time() == 0 {
if min.unix() == 0 {
return none
}

Expand Down
4 changes: 2 additions & 2 deletions vlib/context/onecontext/onecontext_test.v
Expand Up @@ -95,7 +95,7 @@ fn test_merge_deadline_context_1() {
mut ctx, _ := merge(ctx1, ctx2)

if deadline := ctx.deadline() {
assert deadline.unix_time() != 0
assert deadline.unix() != 0
} else {
assert false
}
Expand All @@ -111,7 +111,7 @@ fn test_merge_deadline_context_2() {
mut ctx, _ := merge(ctx1, ctx2)

if deadline := ctx.deadline() {
assert deadline.unix_time() != 0
assert deadline.unix() != 0
} else {
assert false
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/db/mysql/orm.c.v
Expand Up @@ -244,7 +244,7 @@ fn stmt_bind_primitive(mut stmt Stmt, data orm.Primitive) {
stmt.bind_text(data)
}
time.Time {
unix := int(data.unix)
unix := int(data.unix())
stmt_bind_primitive(mut stmt, unix)
}
orm.InfixType {
Expand Down
2 changes: 1 addition & 1 deletion vlib/db/sqlite/orm.v
Expand Up @@ -140,7 +140,7 @@ fn bind(stmt Stmt, c &int, data orm.Primitive) int {
err = stmt.bind_text(c, data)
}
time.Time {
err = stmt.bind_int(c, int(data.unix))
err = stmt.bind_int(c, int(data.unix()))
}
orm.InfixType {
err = bind(stmt, c, data.right)
Expand Down
6 changes: 3 additions & 3 deletions vlib/json/json_test.v
Expand Up @@ -95,7 +95,7 @@ fn test_encode_decode_sumtype() {
enc := json.encode(game)
// eprintln('Encoded Game: $enc')

assert enc == '{"title":"Super Mega Game","player":{"name":"Monke","_type":"Human"},"other":[{"tag":"Pen","_type":"Item"},{"tag":"Cookie","_type":"Item"},"cat","Stool",{"_type":"Time","value":${t.unix_time()}}]}'
assert enc == '{"title":"Super Mega Game","player":{"name":"Monke","_type":"Human"},"other":[{"tag":"Pen","_type":"Item"},{"tag":"Cookie","_type":"Item"},"cat","Stool",{"_type":"Time","value":${t.unix()}}]}'

dec := json.decode(SomeGame, enc)!
// eprintln('Decoded Game: $dec')
Expand All @@ -104,7 +104,7 @@ fn test_encode_decode_sumtype() {
assert game.player == dec.player
assert (game.other[2] as Animal) == .cat
assert dec.other[2] == Entity('cat')
assert (game.other[4] as time.Time).unix_time() == (dec.other[4] as time.Time).unix_time()
assert (game.other[4] as time.Time).unix() == (dec.other[4] as time.Time).unix()
}

fn bar[T](payload string) !Bar { // ?T doesn't work currently
Expand Down Expand Up @@ -156,7 +156,7 @@ fn test_parse_user() {
fn test_encode_decode_time() {
user := User2{
age: 25
reg_date: time.new_time(year: 2020, month: 12, day: 22, hour: 7, minute: 23)
reg_date: time.new(year: 2020, month: 12, day: 22, hour: 7, minute: 23)
}
s := json.encode(user)
// println(s)
Expand Down
6 changes: 2 additions & 4 deletions vlib/net/common.c.v
Expand Up @@ -4,9 +4,7 @@ import time

// no_deadline should be given to functions when no deadline is wanted (i.e. all functions
// return instantly)
const no_deadline = time.Time{
unix: 0
}
const no_deadline = time.unix(0)

// no_timeout should be given to functions when no timeout is wanted (i.e. all functions
// return instantly)
Expand Down Expand Up @@ -148,7 +146,7 @@ fn @select(handle int, test Select, timeout time.Duration) !bool {
@[inline]
fn select_deadline(handle int, test Select, deadline time.Time) !bool {
// if we have a 0 deadline here then the timeout that was passed was infinite...
infinite := deadline.unix_time() == 0
infinite := deadline.unix() == 0
for infinite || time.now() <= deadline {
timeout := if infinite { net.infinite_timeout } else { deadline - time.now() }
ready := @select(handle, test, timeout) or {
Expand Down
2 changes: 1 addition & 1 deletion vlib/net/openssl/ssl_connection.c.v
Expand Up @@ -120,7 +120,7 @@ fn (mut s SSLConn) init() ! {
mut cert := s.config.cert
mut cert_key := s.config.cert_key
if s.config.in_memory_verification {
now := time.now().unix.str()
now := time.now().unix().str()
verify = os.temp_dir() + '/v_verify' + now
cert = os.temp_dir() + '/v_cert' + now
cert_key = os.temp_dir() + '/v_cert_key' + now
Expand Down
6 changes: 3 additions & 3 deletions vlib/net/tcp.c.v
Expand Up @@ -180,7 +180,7 @@ pub fn (c TcpConn) read(mut buf []u8) !int {
}

pub fn (mut c TcpConn) read_deadline() !time.Time {
if c.read_deadline.unix == 0 {
if c.read_deadline.unix() == 0 {
return c.read_deadline
}
return error('none')
Expand Down Expand Up @@ -245,7 +245,7 @@ pub fn (mut c TcpConn) set_read_deadline(deadline time.Time) {
}

pub fn (mut c TcpConn) write_deadline() !time.Time {
if c.write_deadline.unix == 0 {
if c.write_deadline.unix() == 0 {
return c.write_deadline
}
return error('none')
Expand Down Expand Up @@ -448,7 +448,7 @@ pub fn (mut l TcpListener) accept_only() !&TcpConn {
}

pub fn (c &TcpListener) accept_deadline() !time.Time {
if c.accept_deadline.unix != 0 {
if c.accept_deadline.unix() != 0 {
return c.accept_deadline
}
return error('invalid deadline')
Expand Down
4 changes: 2 additions & 2 deletions vlib/net/udp.c.v
Expand Up @@ -122,7 +122,7 @@ pub fn (mut c UdpConn) read(mut buf []u8) !(int, Addr) {
}

pub fn (c &UdpConn) read_deadline() !time.Time {
if c.read_deadline.unix == 0 {
if c.read_deadline.unix() == 0 {
return c.read_deadline
}
return error('none')
Expand All @@ -133,7 +133,7 @@ pub fn (mut c UdpConn) set_read_deadline(deadline time.Time) {
}

pub fn (c &UdpConn) write_deadline() !time.Time {
if c.write_deadline.unix == 0 {
if c.write_deadline.unix() == 0 {
return c.write_deadline
}
return error('none')
Expand Down
6 changes: 2 additions & 4 deletions vlib/net/unix/common.c.v
Expand Up @@ -5,9 +5,7 @@ import net

// no_deadline should be given to functions when no deadline is wanted (i.e. all functions
// return instantly)
const no_deadline = time.Time{
unix: 0
}
const no_deadline = time.unix(0)
// no_timeout should be given to functions when no timeout is wanted (i.e. all functions
// return instantly)
const no_timeout = time.Duration(0)
Expand Down Expand Up @@ -71,7 +69,7 @@ fn @select(handle int, test Select, timeout time.Duration) !bool {
@[inline]
fn select_deadline(handle int, test Select, deadline time.Time) !bool {
// if we have a 0 deadline here then the timeout that was passed was infinite...
infinite := deadline.unix_time() == 0
infinite := deadline.unix() == 0
for infinite || time.now() <= deadline {
timeout := if infinite { net.infinite_timeout } else { deadline - time.now() }
ready := @select(handle, test, timeout) or {
Expand Down
6 changes: 3 additions & 3 deletions vlib/net/unix/stream.c.v
Expand Up @@ -152,7 +152,7 @@ pub fn (mut c StreamConn) read(mut buf []u8) !int {

// read_deadline returns the read deadline
pub fn (mut c StreamConn) read_deadline() !time.Time {
if c.read_deadline.unix == 0 {
if c.read_deadline.unix() == 0 {
return c.read_deadline
}
return error('none')
Expand All @@ -165,7 +165,7 @@ pub fn (mut c StreamConn) set_read_deadline(deadline time.Time) {

// write_deadline returns the write deadline
pub fn (mut c StreamConn) write_deadline() !time.Time {
if c.write_deadline.unix == 0 {
if c.write_deadline.unix() == 0 {
return c.write_deadline
}
return error('none')
Expand Down Expand Up @@ -297,7 +297,7 @@ pub fn (mut l StreamListener) accept() !&StreamConn {

// accept_deadline returns the deadline until a new client is accepted
pub fn (l &StreamListener) accept_deadline() !time.Time {
if l.accept_deadline.unix != 0 {
if l.accept_deadline.unix() != 0 {
return l.accept_deadline
}
return error('no deadline')
Expand Down
2 changes: 1 addition & 1 deletion vlib/net/websocket/websocket_client.v
Expand Up @@ -171,7 +171,7 @@ pub fn (mut ws Client) listen() ! {
}
.pong {
ws.debug_log('read: pong')
ws.last_pong_ut = time.now().unix
ws.last_pong_ut = time.now().unix()
ws.send_message_event(msg)
if msg.payload.len > 0 {
unsafe { msg.free() }
Expand Down
6 changes: 3 additions & 3 deletions vlib/net/websocket/websocket_server.v
Expand Up @@ -108,7 +108,7 @@ fn (mut s Server) handle_ping() {
}
clients_to_remove << c.client.id
}
if (time.now().unix - c.client.last_pong_ut) > s.get_ping_interval() * 2 {
if (time.now().unix() - c.client.last_pong_ut) > s.get_ping_interval() * 2 {
clients_to_remove << c.client.id
c.client.close(1000, 'no pong received') or { continue }
}
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn (mut s Server) handle_handshake(mut conn net.TcpConn, key string) !&Serve
client_state: ClientState{
state: .open
}
last_pong_ut: time.now().unix
last_pong_ut: time.now().unix()
id: rand.uuid_v4()
}
mut server_client := &ServerClient{
Expand Down Expand Up @@ -225,7 +225,7 @@ fn (mut s Server) accept_new_client() !&Client {
client_state: ClientState{
state: .open
}
last_pong_ut: time.now().unix
last_pong_ut: time.now().unix()
id: rand.uuid_v4()
}
return c
Expand Down
2 changes: 1 addition & 1 deletion vlib/orm/orm_test.v
Expand Up @@ -363,7 +363,7 @@ fn test_orm() {
}!

assert data.len == 1
assert tnow.unix == data[0].create.unix
assert tnow.unix() == data[0].create.unix()

mod := Module{}

Expand Down
4 changes: 2 additions & 2 deletions vlib/os/environment_test.v
Expand Up @@ -42,13 +42,13 @@ fn test_environ() {
}

fn test_setenv_var_not_exists() {
key := time.new_time(time.now()).unix
key := time.new(time.now()).unix()
os.setenv('foo${key}', 'bar', false)
assert os.getenv('foo${key}') == 'bar'
}

fn test_getenv_empty_var() {
key := time.new_time(time.now()).unix
key := time.new(time.now()).unix()
os.setenv('empty${key}', '""', false)
assert os.getenv('empty${key}') == '""'
}
10 changes: 5 additions & 5 deletions vlib/os/os_stat_test.v
Expand Up @@ -22,13 +22,13 @@ fn test_stat() {

mut fstat := os.stat(test_file)!
eprintln(@LOCATION)
eprintln(' | start_time: ${start_time.unix}\n | end_time: ${end_time.unix}\n | fstat.ctime: ${fstat.ctime}\n | fstat.mtime: ${fstat.mtime}')
eprintln(' | start_time: ${start_time.unix()}\n | end_time: ${end_time.unix()}\n | fstat.ctime: ${fstat.ctime}\n | fstat.mtime: ${fstat.mtime}')
assert fstat.get_filetype() == .regular
assert fstat.size == u64(test_content.len)
assert fstat.ctime >= start_time.unix
assert fstat.ctime <= end_time.unix
assert fstat.mtime >= start_time.unix
assert fstat.mtime <= end_time.unix
assert fstat.ctime >= start_time.unix()
assert fstat.ctime <= end_time.unix()
assert fstat.mtime >= start_time.unix()
assert fstat.mtime <= end_time.unix()

$if !windows {
os.chmod(test_file, 0o600)!
Expand Down
4 changes: 2 additions & 2 deletions vlib/os/os_test.c.v
Expand Up @@ -869,8 +869,8 @@ fn test_utime() {
os.rm(filename) or { panic(err) }
}
f.write_string(hello) or { panic(err) }
atime := time.now().add_days(2).unix_time()
mtime := time.now().add_days(4).unix_time()
atime := time.now().add_days(2).unix()
mtime := time.now().add_days(4).unix()
os.utime(filename, int(atime), int(mtime)) or { panic(err) }
assert os.file_last_mod_unix(filename) == mtime
}
Expand Down
6 changes: 3 additions & 3 deletions vlib/rand/rand.v
Expand Up @@ -323,10 +323,10 @@ pub fn (mut rng PRNG) f64_in_range(min f64, max f64) !f64 {
// users or business transactions.
// (https://news.ycombinator.com/item?id=14526173)
pub fn (mut rng PRNG) ulid() string {
return internal_ulid_at_millisecond(mut rng, u64(time.utc().unix_time_milli()))
return internal_ulid_at_millisecond(mut rng, u64(time.utc().unix_milli()))
}

// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_milli`.
pub fn (mut rng PRNG) ulid_at_millisecond(unix_time_milli u64) string {
return internal_ulid_at_millisecond(mut rng, unix_time_milli)
}
Expand Down Expand Up @@ -671,7 +671,7 @@ pub fn ulid() string {
return default_rng.ulid()
}

// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_milli`.
pub fn ulid_at_millisecond(unix_time_milli u64) string {
return default_rng.ulid_at_millisecond(unix_time_milli)
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/rand/random_identifiers_test.v
Expand Up @@ -41,7 +41,7 @@ fn test_ulids_max_start_character_is_ok() {
}

fn test_ulids_generated_in_the_same_millisecond_have_the_same_prefix() {
t := u64(time.utc().unix_time_milli())
t := u64(time.utc().unix_milli())
mut ulid1 := ''
mut ulid2 := ''
mut ulid3 := ''
Expand Down
2 changes: 1 addition & 1 deletion vlib/time/README.md
Expand Up @@ -53,7 +53,7 @@ import time
s := '2018-01-27 12:48:34'
t := time.parse(s) or { panic('failing format: ${s} | err: ${err}') }
println(t)
println(t.unix)
println(t.unix())
```

V's time module also has these parse methods:
Expand Down
2 changes: 1 addition & 1 deletion vlib/time/date_time_parser.v
Expand Up @@ -304,7 +304,7 @@ fn (mut p DateTimeParser) parse() !Time {
return error_invalid_time(0, '${month_name} has only 30 days')
}

return new_time(
return new(
year: year_
month: month_
day: day_in_month
Expand Down

0 comments on commit 1363cc8

Please sign in to comment.