Skip to content

Commit

Permalink
fix(winloc): and yet another fix for the timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
spmadden committed Sep 18, 2023
1 parent f9777b2 commit 099c216
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions interfaces/win-location-api/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
use std::fmt::{Display, Formatter, Write};
use std::time::Duration;

use time::ext::NumericalDuration;
use time::macros::datetime;
use time::OffsetDateTime;
use windows::Devices::Geolocation::Geocoordinate;

Expand All @@ -15,7 +13,7 @@ use irox_units::units::angle::Angle;
use irox_units::units::compass::{CompassReference, RotationDirection, Track};
use irox_units::units::speed::Speed;

pub const WIN_EPOCH: OffsetDateTime = datetime!(1601-01-01 00:00:00 +0);
pub const WINDOWS_2_NX_EPOCH_MICROS: i64 = 11_644_473_600_000_000;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum PositionSource {
Expand Down Expand Up @@ -127,13 +125,10 @@ impl From<&Geocoordinate> for WindowsCoordinate {
let mut timestamp = None;
if let Ok(ts) = value.Timestamp() {
// jfc. UniversalTime is the # of 100ns intervals since 01-JAN-1601 00:00:00
let micros_since_win_epoch = ts.UniversalTime / 10;
let unix_epoch =
micros_since_win_epoch.microseconds() - (OffsetDateTime::UNIX_EPOCH - WIN_EPOCH);
let nanos = unix_epoch.whole_nanoseconds();
if nanos >= 0 {
let micros_since_win_epoch = ts.UniversalTime / 10 - WINDOWS_2_NX_EPOCH_MICROS;
if micros_since_win_epoch >= 0 {
timestamp = OffsetDateTime::UNIX_EPOCH
.checked_add(time::Duration::nanoseconds(nanos as i64));
.checked_add(time::Duration::microseconds(micros_since_win_epoch));
}
}
if let Some(coord) = coordinate {
Expand Down

0 comments on commit 099c216

Please sign in to comment.