### Utilities for dealing with nanoseconds ## The amount of nanoseconds in a microsecond const NANOS_PER_MICROSECOND = 1_000; ## The amount of nanoseconds in a millisecond const NANOS_PER_MILLISECOND = NANOS_PER_MICROSECOND * 1_000; ## The amount of nanoseconds in a second const NANOS_PER_SECOND = NANOS_PER_MILLISECOND * 1_000; ## The amount of nanoseconds in a minute const NANOS_PER_MINUTE = NANOS_PER_SECOND * 60; ## The amount of nanoseconds in an hour const NANOS_PER_HOUR = NANOS_PER_MINUTE * 60; ## The amount of nanoseconds in a day const NANOS_PER_DAY = NANOS_PER_HOUR * 24; ## The amount of nanoseconds in a week const NANOS_PER_WEEK = NANOS_PER_DAY * 7; ## convert the given weeks to nanoseconds ## ## > ```tremor ## > use std::time::nanos; ## > nanos::from_weeks(1) # 604800000000000 ## > ``` ## ## Returns an integer fn from_weeks(weeks) with weeks * NANOS_PER_WEEK end; ## convert the given days to nanoseconds ## ## > ```tremor ## > use std::time::nanos; ## > nanos::from_days(1) # 86400000000000 ## > ``` ## ## Returns an integer fn from_days(days) with days * NANOS_PER_DAY end; ## convert the given hours to nanoseconds ## ## > ```tremor ## > use std::time::nanos; ## > nanos::from_hours(1) # 3600000000000 ## > ``` ## ## Returns an integer fn from_hours(hours) with hours * NANOS_PER_HOUR end; ## convert the given minutes to nanoseconds ## ## > ```tremor ## > use std::time::nanos; ## > nanos::from_minutes(1) # 60000000000 ## > ``` ## ## Returns an integer fn from_minutes(minutes) with minutes * NANOS_PER_MINUTE end; ## Convert the given seconds to nanoseconds ## ## > ```tremor ## > use std::time::nanos; ## > nanos::from_seconds(1) # 1000000000 ## > ``` ## ## Returns an integer fn from_seconds(secs) with secs * NANOS_PER_SECOND end; ## Convert the given milliseconds to nanoseconds ## ## > ```tremor ## > use std::time::nanos; ## > nanos::from_millis(1) # 1000000 ## > ``` ## ## Returns an integer fn from_millis(millis) with millis * NANOS_PER_MILLISECOND end; ## Convert the given microseconds to nanoseconds ## ## > ```tremor ## > use std::time::nanos; ## > nanos::from_micros(1) # 1000 ## > ``` ## ## Returns an integer fn from_micros(micros) with micros * NANOS_PER_MICROSECOND end; ## Convert the given nanoseconds to weeks ## ## Returns an integer fn to_weeks(nanos) with nanos / NANOS_PER_WEEK end; ## Convert the given nanoseconds to days ## ## Returns an integer fn to_days(nanos) with nanos / NANOS_PER_DAY end; ## Convert the given nanoseconds to hours ## ## Returns an integer fn to_hours(nanos) with nanos / NANOS_PER_HOUR end; ## Convert the given nanoseconds to minutes ## ## Returns an integer fn to_minutes(nanos) with nanos / NANOS_PER_MINUTE end; ## Convert the given nanoseconds to seconds ## ## Returns an integer fn to_seconds(nanos) with nanos / NANOS_PER_SECOND end; ## Convert the given nanoseconds to milliseconds ## ## Returns an integer fn to_millis(nanos) with nanos / NANOS_PER_MILLISECOND end; ## Convert the given nanoseconds to microseconds ## ## Returns an integer fn to_micros(nanos) with nanos / NANOS_PER_MICROSECOND end; ## Truncate down to the next microsecond. ## ## This function does not take leap-seconds or any other datetime fanciness into account. This is plain timestamp math. ## ## > ```tremor ## > use std::time::nanos; ## > nanos::truncate_micros(9999) ## > # result: 9000 ## > ``` fn truncate_micros(nanos) with match nanos < 0 of case true => (nanos - NANOS_PER_MICROSECOND) - (nanos % NANOS_PER_MICROSECOND) case _ => nanos - (nanos % NANOS_PER_MICROSECOND) end end; ## Truncate down to the next millisecond ## ## This function does not take leap-seconds or any other datetime fanciness into account. This is plain timestamp math. ## ## > ```tremor ## > use std::time::nanos; ## > nanos::truncate_millis(9_999_999) ## > # result: 9_000_000 ## > ``` fn truncate_millis(nanos) with match nanos < 0 of case true => (nanos - NANOS_PER_MILLISECOND) - (nanos % NANOS_PER_MILLISECOND) case _ => nanos - (nanos % NANOS_PER_MILLISECOND) end end; ## Truncate down to the next second ## ## This function does not take leap-seconds or any other datetime fanciness into account. This is plain timestamp math. ## ## > ```tremor ## > use std::time::nanos; ## > nanos::truncate_seconds(9_999_999_999) ## > # result: 9_000_000_000 ## > ``` fn truncate_seconds(nanos) with match nanos < 0 of case true => (nanos - NANOS_PER_SECOND) - (nanos % NANOS_PER_SECOND) case _ => nanos - (nanos % NANOS_PER_SECOND) end end; ## Truncate down to the next minute ## ## This function does not take leap-seconds or any other datetime fanciness into account. This is plain timestamp math. ## ## > ```tremor ## > use std::time::nanos; ## > nanos::truncate_minutes(69_999_999_999) ## > # result: 60_000_000_000 ## > ``` fn truncate_minutes(nanos) with match nanos < 0 of case true => (nanos - NANOS_PER_MINUTE) - (nanos % NANOS_PER_MINUTE) case _ => nanos - (nanos % NANOS_PER_MINUTE) end end; ## Truncate down to the next hour ## ## This function does not take leap-seconds or any other datetime fanciness into account. This is plain timestamp math. ## ## > ```tremor ## > use std::time::nanos; ## > nanos::truncate_hours(3_699_999_999_999) ## > # result: 3_600_000_000_000 ## > ``` fn truncate_hours(nanos) with match nanos < 0 of case true => (nanos - NANOS_PER_HOUR) - (nanos % NANOS_PER_HOUR) case _ => nanos - (nanos % NANOS_PER_HOUR) end end; ## Truncate down to the next day ## ## This function does not take leap-seconds or any other datetime fanciness into account. This is plain timestamp math. ## ## > ```tremor ## > use std::time::nanos; ## > nanos::truncate_days(86_499_999_999_999) ## > # result: 86_400_000_000_000 ## > ``` fn truncate_days(nanos) with match nanos < 0 of case true => (nanos - NANOS_PER_DAY) - (nanos % NANOS_PER_DAY) case _ => nanos - (nanos % NANOS_PER_DAY) end end; # We don't know when a full week started so we cannot easily truncate down to a week # So we skipped the `truncate_weeks` function