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

from_time_of_week is not the reciprocate of to_time_of_week when the reference epoch is J1900 #187

Closed
ChristopherRabotin opened this issue Dec 6, 2022 · 2 comments · Fixed by #188
Labels

Comments

@ChristopherRabotin
Copy link
Member

ChristopherRabotin commented Dec 6, 2022

The following fails, and I just do not understand why:

let epoch_utc = epoch.in_time_scale(TimeScale::UTC);
println!("{epoch:?}\t{epoch_utc:?}");
let (utc_wk, utc_tow) = epoch_utc.to_time_of_week();
assert_eq!(Epoch::from_time_of_week_utc(utc_wk, utc_tow), epoch_utc);

There is an offset of one day.

My hunch is that something is wrong with to_time_of_week but I don't understand why or where.

This happens for UTC, TT, and TAI. For ET and TDB, there is a small offset, most likely due to these time scales using an iteration for initialization (but that is not an "exactly one day" time error).

Note: this is currently hot fixed in the code of from_time_of_week.

@ChristopherRabotin ChristopherRabotin changed the title from_time_of_week_utc is not the reciprocate of to_time_of_week from_time_of_week is not the reciprocate of to_time_of_week when the reference epoch is J1900 Dec 6, 2022
ChristopherRabotin added a commit to gwbres/hifitime that referenced this issue Dec 6, 2022
Signed-off-by: Christopher Rabotin <christopher.rabotin@gmail.com>
@gwbres
Copy link
Collaborator

gwbres commented Dec 6, 2022

@ChristopherRabotin
I identified the problem but I have not found a way to solve it right now

The problem is specific to the new methods

  • to_time_of_week
  • from_time_of_week

the week counter specificly.

In these methods, we're trying to build an epoch, in a time scale, from a week counter.
And we do not consider a possible day offset, due to the time scale weekday starting point.

Take TAI/UTC that started on a Monday.
I'm pretty sure TAI <=> UTC operations work perfectly fine.

Apparently, all GNSS timescales started on a Sunday.
Take a GPST <=> UTC operations, we get the +/-1 day offset because we only consider an modulo operation, which only works in TAI.

The sign will different depending on the UTC => GPST or GPST => UTC operation order.
I'm pretty sure if you take GSPT <=> GST operations, you will get the correct results

We probably need something like this

        // week counter
        let mut week = div_euclid_f64(
            self.to_duration().to_unit(Unit::Day),
            Weekday::DAYS_PER_WEEK,
        ); // this is OK in TAI
        
        week -= time_scale.ref_weekday(); // Counter offset
        
        // the following is only to determine the offset within that week
        // and it works fine
        let mut start_of_week = self.previous_weekday_at_midnight(Weekday::Sunday);
        let ref_epoch = self.time_scale.ref_epoch();
        // restrict start of week/sunday to the start of the time scale
        if start_of_week < ref_epoch {
            start_of_week = ref_epoch;
        }
        let dw = *self - start_of_week; // difference in weekdays [0..6]
        (wk as u32, dw.nanoseconds)

Just give me a couple of more hours, I may have a solution

@ChristopherRabotin
Copy link
Member Author

ChristopherRabotin commented Dec 6, 2022 via email

@ChristopherRabotin ChristopherRabotin linked a pull request Dec 7, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants