Skip to content

Document input.runningTimeMicros() overflows after 18 minutes #5372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
microbit-carlos opened this issue Oct 17, 2023 · 5 comments · Fixed by #6170
Closed

Document input.runningTimeMicros() overflows after 18 minutes #5372

microbit-carlos opened this issue Oct 17, 2023 · 5 comments · Fixed by #6170

Comments

@microbit-carlos
Copy link
Collaborator

This caught me by surprised and it's a MakeCode specific limitation, as CODAL returns a 32 bit unsigned int and MakeCode drops the 2 most significant bits:

/**
* Gets current time in microseconds. Overflows every ~18 minutes.
*/
//%
int micros() {
return system_timer_current_time_us() & 0x3fffffff;
}

I guess the signed bit makes sense, but don't know why the second bit is dropped as well.

It was done in commit 15b4b47 as part of PR #2572 (512b4dd).

This should probably be documented, as 18 minutes is a relatively short time that can be easily reached.

Docs:

# Running Time Micros
Find how long it has been since the program started in micro-seconds.
```sig
input.runningTimeMicros();
```
## Returns
* the [Number](/types/number) of microseconds since the program started.
(One second is 1000000 microseconds.)
## See also
[show number](/reference/basic/show-number), [pause](/reference/basic/pause)

@microbit-carlos microbit-carlos changed the title Document input.runningTimeMicros() overruns after 18 minutes Document input.runningTimeMicros() overflows after 18 minutes Oct 17, 2023
@microbit-carlos
Copy link
Collaborator Author

@pelikhan out of curiosity, do you know why the 2 most significant bits from CODAL/DAL system_timer_current_time_us are masked out in MakeCode? (it was done in 512b4dd)

/**
* Gets current time in microseconds. Overflows every ~18 minutes.
*/
//%
int micros() {
return system_timer_current_time_us() & 0x3fffffff;
}

The signed bit makes sense, but not sure why bit 30 is removed as well, was there a technical reason?

@pelikhan
Copy link
Member

@mmoskal

@mmoskal
Copy link
Member

mmoskal commented Oct 20, 2023

Integers up to 0x3fff_ffff are unboxed. Anything larger is boxed as a double. We could change this function to return a double (I think the codal call returns uint64_t).

@microbit-carlos
Copy link
Collaborator Author

Ah, that's interesting to know, thanks!

And yes, both DAL and CODAL return uint64_t (codal-microbit-v2 set CODAL_TIMESTAMP in target.json).

Not sure if changing the size range of input.runningTimeMicros() would be considered a breaking change (probably no, as it's still a number in JS/TS), so up to you, although if it's left as it is, it's worth noting the overflow in the docs.

@abchatra
Copy link
Collaborator

We need to document this. Not planning to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants