mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-21 07:20:41 +00:00
fix: impl scalar value helper and remove range limit (#205)
* fix: impl scalar value helper function for DateTime * remove range limit for date * remove range limit for date
This commit is contained in:
@@ -4,17 +4,15 @@ use std::str::FromStr;
|
||||
use chrono::{Datelike, NaiveDate};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use snafu::{ensure, ResultExt};
|
||||
use snafu::ResultExt;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::error::{DateOverflowSnafu, Error, ParseDateStrSnafu};
|
||||
use crate::error::{Error, ParseDateStrSnafu};
|
||||
|
||||
const UNIX_EPOCH_FROM_CE: i32 = 719_163;
|
||||
|
||||
/// ISO 8601 [Date] values. The inner representation is a signed 32 bit integer that represents the
|
||||
/// **days since "1970-01-01 00:00:00 UTC" (UNIX Epoch)**.
|
||||
///
|
||||
/// [Date] value ranges between "0000-01-01" to "9999-12-31".
|
||||
#[derive(
|
||||
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deserialize, Serialize,
|
||||
)]
|
||||
@@ -44,23 +42,13 @@ impl Display for Date {
|
||||
}
|
||||
|
||||
impl Date {
|
||||
pub fn try_new(val: i32) -> Result<Self> {
|
||||
ensure!(
|
||||
val >= Self::MIN.0 && val <= Self::MAX.0,
|
||||
DateOverflowSnafu { value: val }
|
||||
);
|
||||
|
||||
Ok(Self(val))
|
||||
pub fn new(val: i32) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
|
||||
pub fn val(&self) -> i32 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Max valid Date value: "9999-12-31"
|
||||
pub const MAX: Date = Date(2932896);
|
||||
/// Min valid Date value: "1000-01-01"
|
||||
pub const MIN: Date = Date(-354285);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -71,9 +59,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
pub fn test_print_date2() {
|
||||
assert_eq!("1969-12-31", Date::try_new(-1).unwrap().to_string());
|
||||
assert_eq!("1970-01-01", Date::try_new(0).unwrap().to_string());
|
||||
assert_eq!("1970-02-12", Date::try_new(42).unwrap().to_string());
|
||||
assert_eq!("1969-12-31", Date::new(-1).to_string());
|
||||
assert_eq!("1970-01-01", Date::new(0).to_string());
|
||||
assert_eq!("1970-02-12", Date::new(42).to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -93,19 +81,9 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_illegal_date_values() {
|
||||
assert!(Date::try_new(Date::MAX.0 + 1).is_err());
|
||||
assert!(Date::try_new(Date::MIN.0 - 1).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_edge_date_values() {
|
||||
let date = Date::from_str("9999-12-31").unwrap();
|
||||
assert_eq!(Date::MAX.0, date.0);
|
||||
assert_eq!(date, Date::try_new(date.0).unwrap());
|
||||
|
||||
let date = Date::from_str("1000-01-01").unwrap();
|
||||
assert_eq!(Date::MIN.0, date.0);
|
||||
assert_eq!(date, Date::try_new(date.0).unwrap());
|
||||
pub fn test_min_max() {
|
||||
let mut date = Date::from_str("9999-12-31").unwrap();
|
||||
date.0 += 1000;
|
||||
assert_eq!(date, Date::from_str(&date.to_string()).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,15 +3,13 @@ use std::str::FromStr;
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use snafu::{ensure, ResultExt};
|
||||
use snafu::ResultExt;
|
||||
|
||||
use crate::error::{DateTimeOverflowSnafu, Error, ParseDateStrSnafu, Result};
|
||||
use crate::error::{Error, ParseDateStrSnafu, Result};
|
||||
|
||||
const DATETIME_FORMAT: &str = "%F %T";
|
||||
|
||||
/// [DateTime] represents the **seconds elapsed since "1970-01-01 00:00:00 UTC" (UNIX Epoch)**.
|
||||
///
|
||||
/// Valid [DateTime] value ranges from "1000-01-01 00:00:00" to "9999-12-31 23:59:59".
|
||||
#[derive(
|
||||
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
|
||||
)]
|
||||
@@ -41,23 +39,13 @@ impl FromStr for DateTime {
|
||||
}
|
||||
|
||||
impl DateTime {
|
||||
pub fn try_new(val: i64) -> Result<Self> {
|
||||
ensure!(
|
||||
val >= Self::MIN.0 && val <= Self::MAX.0,
|
||||
DateTimeOverflowSnafu { value: val }
|
||||
);
|
||||
|
||||
Ok(Self(val))
|
||||
pub fn new(val: i64) -> Self {
|
||||
Self(val)
|
||||
}
|
||||
|
||||
pub fn val(&self) -> i64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Max valid DateTime value: 9999-12-31 23:59:59
|
||||
pub const MAX: DateTime = DateTime(253402300799);
|
||||
/// Min valid DateTime value: 0000-01-01 00:00:00
|
||||
pub const MIN: DateTime = DateTime(-30610224000);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -66,24 +54,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
pub fn test_new_date_time() {
|
||||
assert_eq!(
|
||||
"1970-01-01 00:00:00",
|
||||
DateTime::try_new(0).unwrap().to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"1970-01-01 00:00:01",
|
||||
DateTime::try_new(1).unwrap().to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
"1969-12-31 23:59:59",
|
||||
DateTime::try_new(-1).unwrap().to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_max_min() {
|
||||
assert_eq!("9999-12-31 23:59:59", DateTime::MAX.to_string());
|
||||
assert_eq!("1000-01-01 00:00:00", DateTime::MIN.to_string());
|
||||
assert_eq!("1970-01-01 00:00:00", DateTime::new(0).to_string());
|
||||
assert_eq!("1970-01-01 00:00:01", DateTime::new(1).to_string());
|
||||
assert_eq!("1969-12-31 23:59:59", DateTime::new(-1).to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
use chrono::ParseError;
|
||||
use snafu::{Backtrace, Snafu};
|
||||
use snafu::Snafu;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[snafu(visibility(pub))]
|
||||
pub enum Error {
|
||||
#[snafu(display("Failed to parse string to date, raw: {}, source: {}", raw, source))]
|
||||
ParseDateStr { raw: String, source: ParseError },
|
||||
|
||||
#[snafu(display("Failed to parse i32 value to Date: {}", value))]
|
||||
DateOverflow { value: i32, backtrace: Backtrace },
|
||||
|
||||
#[snafu(display("Failed to parse i64 value to DateTime: {}", value))]
|
||||
DateTimeOverflow { value: i64, backtrace: Backtrace },
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
Reference in New Issue
Block a user