2022-03-16 15:40:26 +00:00
|
|
|
use crate::sql::common::take_u64;
|
2022-01-13 17:36:41 +00:00
|
|
|
use crate::sql::datetime::Datetime;
|
2022-09-29 06:26:34 +00:00
|
|
|
use crate::sql::ending::duration as ending;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2023-04-25 10:13:04 +00:00
|
|
|
use crate::sql::strand::Strand;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2022-05-05 06:59:48 +00:00
|
|
|
use nom::multi::many1;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
2022-05-05 06:59:48 +00:00
|
|
|
use std::iter::Sum;
|
2022-01-13 17:36:41 +00:00
|
|
|
use std::ops;
|
2022-05-05 06:10:05 +00:00
|
|
|
use std::ops::Deref;
|
2023-04-25 10:13:04 +00:00
|
|
|
use std::str::FromStr;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::time;
|
|
|
|
|
2023-09-08 11:28:36 +00:00
|
|
|
use super::error::expected;
|
|
|
|
|
2022-09-27 21:35:30 +00:00
|
|
|
static SECONDS_PER_YEAR: u64 = 365 * SECONDS_PER_DAY;
|
|
|
|
static SECONDS_PER_WEEK: u64 = 7 * SECONDS_PER_DAY;
|
|
|
|
static SECONDS_PER_DAY: u64 = 24 * SECONDS_PER_HOUR;
|
|
|
|
static SECONDS_PER_HOUR: u64 = 60 * SECONDS_PER_MINUTE;
|
2022-05-05 06:10:05 +00:00
|
|
|
static SECONDS_PER_MINUTE: u64 = 60;
|
2023-03-09 19:32:32 +00:00
|
|
|
static NANOSECONDS_PER_MILLISECOND: u32 = 1000000;
|
2023-04-30 18:34:26 +00:00
|
|
|
static NANOSECONDS_PER_MICROSECOND: u32 = 1000;
|
2022-05-05 06:10:05 +00:00
|
|
|
|
2023-03-30 10:41:44 +00:00
|
|
|
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Duration";
|
|
|
|
|
2023-10-18 08:56:22 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-04-29 15:58:22 +00:00
|
|
|
#[serde(rename = "$surrealdb::private::sql::Duration")]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2022-05-05 06:10:05 +00:00
|
|
|
pub struct Duration(pub time::Duration);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl From<time::Duration> for Duration {
|
2022-05-05 06:10:05 +00:00
|
|
|
fn from(v: time::Duration) -> Self {
|
2022-10-04 21:51:18 +00:00
|
|
|
Self(v)
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:13:04 +00:00
|
|
|
impl From<Duration> for time::Duration {
|
|
|
|
fn from(s: Duration) -> Self {
|
|
|
|
s.0
|
2022-05-20 21:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:13:04 +00:00
|
|
|
impl FromStr for Duration {
|
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Self::try_from(s)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:13:04 +00:00
|
|
|
impl TryFrom<String> for Duration {
|
|
|
|
type Error = ();
|
|
|
|
fn try_from(v: String) -> Result<Self, Self::Error> {
|
|
|
|
Self::try_from(v.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Strand> for Duration {
|
|
|
|
type Error = ();
|
|
|
|
fn try_from(v: Strand) -> Result<Self, Self::Error> {
|
|
|
|
Self::try_from(v.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&str> for Duration {
|
|
|
|
type Error = ();
|
|
|
|
fn try_from(v: &str) -> Result<Self, Self::Error> {
|
|
|
|
match duration(v) {
|
|
|
|
Ok((_, v)) => Ok(v),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
2022-10-26 14:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 06:10:05 +00:00
|
|
|
impl Deref for Duration {
|
|
|
|
type Target = time::Duration;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 09:17:16 +00:00
|
|
|
impl Duration {
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Convert the Duration to a raw String
|
2022-07-06 09:17:16 +00:00
|
|
|
pub fn to_raw(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
2023-04-25 10:13:04 +00:00
|
|
|
/// Get the total number of nanoseconds
|
|
|
|
pub fn nanos(&self) -> u128 {
|
|
|
|
self.0.as_nanos()
|
|
|
|
}
|
|
|
|
/// Get the total number of microseconds
|
|
|
|
pub fn micros(&self) -> u128 {
|
|
|
|
self.0.as_micros()
|
|
|
|
}
|
|
|
|
/// Get the total number of milliseconds
|
|
|
|
pub fn millis(&self) -> u128 {
|
|
|
|
self.0.as_millis()
|
|
|
|
}
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Get the total number of seconds
|
2023-04-25 10:13:04 +00:00
|
|
|
pub fn secs(&self) -> u64 {
|
|
|
|
self.0.as_secs()
|
2022-09-22 23:48:49 +00:00
|
|
|
}
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Get the total number of minutes
|
2023-04-25 10:13:04 +00:00
|
|
|
pub fn mins(&self) -> u64 {
|
|
|
|
self.0.as_secs() / SECONDS_PER_MINUTE
|
2022-09-22 23:48:49 +00:00
|
|
|
}
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Get the total number of hours
|
2023-04-25 10:13:04 +00:00
|
|
|
pub fn hours(&self) -> u64 {
|
|
|
|
self.0.as_secs() / SECONDS_PER_HOUR
|
2022-09-22 23:48:49 +00:00
|
|
|
}
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Get the total number of dats
|
2023-04-25 10:13:04 +00:00
|
|
|
pub fn days(&self) -> u64 {
|
|
|
|
self.0.as_secs() / SECONDS_PER_DAY
|
2022-09-22 23:48:49 +00:00
|
|
|
}
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Get the total number of months
|
2023-04-25 10:13:04 +00:00
|
|
|
pub fn weeks(&self) -> u64 {
|
|
|
|
self.0.as_secs() / SECONDS_PER_WEEK
|
2022-09-22 23:48:49 +00:00
|
|
|
}
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Get the total number of years
|
2023-04-25 10:13:04 +00:00
|
|
|
pub fn years(&self) -> u64 {
|
|
|
|
self.0.as_secs() / SECONDS_PER_YEAR
|
|
|
|
}
|
|
|
|
/// Create a duration from nanoseconds
|
|
|
|
pub fn from_nanos(nanos: u64) -> Duration {
|
|
|
|
time::Duration::from_nanos(nanos).into()
|
|
|
|
}
|
|
|
|
/// Create a duration from microseconds
|
|
|
|
pub fn from_micros(micros: u64) -> Duration {
|
|
|
|
time::Duration::from_micros(micros).into()
|
|
|
|
}
|
|
|
|
/// Create a duration from milliseconds
|
|
|
|
pub fn from_millis(millis: u64) -> Duration {
|
|
|
|
time::Duration::from_millis(millis).into()
|
|
|
|
}
|
|
|
|
/// Create a duration from seconds
|
|
|
|
pub fn from_secs(secs: u64) -> Duration {
|
|
|
|
time::Duration::from_secs(secs).into()
|
|
|
|
}
|
|
|
|
/// Create a duration from minutes
|
|
|
|
pub fn from_mins(mins: u64) -> Duration {
|
|
|
|
time::Duration::from_secs(mins * SECONDS_PER_MINUTE).into()
|
|
|
|
}
|
|
|
|
/// Create a duration from hours
|
|
|
|
pub fn from_hours(hours: u64) -> Duration {
|
|
|
|
time::Duration::from_secs(hours * SECONDS_PER_HOUR).into()
|
|
|
|
}
|
|
|
|
/// Create a duration from days
|
|
|
|
pub fn from_days(days: u64) -> Duration {
|
|
|
|
time::Duration::from_secs(days * SECONDS_PER_DAY).into()
|
|
|
|
}
|
|
|
|
/// Create a duration from weeks
|
|
|
|
pub fn from_weeks(days: u64) -> Duration {
|
|
|
|
time::Duration::from_secs(days * SECONDS_PER_WEEK).into()
|
2022-09-22 23:48:49 +00:00
|
|
|
}
|
2022-07-06 09:17:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
impl fmt::Display for Duration {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-05-05 06:10:05 +00:00
|
|
|
// Split up the duration
|
|
|
|
let secs = self.0.as_secs();
|
|
|
|
let nano = self.0.subsec_nanos();
|
2022-08-29 01:47:33 +00:00
|
|
|
// Ensure no empty output
|
|
|
|
if secs == 0 && nano == 0 {
|
|
|
|
return write!(f, "0ns");
|
|
|
|
}
|
2022-05-05 06:10:05 +00:00
|
|
|
// Calculate the total years
|
|
|
|
let year = secs / SECONDS_PER_YEAR;
|
|
|
|
let secs = secs % SECONDS_PER_YEAR;
|
|
|
|
// Calculate the total weeks
|
|
|
|
let week = secs / SECONDS_PER_WEEK;
|
|
|
|
let secs = secs % SECONDS_PER_WEEK;
|
|
|
|
// Calculate the total days
|
|
|
|
let days = secs / SECONDS_PER_DAY;
|
|
|
|
let secs = secs % SECONDS_PER_DAY;
|
|
|
|
// Calculate the total hours
|
|
|
|
let hour = secs / SECONDS_PER_HOUR;
|
|
|
|
let secs = secs % SECONDS_PER_HOUR;
|
2023-03-09 19:32:32 +00:00
|
|
|
// Calculate the total minutes
|
2022-05-05 06:10:05 +00:00
|
|
|
let mins = secs / SECONDS_PER_MINUTE;
|
|
|
|
let secs = secs % SECONDS_PER_MINUTE;
|
2023-09-28 09:17:29 +00:00
|
|
|
// Calculate the total milliseconds
|
2023-03-09 19:32:32 +00:00
|
|
|
let msec = nano / NANOSECONDS_PER_MILLISECOND;
|
|
|
|
let nano = nano % NANOSECONDS_PER_MILLISECOND;
|
2023-04-30 18:34:26 +00:00
|
|
|
// Calculate the total microseconds
|
|
|
|
let usec = nano / NANOSECONDS_PER_MICROSECOND;
|
|
|
|
let nano = nano % NANOSECONDS_PER_MICROSECOND;
|
2022-05-05 06:10:05 +00:00
|
|
|
// Write the different parts
|
|
|
|
if year > 0 {
|
2022-08-29 01:47:33 +00:00
|
|
|
write!(f, "{year}y")?;
|
2022-05-05 06:10:05 +00:00
|
|
|
}
|
|
|
|
if week > 0 {
|
2022-08-29 01:47:33 +00:00
|
|
|
write!(f, "{week}w")?;
|
2022-05-05 06:10:05 +00:00
|
|
|
}
|
|
|
|
if days > 0 {
|
2022-08-29 01:47:33 +00:00
|
|
|
write!(f, "{days}d")?;
|
2022-05-05 06:10:05 +00:00
|
|
|
}
|
|
|
|
if hour > 0 {
|
2022-08-29 01:47:33 +00:00
|
|
|
write!(f, "{hour}h")?;
|
2022-05-05 06:10:05 +00:00
|
|
|
}
|
|
|
|
if mins > 0 {
|
2022-08-29 01:47:33 +00:00
|
|
|
write!(f, "{mins}m")?;
|
2022-05-05 06:10:05 +00:00
|
|
|
}
|
|
|
|
if secs > 0 {
|
2022-08-29 01:47:33 +00:00
|
|
|
write!(f, "{secs}s")?;
|
2022-05-05 06:10:05 +00:00
|
|
|
}
|
2023-03-09 19:32:32 +00:00
|
|
|
if msec > 0 {
|
|
|
|
write!(f, "{msec}ms")?;
|
|
|
|
}
|
2023-04-30 18:34:26 +00:00
|
|
|
if usec > 0 {
|
|
|
|
write!(f, "{usec}µs")?;
|
|
|
|
}
|
2022-05-05 06:10:05 +00:00
|
|
|
if nano > 0 {
|
2022-08-29 01:47:33 +00:00
|
|
|
write!(f, "{nano}ns")?;
|
2022-05-05 06:10:05 +00:00
|
|
|
}
|
2022-08-29 01:47:33 +00:00
|
|
|
Ok(())
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl ops::Add for Duration {
|
|
|
|
type Output = Self;
|
|
|
|
fn add(self, other: Self) -> Self {
|
2023-04-06 08:34:56 +00:00
|
|
|
match self.0.checked_add(other.0) {
|
|
|
|
Some(v) => Duration::from(v),
|
|
|
|
None => Duration::from(time::Duration::MAX),
|
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 06:59:48 +00:00
|
|
|
impl<'a, 'b> ops::Add<&'b Duration> for &'a Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
fn add(self, other: &'b Duration) -> Duration {
|
2023-04-06 08:34:56 +00:00
|
|
|
match self.0.checked_add(other.0) {
|
|
|
|
Some(v) => Duration::from(v),
|
|
|
|
None => Duration::from(time::Duration::MAX),
|
|
|
|
}
|
2022-05-05 06:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl ops::Sub for Duration {
|
|
|
|
type Output = Self;
|
|
|
|
fn sub(self, other: Self) -> Self {
|
2023-04-06 08:34:56 +00:00
|
|
|
match self.0.checked_sub(other.0) {
|
|
|
|
Some(v) => Duration::from(v),
|
|
|
|
None => Duration::default(),
|
|
|
|
}
|
2022-01-13 17:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 06:59:48 +00:00
|
|
|
impl<'a, 'b> ops::Sub<&'b Duration> for &'a Duration {
|
|
|
|
type Output = Duration;
|
|
|
|
fn sub(self, other: &'b Duration) -> Duration {
|
2023-04-06 08:34:56 +00:00
|
|
|
match self.0.checked_sub(other.0) {
|
|
|
|
Some(v) => Duration::from(v),
|
|
|
|
None => Duration::default(),
|
|
|
|
}
|
2022-05-05 06:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
impl ops::Add<Datetime> for Duration {
|
|
|
|
type Output = Datetime;
|
|
|
|
fn add(self, other: Datetime) -> Datetime {
|
2022-05-05 06:10:05 +00:00
|
|
|
match chrono::Duration::from_std(self.0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Ok(d) => Datetime::from(other.0 + d),
|
2022-01-13 17:36:41 +00:00
|
|
|
Err(_) => Datetime::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<Datetime> for Duration {
|
|
|
|
type Output = Datetime;
|
|
|
|
fn sub(self, other: Datetime) -> Datetime {
|
2022-05-05 06:10:05 +00:00
|
|
|
match chrono::Duration::from_std(self.0) {
|
2022-05-05 05:01:00 +00:00
|
|
|
Ok(d) => Datetime::from(other.0 - d),
|
2022-01-13 17:36:41 +00:00
|
|
|
Err(_) => Datetime::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 06:59:48 +00:00
|
|
|
impl Sum<Self> for Duration {
|
|
|
|
fn sum<I>(iter: I) -> Duration
|
|
|
|
where
|
|
|
|
I: Iterator<Item = Self>,
|
|
|
|
{
|
|
|
|
iter.fold(Duration::default(), |a, b| a + b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Sum<&'a Self> for Duration {
|
|
|
|
fn sum<I>(iter: I) -> Duration
|
|
|
|
where
|
|
|
|
I: Iterator<Item = &'a Self>,
|
|
|
|
{
|
|
|
|
iter.fold(Duration::default(), |a, b| &a + b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn duration(i: &str) -> IResult<&str, Duration> {
|
2023-09-08 11:28:36 +00:00
|
|
|
expected("a duration", |i| {
|
|
|
|
let (i, v) = many1(duration_raw)(i)?;
|
|
|
|
let (i, _) = ending(i)?;
|
|
|
|
Ok((i, v.iter().sum::<Duration>()))
|
|
|
|
})(i)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 13:37:11 +00:00
|
|
|
fn duration_raw(i: &str) -> IResult<&str, Duration> {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, v) = part(i)?;
|
|
|
|
let (i, u) = unit(i)?;
|
2023-04-25 22:00:31 +00:00
|
|
|
|
|
|
|
let std_duration = match u {
|
|
|
|
"ns" => Some(time::Duration::from_nanos(v)),
|
|
|
|
"µs" => Some(time::Duration::from_micros(v)),
|
|
|
|
"us" => Some(time::Duration::from_micros(v)),
|
|
|
|
"ms" => Some(time::Duration::from_millis(v)),
|
|
|
|
"s" => Some(time::Duration::from_secs(v)),
|
|
|
|
"m" => v.checked_mul(SECONDS_PER_MINUTE).map(time::Duration::from_secs),
|
|
|
|
"h" => v.checked_mul(SECONDS_PER_HOUR).map(time::Duration::from_secs),
|
|
|
|
"d" => v.checked_mul(SECONDS_PER_DAY).map(time::Duration::from_secs),
|
|
|
|
"w" => v.checked_mul(SECONDS_PER_WEEK).map(time::Duration::from_secs),
|
|
|
|
"y" => v.checked_mul(SECONDS_PER_YEAR).map(time::Duration::from_secs),
|
|
|
|
_ => unreachable!("shouldn't have parsed {u} as duration unit"),
|
|
|
|
};
|
|
|
|
|
2023-09-08 11:28:36 +00:00
|
|
|
std_duration.map(|d| (i, Duration(d))).ok_or(nom::Err::Error(crate::sql::ParseError::Base(i)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn part(i: &str) -> IResult<&str, u64> {
|
2022-09-27 21:35:30 +00:00
|
|
|
take_u64(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unit(i: &str) -> IResult<&str, &str> {
|
2022-07-20 09:35:42 +00:00
|
|
|
alt((
|
|
|
|
tag("ns"),
|
|
|
|
tag("µs"),
|
2023-02-13 16:06:36 +00:00
|
|
|
tag("us"),
|
2022-07-20 09:35:42 +00:00
|
|
|
tag("ms"),
|
|
|
|
tag("s"),
|
|
|
|
tag("m"),
|
|
|
|
tag("h"),
|
|
|
|
tag("d"),
|
|
|
|
tag("w"),
|
|
|
|
tag("y"),
|
|
|
|
))(i)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2022-05-05 06:10:05 +00:00
|
|
|
use std::time::Duration;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_nil() {
|
|
|
|
let sql = "0ns";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("0ns", format!("{}", out));
|
2022-05-05 06:10:05 +00:00
|
|
|
assert_eq!(out.0, Duration::new(0, 0));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_basic() {
|
|
|
|
let sql = "1s";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("1s", format!("{}", out));
|
2022-05-05 06:10:05 +00:00
|
|
|
assert_eq!(out.0, Duration::new(1, 0));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_simple() {
|
|
|
|
let sql = "1000ms";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-05-05 06:10:05 +00:00
|
|
|
assert_eq!("1s", format!("{}", out));
|
|
|
|
assert_eq!(out.0, Duration::new(1, 0));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_complex() {
|
|
|
|
let sql = "86400s";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
2022-05-05 06:10:05 +00:00
|
|
|
assert_eq!("1d", format!("{}", out));
|
|
|
|
assert_eq!(out.0, Duration::new(86_400, 0));
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
2021-03-29 15:43:37 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_days() {
|
|
|
|
let sql = "5d";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("5d", format!("{}", out));
|
2022-05-05 06:10:05 +00:00
|
|
|
assert_eq!(out.0, Duration::new(432_000, 0));
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_weeks() {
|
|
|
|
let sql = "4w";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("4w", format!("{}", out));
|
2022-05-05 06:10:05 +00:00
|
|
|
assert_eq!(out.0, Duration::new(2_419_200, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_split() {
|
|
|
|
let sql = "129600s";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("1d12h", format!("{}", out));
|
|
|
|
assert_eq!(out.0, Duration::new(129_600, 0));
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
2022-05-05 06:59:48 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_multi() {
|
|
|
|
let sql = "1d12h30m";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("1d12h30m", format!("{}", out));
|
|
|
|
assert_eq!(out.0, Duration::new(131_400, 0));
|
|
|
|
}
|
2023-03-09 19:32:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_milliseconds() {
|
|
|
|
let sql = "500ms";
|
|
|
|
let res = duration(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("500ms", format!("{}", out));
|
|
|
|
assert_eq!(out.0, Duration::new(0, 500000000));
|
|
|
|
}
|
2023-04-25 22:00:31 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_overflow() {
|
|
|
|
let sql = "10000000000000000d";
|
|
|
|
let res = duration(sql);
|
2023-08-29 10:50:00 +00:00
|
|
|
res.unwrap_err();
|
2023-04-25 22:00:31 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|