Skip to content

Support integer parts-per-second time unit#2227

Open
petermm wants to merge 2 commits into
atomvm:release-0.7from
petermm:feature/integer-time-unit
Open

Support integer parts-per-second time unit#2227
petermm wants to merge 2 commits into
atomvm:release-0.7from
petermm:feature/integer-time-unit

Conversation

@petermm

@petermm petermm commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

Add support for positive integer time units ("parts per second") in erlang:monotonic_time/1, erlang:system_time/1, and calendar:system_time_to_universal_time/2.

Used in elixir DynamicSupervisor - completes the timeunit support.

Restrict integer-unit handling to int64 inputs in the affected NIF paths. Use checked int64 decomposition for monotonic/system time conversion to avoid signed overflow in intermediate arithmetic. For calendar integer units, floor negative fractional values to whole seconds before converting to UTC.

Add focused Erlang tests for integer-unit parity, badarg on non-positive integer units, and negative fractional calendar conversion for integer units.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@petermm petermm force-pushed the feature/integer-time-unit branch 2 times, most recently from dad612d to 1f640b2 Compare March 24, 2026 12:54
@petermm petermm force-pushed the feature/integer-time-unit branch 5 times, most recently from 0303a59 to 4dff77a Compare April 8, 2026 13:22
@petermm petermm force-pushed the feature/integer-time-unit branch from 4dff77a to b5006b8 Compare May 28, 2026 18:21
@petermm petermm force-pushed the feature/integer-time-unit branch from b5006b8 to fe55a5f Compare June 6, 2026 02:56

@bettio bettio left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests are not working on the BEAM, let's fix this first.

log from run-tests:

datetime:OK
Raised {badmatch,fail}, stacktrace:
[{test_system_time,test_bad_integer_unit_universal_time,0,
                   [{file,"/__w/AtomVM/AtomVM/tests/erlang_tests/test_system_time.erl"},
                    {line,259}]},
 {test_system_time,start,0,
                   [{file,"/__w/AtomVM/AtomVM/tests/erlang_tests/test_system_time.erl"},
                    {line,45}]},
 {erl_eval,do_apply,7,[{file,"erl_eval.erl"},{line,915}]},
 {erl_eval,try_clauses,10,[{file,"erl_eval.erl"},{line,1233}]},
 {erl_eval,expr,6,[{file,"erl_eval.erl"},{line,663}]},
 {erl_eval,exprs,6,[{file,"erl_eval.erl"},{line,271}]},
 {init,start_it,1,[]},
 {init,start_em,1,[]}]
test_system_time:
test_system_time:FAILED

Comment thread tests/erlang_tests/test_system_time.erl Outdated
Comment thread tests/erlang_tests/test_monotonic_time.erl Outdated
Comment thread CHANGELOG.md Outdated
@petermm

petermm commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

thank you, shall blame the CI blunder on it being backed up;-)

let me know if a squash is needed.

@petermm petermm force-pushed the feature/integer-time-unit branch from f713183 to c817ad6 Compare June 8, 2026 17:43
@petermm

This comment was marked as outdated.

@bettio bettio left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of minor fixes. Feel free to cleanup PR history and push it for merge.

Comment thread src/libAtomVM/nifs.c Outdated
Comment thread src/libAtomVM/nifs.c Outdated
Comment thread src/libAtomVM/nifs.c Outdated
@petermm petermm force-pushed the feature/integer-time-unit branch from 4c19344 to df30d10 Compare June 9, 2026 12:04
@petermm petermm marked this pull request as draft June 9, 2026 12:20
@petermm petermm force-pushed the feature/integer-time-unit branch 5 times, most recently from 69a2fdd to b751fb0 Compare June 9, 2026 22:37
…bounds

- Add support for integer parts-per-second time units (e.g. 1_000_000 for microseconds)

- Refactor time-unit conversion into shared helper functions

- Harden calendar and integer time unit conversion bounds

- Fix input validation and negative rounding in calendar:system_time_to_universal_time/2

- Ensure BEAM compatibility for monotonic_time and system_time tests

- Use AVM_INT range for calendar year and add year boundary tests

Signed-off-by: Peter M <petermm@gmail.com>
@petermm petermm force-pushed the feature/integer-time-unit branch from b751fb0 to 18642d9 Compare June 13, 2026 19:43
Reserve heap space for calendar years that exceed the immediate integer range and construct them as boxed integers. This prevents malformed year terms on 32-bit builds while preserving support for representable large dates.

Add a regression test that crosses the 32-bit small-integer boundary and remains portable to platforms with limited time_t or gmtime_r ranges.

Signed-off-by: Peter M <petermm@gmail.com>
@petermm

petermm commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Hit some complexities - namely OTP only support "extreme" values in calendar since OTP29 - erlang/otp#10449

then further complexities around negative numbers, what esp32/newlib uses, and some theoretical issues for years +-134 millions and beyond on some 32bit system :/

time is always fun, should be ready now.

@petermm petermm marked this pull request as ready for review June 14, 2026 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants