diff --git a/sw/lib/ocaml/latlong.ml b/sw/lib/ocaml/latlong.ml index 22d4d190c3..e377b6b308 100644 --- a/sw/lib/ocaml/latlong.ml +++ b/sw/lib/ocaml/latlong.ml @@ -443,3 +443,22 @@ let bearing = fun geo1 geo2 -> and utm2 = utm_of WGS84 geo2 in let (dx, dy) = utm_sub utm2 utm1 in ((Rad>>Deg)(atan2 dx dy), sqrt(dx*.dx+.dy*.dy)) + + +let leap_seconds = 14 (* http://www.leapsecond.com/java/gpsclock.htm *) + + +let gps_tow_of_utc = fun ?wday hour min sec -> + let wday = + match wday with + Some w -> w + | None -> (Unix.gmtime (Unix.gettimeofday ())).Unix.tm_wday in + ((wday*24 + hour)*60+min)*60+sec + leap_seconds + +let get_gps_tow = fun () -> + let utc = Unix.gmtime (Unix.gettimeofday ()) in + gps_tow_of_utc ~wday:utc.Unix.tm_wday utc.Unix.tm_hour utc.Unix.tm_min utc.Unix.tm_sec + +let unix_time_of_tow = fun tow -> + let host_tow = get_gps_tow () in + Unix.gettimeofday () +. float (tow - host_tow) diff --git a/sw/lib/ocaml/latlong.mli b/sw/lib/ocaml/latlong.mli index 8aa3b9da88..110a4800e8 100644 --- a/sw/lib/ocaml/latlong.mli +++ b/sw/lib/ocaml/latlong.mli @@ -146,3 +146,12 @@ val inv_mercator_lat : float -> float val bearing : geographic -> geographic -> float * float (** [bearing from to] returns (degrees CW/north, m) *) +val gps_tow_of_utc : ?wday:int -> int -> int -> int -> int +(** [gps_tow_of_utc ?wday hour min sec] Returns the GPS time of week + in seconds, taking into acount the leap seconds. Default [wday] (week + day) is the current day (Sunday is 0). *) + +val get_gps_tow : unit -> int +(** Returns the current GPS time of week in seconds *) + +val unix_time_of_tow : int -> float