Skip to content
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

Epoch::from_time_of_week returns wrong time in GPST #209

Closed
ljbade opened this issue Mar 18, 2023 · 10 comments
Closed

Epoch::from_time_of_week returns wrong time in GPST #209

ljbade opened this issue Mar 18, 2023 · 10 comments

Comments

@ljbade
Copy link

ljbade commented Mar 18, 2023

When using Epoch::from_time_of_week to initialise Epoch from a GPS week number and time of week, the resulting epoch has a 19 second offset. Below is a sample program that shows the bug:

use hifitime::{Epoch, TimeScale};

fn main() {
    let t = Epoch::from_time_of_week(1983, 0, TimeScale::GPST);
    println!("{:#?}", t);
    // Should print "2018-01-07T00:00:00 GPST"

    let t = Epoch::from_time_of_week(1982, 604800000000000 - 19000000000, TimeScale::GPST);
    println!("{:#?}", t);
}

Currently this prints:

2018-01-07T00:00:19 GPST
2018-01-07T00:00:00 GPST
@ljbade
Copy link
Author

ljbade commented Mar 18, 2023

Also, as a feature request it would be handy to have a version of from_time_of_week and from_time_of_week that works on f64 seconds instead of nanoseconds

@ChristopherRabotin
Copy link
Member

ChristopherRabotin commented Mar 18, 2023 via email

@ljbade
Copy link
Author

ljbade commented Mar 18, 2023

It's not urgent, I was able to work around it by just subtracting the 19 second offset to value I give to from_time_of_week

@ljbade
Copy link
Author

ljbade commented Mar 29, 2023

Possibly related to this bug, if you provide the same time in two different GNSS timescales you get the wrong values in UTC. For example if I use GPST and BDT then I get the same incorrect time in UTC for both.

A test:

se hifitime::{Epoch, TimeScale};

fn main() {
    let t = Epoch::from_time_of_week(1983, 0, TimeScale::GPST);
    println!("{:#?}", t);
    // Should print "2018-01-07T00:00:00 GPST"

    let t = Epoch::from_time_of_week(1982, 604800000000000 - 19000000000, TimeScale::GPST);
    println!("{:#?}", t);


    let t_gps = Epoch::from_gregorian(2023, 03, 12, 00, 00, 00, 0, TimeScale::GPST);
    println!("{}", t_gps);
    // Should print "2018-01-06T23:59:42 UTC"

    let t_bdt = Epoch::from_gregorian(2023, 03, 12, 00, 00, 00, 0, TimeScale::BDT);
    println!("{}", t_bdt);
    // Should print "2018-01-06T23:59:56 UTC"

    let delta_t = t_bdt - t_gps;
    println!("{}", delta_t.in_seconds());
    // Should print "14"
}

This prints currently:

2023-03-11T23:59:23 UTC
2023-03-11T23:59:23 UTC
0

The UTC time printed seems to be always 37 seconds off from the input time. 37 seconds I think is also the difference between UTC and TAI, so perhaps both times are being interpreted as TAI?

@ChristopherRabotin
Copy link
Member

Thanks for the report, that does look like there is a wrong interpretation of the leap seconds.

@gwbres Any thoughts on this issue?

@gwbres
Copy link
Collaborator

gwbres commented Mar 29, 2023

Thank you @ljbade for the bug report !

If i remember correctly, 19 is the amount of leap seconds when GPST was initiated (1980 Jan 5th to 6th midnight).
My first guess would be a problem of bad TAI referencing? we're supposed to always refer to TAI in our calculations

@ljbade
Copy link
Author

ljbade commented Mar 29, 2023

Yes GPS is 19 seconds offset from TAI, and BDT is 33 seconds.

@ChristopherRabotin
Copy link
Member

Hey @ljbade , if that's OK, I'll work on this for version 4.0.0. As explained in #237 , I think that things get confused in between the GNSS time scales.

@ChristopherRabotin
Copy link
Member

Hi @ljbade ,

I think that I've fixed the bug in #303 . Here is the test I'm about to add to the regression tests and the output I got. The first case matches exactly what you said it should be, and I'd like your confirmation that the second output is also correct.

Thanks

#[test]
fn regression_test_gh_209() {
    let t = Epoch::from_time_of_week(1983, 0, TimeScale::GPST);

    println!("{t}");
    // Should print

    assert_eq!(format!("{t}"), format!("2018-01-07T00:00:00 GPST"));

    let t = Epoch::from_time_of_week(1982, 604800000000000 - 19000000000, TimeScale::GPST);
    println!("{t}");
}

Output:

---- regression_test_gh_209 stdout ----
2018-01-07T00:00:00 GPST
2018-01-06T23:59:41 GPST


successes:
    regression_test_gh_209

ChristopherRabotin added a commit that referenced this issue Jun 9, 2024
@ChristopherRabotin
Copy link
Member

Done in #305 .

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

No branches or pull requests

3 participants