risingwave_common::cast

Function i64_to_timestamp

source
pub fn i64_to_timestamp(t: i64) -> Result<Timestamp, String>
Expand description

Converts UNIX epoch time to timestamp.

The input UNIX epoch time is interpreted as follows:

  • [0, 1e11) are assumed to be in seconds.
  • [1e11, 1e14) are assumed to be in milliseconds.
  • [1e14, 1e17) are assumed to be in microseconds.
  • [1e17, upper) are assumed to be in nanoseconds.

This would cause no problem for timestamp in [1973-03-03 09:46:40, 5138-11-16 09:46:40).

ยงExample

assert_eq!(
    i64_to_timestamp(1_666_666_666).unwrap().to_string(),
    "2022-10-25 02:57:46"
);
assert_eq!(
    i64_to_timestamp(1_666_666_666_666).unwrap().to_string(),
    "2022-10-25 02:57:46.666"
);
assert_eq!(
    i64_to_timestamp(1_666_666_666_666_666).unwrap().to_string(),
    "2022-10-25 02:57:46.666666"
);
assert_eq!(
    i64_to_timestamp(1_666_666_666_666_666_666)
        .unwrap()
        .to_string(),
    // note that we only support microseconds precision
    "2022-10-25 02:57:46.666666"
);