/*----------------------------------------------------------------------------
   Copyright 2012-2021, Microsoft Research, Daan Leijen.

   Licensed under the Apache License, Version 2.0 ("The Licence"). You may not
   use this file except in compliance with the License. A copy of the License
   can be found in the LICENSE file at the root of this distribution.
----------------------------------------------------------------------------*/

/* Get the system time.

*/
module std/time/chronostd/time/chrono

import std/time/timestampstd/time/timestamp
import std/time/durationstd/time/duration
import std/time/instantstd/time/instant
import std/time/utcstd/time/utc

extern import
  c  file "chrono-inline.c"
  cs file "chrono-inline.cs"
  js file "chrono-inline.js"

// The current `:instant` in time as returned by the system clock
// in the UTC timescale. Equivalent to `now-in(utc())`, see
// `now-in` for further information about the system clock.
pub fun nowstd/time/chrono/now: () -> <ndet,utc> instant()result: -> <ndet,utc> instant : <std/core/types/total: Endetstd/core/types/ndet: X,utcstd/time/utc/utc: (E, V) -> V> instantstd/time/instant/instant: V
  now-instd/time/chrono/now-in: (ts : ? timescale) -> <ndet,utc> instant(utcstd/time/utc/utc: () -> <utc,ndet> timescale())


// The current `:instant` in time as returned by the system clock
// in an optional time scale `ts` (`= ts-ti`).
//
// This uses the best available system clock for the requested
// timescale. For example
// it uses [``CLOCK_UTC``](https://www.madore.org/~david/computers/unix-leap-seconds.html)
// when available to get proper UTC time, or ``CLOCK_TAI`` for TAI time.
//
// Otherwise, it usually uses Unix (POSIX) time (``CLOCK_REALTIME``).
// Unfortunately, most operating systems cannot not report time in leap
// seconds accurately. The `now` function is limited by the OS in this case.
//
// To guard against inaccurate clocks and increase monotonicity,
// the `now` function guarantees that if the current measurement is
// upto 1 second in the past with regard to the previous call to `now`,
// that the returned instant is monotonic by adding nano seconds to the
// previous measurement until the system clock catches up again.
//
// This is effective in particular on older OS's where the time sometimes jumps
// back one second after a leap second. By limiting the adjustment to at most
// one second it ensures the clock catches up soon and does not affect the user
// setting a new time in the past.
pub fun now-instd/time/chrono/now-in: (ts : ? timescale) -> ndet instant( tsts: ? timescale : timescalestd/time/instant/timescale: V = ts-tistd/time/utc/ts-ti: timescale)result: -> ndet instant : ndetstd/core/types/ndet: X instantstd/time/instant/instant: V
  // on current backends (C#, JavaScript) we can only use `unix-now` :-(
  val (std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b)secssecs: float64,fracfrac: float64)std/core/types/Tuple2: forall<a,b> (fst : a, snd : b) -> (a, b) = unix-nowstd/time/chrono/unix-now: () -> ndet (float64, float64)()
  val leapleap: int = 0literal: int
dec = 0
hex8 = 0x00
bit8 = 0b00000000
unix-instantstd/time/utc/timespan/unix-instant: (t : timespan, leap : ? int, ts : ? timescale) -> ndet instant(timespanstd/time/timestamp/tuple64/timespan: (secs : float64, frac : float64) -> ndet timespan(secssecs: float64,fracfrac: float64),leapleap: int,tsts: timescale
) // Returns a unix time stamp as seconds and fraction of seconds; // The fraction of seconds is for added precision if necessary, // and can be larger than one to indicate leap seconds. // This still needs to be adjusted to our epoch and taking account of leap seconds. extern unix-nowstd/time/chrono/unix-now: () -> ndet (float64, float64)() : ndetstd/core/types/ndet: X (std/core/types/tuple2: (V, V) -> Vfloat64std/core/types/float64: V,float64std/core/types/float64: V) c "kk_time_unix_now_tuple" cs "_Chrono.UnixNow" js "_unix_now" // Return the smallest time difference that the system clock can measure. pub fun now-resolutionstd/time/chrono/now-resolution: () -> ndet duration()result: -> ndet duration : ndetstd/core/types/ndet: X durationstd/time/duration/duration: V durationstd/time/duration/float64/duration: (secs : float64) -> ndet duration(xnow-resolutionstd/time/chrono/xnow-resolution: () -> ndet float64()) extern xnow-resolutionstd/time/chrono/xnow-resolution: () -> ndet float64() : ndetstd/core/types/ndet: X float64std/core/types/float64: V c "kk_time_dresolution" cs "_Chrono.NowResolution" js "_now_resolution"