pub struct PoSQLTimestamp { /* private fields */ }Expand description
Defines an RFC3339-formatted timestamp Represents a fully parsed timestamp with detailed time unit and timezone information
Implementations§
Source§impl PoSQLTimestamp
impl PoSQLTimestamp
Sourcepub fn timeunit(&self) -> PoSQLTimeUnit
pub fn timeunit(&self) -> PoSQLTimeUnit
Returns the PoSQLTimeUnit for this timestamp
Sourcepub fn timezone(&self) -> PoSQLTimeZone
pub fn timezone(&self) -> PoSQLTimeZone
Returns the PoSQLTimeZone for this timestamp
Sourcepub fn try_from(timestamp_str: &str) -> Result<Self, PoSQLTimestampError>
pub fn try_from(timestamp_str: &str) -> Result<Self, PoSQLTimestampError>
Attempts to parse a timestamp string into an PoSQLTimestamp structure.
This function supports two primary formats:
-
RFC 3339 Parsing:
- Parses the timestamp along with its timezone.
- If parsing succeeds, it extracts the timezone offset using
dt.offset().local_minus_utc()and then uses this to construct the appropriatePoSQLTimeZone.
-
Timezone Parsing and Conversion:
- The
from_offsetmethod is used to determine whether the timezone should be represented asUtcorFixedOffset. This function simplifies the decision based on the offset value.
- The
§Errors
This function returns a PoSQLTimestampError in the following cases:
- Parsing Error: Returns
PoSQLTimestampError::ParsingErrorif the input string does not conform to the RFC 3339 format or if the timestamp cannot be parsed due to invalid formatting. This error includes the original parsing error message for further details.
§Examples
use chrono::{DateTime, Utc};
use proof_of_sql_parser::posql_time::{PoSQLTimestamp, PoSQLTimeZone};
// Parsing an RFC 3339 timestamp without a timezone:
let timestamp_str = "2009-01-03T18:15:05Z";
let intermediate_timestamp = PoSQLTimestamp::try_from(timestamp_str).unwrap();
assert_eq!(intermediate_timestamp.timezone(), PoSQLTimeZone::utc());
// Parsing an RFC 3339 timestamp with a positive timezone offset:
let timestamp_str_with_tz = "2009-01-03T18:15:05+03:00";
let intermediate_timestamp = PoSQLTimestamp::try_from(timestamp_str_with_tz).unwrap();
assert_eq!(intermediate_timestamp.timezone(), PoSQLTimeZone::new(10800)); // 3 hours in secondsSourcepub fn to_timestamp(epoch: i64) -> Result<Self, PoSQLTimestampError>
pub fn to_timestamp(epoch: i64) -> Result<Self, PoSQLTimestampError>
Attempts to parse a timestamp string into an PoSQLTimestamp structure.
This function supports two primary formats:
Unix Epoch Time Parsing:
- Since Unix epoch timestamps don’t inherently carry timezone information, any Unix time parsed directly from an integer is assumed to be in UTC.
§Errors
This function returns a PoSQLTimestampError in the following cases:
-
Ambiguous Time: Returns
PoSQLTimestampError::Ambiguousif the provided epoch time corresponds to a time that is ambiguous (e.g., during a daylight saving time change where the local time could correspond to two different UTC times). -
Non-Existent Local Time: Returns
PoSQLTimestampError::LocalTimeDoesNotExistif the provided epoch time corresponds to a time that does not exist in the local time zone (e.g., during a daylight saving time change where a certain local time is skipped).
§Examples
use chrono::{DateTime, Utc};
use proof_of_sql_parser::posql_time::{PoSQLTimestamp, PoSQLTimeZone};
// Parsing a Unix epoch timestamp (assumed to be seconds and UTC):
let unix_time = 1231006505;
let intermediate_timestamp = PoSQLTimestamp::to_timestamp(unix_time).unwrap();
assert_eq!(intermediate_timestamp.timezone(), PoSQLTimeZone::utc());Trait Implementations§
Source§impl Clone for PoSQLTimestamp
impl Clone for PoSQLTimestamp
Source§fn clone(&self) -> PoSQLTimestamp
fn clone(&self) -> PoSQLTimestamp
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source. Read more