Skip to content

time module: Return value of sys_mono_now is not the counter value. #26265

@quaesitor-scientiam

Description

@quaesitor-scientiam

Describe the bug

Current return value is not the actual value but a calculated value that leads to rollover causing negative time values. Examination of the various platforms and backends show inconsistent handling. I can show each example here but not sure if that is needed.

Reproduction Steps

import time


wait_times := [
	5*time.minute + 52*time.second,
	3*time.minute + 14*time.second,
	44*time.second,
	26*time.second,
	1*time.minute + 58*time.second,
	4*time.second,
	4*time.minute + 27*time.second,
	3*time.minute + 24*time.second,
	5*time.minute + 3*time.second,
	1*time.minute + 56*time.second,
	1*time.minute + 8*time.second,
	12*time.second,
	10*time.minute + 8*time.second,
]
mut wt := 0
mut sw := time.new_stopwatch()
for {
	println('SW: ${sw}')
	time.sleep(wait_times[wt])
	wt++
	sw.stop()
	if sw.end < sw.start {
		println('Negative: ${sw}')
		break
	}
	println('Elapsed: ${sw.elapsed()}')
	if wt == 13 {
		println('Success')
		break}

	sw.start()
}

Expected Behavior

No rollover of sys_mono_now values causing negative elapsed values

Current Behavior

Start SW: time.StopWatch{
    elapsed: 0
    start: 323682500
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 323682500
    end: 373885451300
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 373885759500
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 373885759500
    end: 569315427200
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 569315652400
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 569315652400
    end: 606243234400
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 606243434100
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 606243434100
    end: 627761309800
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 627761517700
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 627761517700
    end: 719225597500
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 719225799200
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 719225799200
    end: 722222679800
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 722222906800
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 722222906800
    end: 989538689700
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 989538914700
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 989538914700
    end: 1133303427200
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 1133303712700
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 1133303712700
    end: 1453101667500
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 1453101896700
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 1453101896700
    end: 1530729617000
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 1530729890300
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 1530729890300
    end: 1578334624700
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 1578334857700
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 1578334857700
    end: 1587515723400
}
Start SW: time.StopWatch{
    elapsed: 0
    start: 1587515949300
    end: 0
}
Stopped SW: time.StopWatch{
    elapsed: 0
    start: 1587515949300
    end: 187768837029
}

The last one generated negative elapsed time

Possible Solution

This needs a discussion because fixing to return the actual value has ramifications for whole lot of V I think. I have a working solution but have not tested the ecosystem of V.

Additional Information/Context

No response

V version

V 0.5.0 b9e9192

Environment details (OS name and version, etc.)

|V full version      |V 0.5.0 b9e9192b2b229c82b097911e2f64ea420a1c6c68
|:-------------------|:-------------------
|OS                  |windows, Microsoft Windows 11 Pro 26200 64-bit
|Processor           |24 cpus, 64bit, little endian, AMD Ryzen 9 5900X 12-Core Processor
|Memory              |72.83GB/127.92GB
|                    |
|V executable        |S:\repo\vlang2\v.exe
|V last modified time|2026-01-05 04:56:05
|                    |
|V home dir          |OK, value: S:\repo\vlang2
|VMODULES            |OK, value: C:\Users\john3\.vmodules
|VTMP                |OK, value: S:\temp\v_0
|Current working dir |OK, value: S:\vProjects\testit
|                    |
|env VFLAGS          |"-g"
|Git version         |git version 2.52.0.windows.1
|V git status        |0.5-23-gb9e9192b-dirty
|.git/config present |true
|                    |
|cc version          |N/A
|gcc version         |gcc (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders, r8) 13.2.0
|clang version       |N/A
|msvc version        |N/A
|tcc version         |tcc version 0.9.27 (x86_64 Windows)
|tcc git status      |thirdparty-windows-amd64 b425ac82
|emcc version        |N/A
|glibc version       |N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions