mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-26 01:40:36 +00:00
* fix: Rename current_timestamp to current_time_millis, fix resolution Fix current_timestamp returns seconds resolution, also add a test for this method * chore: Use slice of array instead of Vec Save some heap allocations * test: Compare std and chrono timestamp The original test always success even the current_time_millis returns in seconds resolution * chore: Store current time in gmt_created/gmt_modified
This commit is contained in:
@@ -6,4 +6,4 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4"
|
||||
chrono = "0.4"
|
||||
|
||||
@@ -1,4 +1,31 @@
|
||||
/// Calculates the time duration since UNIX_EPOCH in milliseconds.
|
||||
pub fn current_timestamp() -> i64 {
|
||||
chrono::Utc::now().timestamp()
|
||||
/// Returns the time duration since UNIX_EPOCH in milliseconds.
|
||||
pub fn current_time_millis() -> i64 {
|
||||
chrono::Utc::now().timestamp_millis()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::{self, SystemTime};
|
||||
|
||||
use chrono::{Datelike, TimeZone, Timelike};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_current_time_millis() {
|
||||
let now = current_time_millis();
|
||||
|
||||
let millis_from_std = SystemTime::now()
|
||||
.duration_since(time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as i64;
|
||||
let datetime_now = chrono::Utc.timestamp_millis(now);
|
||||
let datetime_std = chrono::Utc.timestamp_millis(millis_from_std);
|
||||
|
||||
assert_eq!(datetime_std.year(), datetime_now.year());
|
||||
assert_eq!(datetime_std.month(), datetime_now.month());
|
||||
assert_eq!(datetime_std.day(), datetime_now.day());
|
||||
assert_eq!(datetime_std.hour(), datetime_now.hour());
|
||||
assert_eq!(datetime_std.minute(), datetime_now.minute());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user