pub fn from_system_time_ms(time: SystemTime) -> Result<TimeStampMs, Error>Expand description
Convert a std::time::SystemTime to a UNIX timestamp with millisecond precision.
This function converts a std::time::SystemTime instance to a TimeStampMs (Unix timestamp with millisecond precision).
It extracts both the seconds and milliseconds components from the system time.
§Examples
use std::time::{SystemTime, UNIX_EPOCH, Duration};
// Convert the current system time to a timestamp with millisecond precision
let system_time = SystemTime::now();
let timestamp_ms = time_format::from_system_time_ms(system_time).unwrap();
println!("Seconds: {}, Milliseconds: {}", timestamp_ms.seconds, timestamp_ms.milliseconds);
// Convert a specific time with millisecond precision
let specific_time = UNIX_EPOCH + Duration::from_millis(1500000123);
let specific_ts_ms = time_format::from_system_time_ms(specific_time).unwrap();
assert_eq!(specific_ts_ms.seconds, 1500000);
assert_eq!(specific_ts_ms.milliseconds, 123);§Using with TimeStampMs methods
use std::time::{SystemTime, UNIX_EPOCH, Duration};
// Create a precise time: 1500000 seconds and 123 milliseconds after the epoch
let specific_time = UNIX_EPOCH + Duration::from_millis(1500000123);
// Convert to TimeStampMs
let ts_ms = time_format::from_system_time_ms(specific_time).unwrap();
// Get total milliseconds
let total_ms = ts_ms.total_milliseconds();
assert_eq!(total_ms, 1500000123);§Formatting timestamps with millisecond precision
You can format a timestamp with millisecond precision:
use std::time::{SystemTime, UNIX_EPOCH, Duration};
// Create a specific timestamp with millisecond precision
// We'll use a fixed timestamp rather than a date calculation to avoid test failures
let ts_ms = time_format::TimeStampMs::new(1743087045, 678);
// Format with milliseconds using your preferred pattern
let formatted = time_format::strftime_ms_utc("%Y-%m-%d %H:%M:%S.{ms}", ts_ms).unwrap();
// Verify the milliseconds are included
assert!(formatted.contains(".678"));
// Format as ISO 8601 with milliseconds
let iso8601_ms = time_format::format_iso8601_ms_utc(ts_ms).unwrap();
assert!(iso8601_ms.ends_with(".678Z"));
// Use with common date formats
let rfc3339 = time_format::format_common_ms_utc(ts_ms, time_format::DateFormat::RFC3339).unwrap();
assert!(rfc3339.contains(".678"));§Converting between TimeStamp and TimeStampMs
use std::time::{SystemTime, UNIX_EPOCH, Duration};
// Create a system time with millisecond precision
let system_time = UNIX_EPOCH + Duration::from_millis(1673793045678);
// Convert to TimeStampMs
let ts_ms = time_format::from_system_time_ms(system_time).unwrap();
assert_eq!(ts_ms.seconds, 1673793045);
assert_eq!(ts_ms.milliseconds, 678);
// Convert to TimeStamp (loses millisecond precision)
let ts = time_format::from_system_time(system_time).unwrap();
assert_eq!(ts, 1673793045);
// Convert from TimeStamp to TimeStampMs
let ts_ms_from_ts = time_format::TimeStampMs::from_timestamp(ts);
assert_eq!(ts_ms_from_ts.seconds, ts);
assert_eq!(ts_ms_from_ts.milliseconds, 0); // milliseconds are lost