use crate::tackler;
use jiff::Zoned;
use jiff::fmt::strtime;
use jiff::tz::{Offset, TimeZone};
pub static TZ_UTC: Offset = jiff::tz::Offset::UTC;
#[derive(Debug, Copy, Clone, Default)]
pub enum TimestampStyle {
Date,
Secodns,
#[default]
Full,
}
impl TimestampStyle {
pub const DATE: &'static str = "date";
pub const SECONDS: &'static str = "seconds";
pub const FULL: &'static str = "full";
pub fn from(name: &str) -> Result<Self, tackler::Error> {
match name {
TimestampStyle::DATE => Ok(TimestampStyle::Date),
TimestampStyle::SECONDS => Ok(TimestampStyle::Secodns),
TimestampStyle::FULL => Ok(TimestampStyle::Full),
_ => Err(format!(
"Unknown timestamp style: '{name}'. Valid options are: {}, {}, {}",
Self::DATE,
Self::SECONDS,
Self::FULL
)
.into()),
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub enum GroupBy {
Year,
#[default]
Month,
Date,
IsoWeek,
IsoWeekDate,
}
impl GroupBy {
pub const YEAR: &'static str = "year";
pub const MONTH: &'static str = "month";
pub const DATE: &'static str = "date";
pub const ISO_WEEK: &'static str = "iso-week";
pub const ISO_WEEK_DATE: &'static str = "iso-week-date";
pub fn from(group_by: &str) -> Result<GroupBy, tackler::Error> {
match group_by {
GroupBy::ISO_WEEK_DATE => Ok(GroupBy::IsoWeekDate),
GroupBy::ISO_WEEK => Ok(GroupBy::IsoWeek),
GroupBy::DATE => Ok(GroupBy::Date),
GroupBy::MONTH => Ok(GroupBy::Month),
GroupBy::YEAR => Ok(GroupBy::Year),
_ => {
let msg = format!(
"Unknown group-by selector: '{group_by}'. Valid options are: {}, {}, {}, {}, {}",
GroupBy::ISO_WEEK_DATE,
GroupBy::ISO_WEEK,
GroupBy::DATE,
GroupBy::MONTH,
GroupBy::YEAR
);
Err(msg.into())
}
}
}
}
pub fn rfc3339_to_zoned(rfc3339_str: &str) -> Result<Zoned, tackler::Error> {
strtime::parse("%Y-%m-%dT%H:%M:%S%.f%:z", rfc3339_str)?
.to_zoned()
.map_err(|e| {
let msg = format!("Can't parse ts as rfc3339: '{rfc3339_str}', error: {e}");
msg.into()
})
}
#[must_use]
pub fn rfc_3339(ts: &Zoned) -> String {
strtime::format("%Y-%m-%dT%H:%M:%S%.f%:z", ts)
.unwrap_or_else(|err| format!("IE: rfc_3339, frmt error: {err}"))
}
#[must_use]
pub fn seconds_tz(ts: &Zoned) -> String {
strtime::format("%Y-%m-%d %H:%M:%S %:z", ts)
.unwrap_or_else(|err| format!("IE: seconds_tz, frmt error: {err}"))
}
#[must_use]
pub fn full_tz(ts: &Zoned) -> String {
strtime::format("%Y-%m-%d %H:%M:%S%.f %:z", ts)
.unwrap_or_else(|err| format!("IE: full_tz, frmt error: {err}"))
}
fn fmt_seconds(ts: &Zoned) -> String {
strtime::format("%Y-%m-%d %H:%M:%S", ts)
.unwrap_or_else(|err| format!("IE: fmt_seconds, frmt error: {err}"))
}
fn fmt_full(ts: &Zoned) -> String {
strtime::format("%Y-%m-%d %H:%M:%S%.f", ts)
.unwrap_or_else(|err| format!("IE: fmt_full, frmt error: {err}"))
}
fn fmt_date(ts: &Zoned) -> String {
strtime::format("%Y-%m-%d", ts).unwrap_or_else(|err| format!("IE: fmt_date, frmt error: {err}"))
}
fn fmt_month(ts: &Zoned) -> String {
strtime::format("%Y-%m", ts).unwrap_or_else(|err| format!("IE: fmt_month, frmt error: {err}"))
}
fn fmt_year(ts: &Zoned) -> String {
strtime::format("%Y", ts).unwrap_or_else(|err| format!("IE: fmt_year, frmt error: {err}"))
}
fn fmt_week(ts: &Zoned) -> String {
let iso_date = ts.date().iso_week_date();
let y = iso_date.year();
let w = iso_date.week();
format!("{y}-W{w:02}")
}
fn fmt_week_date(ts: &Zoned) -> String {
let iso_date = ts.date().iso_week_date();
let y = iso_date.year();
let w = iso_date.week();
let wd = iso_date.weekday().to_monday_one_offset();
format!("{y}-W{w:02}-{wd}")
}
#[must_use]
pub fn as_utc_seconds(ts: &Zoned) -> String {
fmt_seconds(&ts.with_time_zone(TZ_UTC.to_time_zone().clone()))
}
#[must_use]
pub fn as_utc_full(ts: &Zoned) -> String {
fmt_full(&ts.with_time_zone(TZ_UTC.to_time_zone().clone()))
}
#[must_use]
pub fn as_utc_date(ts: &Zoned) -> String {
fmt_date(&ts.with_time_zone(TZ_UTC.to_time_zone().clone()))
}
#[must_use]
pub fn as_utc_month(ts: &Zoned) -> String {
fmt_month(&ts.with_time_zone(TZ_UTC.to_time_zone().clone()))
}
#[must_use]
pub fn as_utc_year(ts: &Zoned) -> String {
fmt_year(&ts.with_time_zone(TZ_UTC.to_time_zone().clone()))
}
#[must_use]
pub fn as_utc_iso_week(ts: &Zoned) -> String {
fmt_week(&ts.with_time_zone(TZ_UTC.to_time_zone().clone()))
}
#[must_use]
pub fn as_utc_iso_week_date(ts: &Zoned) -> String {
fmt_week_date(&ts.with_time_zone(TZ_UTC.to_time_zone().clone()))
}
#[must_use]
pub fn as_tz_seconds(ts: &Zoned, tz: TimeZone) -> String {
fmt_seconds(&ts.with_time_zone(tz))
}
#[must_use]
pub fn as_tz_full(ts: &Zoned, tz: TimeZone) -> String {
fmt_full(&ts.with_time_zone(tz))
}
#[must_use]
pub fn as_tz_date(ts: &Zoned, tz: TimeZone) -> String {
fmt_date(&ts.with_time_zone(tz))
}
#[must_use]
pub fn as_tz_month(ts: &Zoned, tz: TimeZone) -> String {
fmt_month(&ts.with_time_zone(tz))
}
#[must_use]
pub fn as_tz_year(ts: &Zoned, tz: TimeZone) -> String {
fmt_year(&ts.with_time_zone(tz))
}
#[must_use]
pub fn as_tz_iso_week(ts: &Zoned, tz: TimeZone) -> String {
fmt_week(&ts.with_time_zone(tz))
}
#[must_use]
pub fn as_tz_iso_week_date(ts: &Zoned, tz: TimeZone) -> String {
fmt_week_date(&ts.with_time_zone(tz))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn timestampstyle_full() {
let tss = TimestampStyle::from("full");
match tss {
Ok(TimestampStyle::Full) => (),
_ => panic!("/*:test:*/"),
}
}
#[test]
fn timestampstyle_seconds() {
let tss = TimestampStyle::from("seconds");
match tss {
Ok(TimestampStyle::Secodns) => (),
_ => panic!("/*:test:*/"),
}
}
#[test]
fn timestampstyle_date() {
let tss = TimestampStyle::from("date");
match tss {
Ok(TimestampStyle::Date) => (),
_ => panic!("/*:test:*/"),
}
}
#[test]
fn timestampstyle_invalid() {
let tss = TimestampStyle::from("minutes");
assert!(tss.is_err());
}
fn txt2ts(txt_ts: &str) -> Zoned {
strtime::parse("%Y-%m-%dT%H:%M:%S%.f%:z", txt_ts)
.unwrap()
.to_zoned()
.unwrap()
}
#[test]
fn doc_test() {
let ts = txt2ts("2022-12-24T13:14:15+02:00");
assert_eq!(rfc_3339(&ts), "2022-12-24T13:14:15+02:00");
}
#[test]
fn test_rfc_3339() {
assert_eq!(
rfc_3339(&txt2ts("2010-01-02T13:14:15+16:00")),
"2010-01-02T13:14:15+16:00"
);
assert_eq!(
rfc_3339(&txt2ts("2010-12-24T01:02:03.456+16:00")),
"2010-12-24T01:02:03.456+16:00"
);
assert_eq!(
rfc_3339(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010-12-24T01:02:03.456789+16:00"
);
assert_eq!(
rfc_3339(&txt2ts("2010-12-24T01:02:03.700-16:00")),
"2010-12-24T01:02:03.7-16:00"
);
assert_eq!(
rfc_3339(&txt2ts("2010-12-24T00:00:00+00:00")),
"2010-12-24T00:00:00+00:00"
);
assert_eq!(
rfc_3339(&txt2ts("2020-12-31T23:58:59+00:00")),
"2020-12-31T23:58:59+00:00"
);
assert_eq!(
rfc_3339(&txt2ts("2020-12-31T23:58:59+00:00")),
"2020-12-31T23:58:59+00:00"
);
}
#[test]
fn test_seconds_tz() {
assert_eq!(
seconds_tz(&txt2ts("2010-01-02T00:00:00+00:00")),
"2010-01-02 00:00:00 +00:00"
); assert_eq!(
seconds_tz(&txt2ts("2010-12-24T13:14:15+16:00")),
"2010-12-24 13:14:15 +16:00"
);
assert_eq!(
seconds_tz(&txt2ts("2010-12-24T01:02:03.456+16:00")),
"2010-12-24 01:02:03 +16:00"
);
assert_eq!(
seconds_tz(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010-12-24 01:02:03 +16:00"
);
assert_eq!(
seconds_tz(&txt2ts("2020-12-24T23:58:59.123456789+00:00")),
"2020-12-24 23:58:59 +00:00"
); assert_eq!(
seconds_tz(&txt2ts("2010-12-24T01:02:03.700-16:00")),
"2010-12-24 01:02:03 -16:00"
);
assert_eq!(
seconds_tz(&txt2ts("2020-12-31T23:58:59+00:00")),
"2020-12-31 23:58:59 +00:00"
);
}
#[test]
fn test_full_tz() {
assert_eq!(
full_tz(&txt2ts("2010-01-02T13:14:15+16:00")),
"2010-01-02 13:14:15 +16:00"
);
assert_eq!(
full_tz(&txt2ts("2010-12-24T01:02:03.456+16:00")),
"2010-12-24 01:02:03.456 +16:00"
);
assert_eq!(
full_tz(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010-12-24 01:02:03.456789 +16:00"
);
assert_eq!(
full_tz(&txt2ts("2010-12-24T01:02:03.700-16:00")),
"2010-12-24 01:02:03.7 -16:00"
);
assert_eq!(
full_tz(&txt2ts("2010-12-24T00:00:00+00:00")),
"2010-12-24 00:00:00 +00:00"
);
assert_eq!(
full_tz(&txt2ts("2020-12-31T23:58:59.123456789+00:00")),
"2020-12-31 23:58:59.123456789 +00:00"
);
}
#[test]
fn test_fmt_seconds() {
assert_eq!(
fmt_seconds(&txt2ts("2010-01-02T00:00:00+00:00")),
"2010-01-02 00:00:00"
);
assert_eq!(
fmt_seconds(&txt2ts("2010-12-24T13:14:15+16:00")),
"2010-12-24 13:14:15"
);
assert_eq!(
fmt_seconds(&txt2ts("2010-12-24T01:02:03.456+16:00")),
"2010-12-24 01:02:03"
);
assert_eq!(
fmt_seconds(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010-12-24 01:02:03"
);
assert_eq!(
fmt_seconds(&txt2ts("2020-12-24T23:58:59.123456789+00:00")),
"2020-12-24 23:58:59"
);
assert_eq!(
fmt_seconds(&txt2ts("2010-12-24T01:02:03.700-16:00")),
"2010-12-24 01:02:03"
);
}
#[test]
fn test_fmt_full() {
assert_eq!(
fmt_full(&txt2ts("2010-01-02T00:00:00+00:00")),
"2010-01-02 00:00:00"
);
assert_eq!(
fmt_full(&txt2ts("2010-12-24T13:14:15+16:00")),
"2010-12-24 13:14:15"
);
assert_eq!(
fmt_full(&txt2ts("2010-12-24T01:02:03.456+16:00")),
"2010-12-24 01:02:03.456"
);
assert_eq!(
fmt_full(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010-12-24 01:02:03.456789"
);
assert_eq!(
fmt_full(&txt2ts("2020-12-24T23:58:59.123456789+00:00")),
"2020-12-24 23:58:59.123456789"
);
assert_eq!(
fmt_full(&txt2ts("2010-12-24T01:02:03.700-16:00")),
"2010-12-24 01:02:03.7"
);
}
#[test]
fn test_fmt_date() {
assert_eq!(fmt_date(&txt2ts("2010-01-02T00:00:00+00:00")), "2010-01-02");
assert_eq!(fmt_date(&txt2ts("2010-12-24T13:14:15+16:00")), "2010-12-24");
assert_eq!(
fmt_date(&txt2ts("2010-12-24T01:02:03.456+16:00")),
"2010-12-24"
);
assert_eq!(
fmt_date(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010-12-24"
);
assert_eq!(
fmt_date(&txt2ts("2020-12-24T23:58:59.123456789+00:00")),
"2020-12-24"
);
assert_eq!(
fmt_date(&txt2ts("2010-12-24T01:02:03.700-16:00")),
"2010-12-24"
);
}
#[test]
fn test_fmt_month() {
assert_eq!(fmt_month(&txt2ts("2010-01-02T00:00:00+00:00")), "2010-01");
assert_eq!(fmt_month(&txt2ts("2010-12-24T13:14:15+16:00")), "2010-12");
assert_eq!(
fmt_month(&txt2ts("2010-12-24T01:02:03.456+16:00")),
"2010-12"
);
assert_eq!(
fmt_month(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010-12"
);
assert_eq!(
fmt_month(&txt2ts("2020-12-24T23:58:59.123456789+00:00")),
"2020-12"
);
assert_eq!(
fmt_month(&txt2ts("2010-12-24T01:02:03.700-16:00")),
"2010-12"
);
}
#[test]
fn test_fmt_year() {
assert_eq!(fmt_year(&txt2ts("2010-01-02T00:00:00+00:00")), "2010");
assert_eq!(fmt_year(&txt2ts("2010-12-24T13:14:15+16:00")), "2010");
assert_eq!(fmt_year(&txt2ts("2010-12-24T01:02:03.456+16:00")), "2010");
assert_eq!(
fmt_year(&txt2ts("2010-12-24T01:02:03.456789+16:00")),
"2010"
);
assert_eq!(
fmt_year(&txt2ts("2020-12-24T23:58:59.123456789+00:00")),
"2020"
);
assert_eq!(fmt_year(&txt2ts("2010-12-24T01:02:03.700-16:00")), "2010");
}
#[test]
fn test_fmt_week() {
assert_eq!(fmt_week(&txt2ts("2010-01-03T00:00:00+00:00")), "2009-W53");
assert_eq!(fmt_week(&txt2ts("2010-01-04T00:00:00+00:00")), "2010-W01");
assert_eq!(fmt_week(&txt2ts("2017-01-01T00:00:00+00:00")), "2016-W52");
assert_eq!(fmt_week(&txt2ts("2017-01-02T00:00:00+00:00")), "2017-W01");
}
#[test]
fn test_fmt_week_date() {
assert_eq!(
fmt_week_date(&txt2ts("2010-01-03T00:00:00+00:00")),
"2009-W53-7"
);
assert_eq!(
fmt_week_date(&txt2ts("2010-01-04T00:00:00+00:00")),
"2010-W01-1"
);
assert_eq!(
fmt_week_date(&txt2ts("2017-01-01T00:00:00+00:00")),
"2016-W52-7"
);
assert_eq!(
fmt_week_date(&txt2ts("2017-01-02T00:00:00+00:00")),
"2017-W01-1"
);
assert_eq!(
fmt_week_date(&txt2ts("2020-12-31T00:00:00+00:00")),
"2020-W53-4"
);
assert_eq!(
fmt_week_date(&txt2ts("2021-01-01T00:00:00+00:00")),
"2020-W53-5"
);
}
#[test]
fn test_utc_seconds() {
assert_eq!(
as_utc_seconds(&txt2ts("2010-01-01T00:00:00+16:00")),
"2009-12-31 08:00:00"
);
assert_eq!(
as_utc_seconds(&txt2ts("2010-01-02T14:15:16+00:00")),
"2010-01-02 14:15:16"
);
assert_eq!(
as_utc_seconds(&txt2ts("2010-01-01T01:02:03.700-16:00")),
"2010-01-01 17:02:03"
);
}
#[test]
fn test_utc_full() {
assert_eq!(
as_utc_full(&txt2ts("2010-01-01T00:00:00+16:00")),
"2009-12-31 08:00:00"
);
assert_eq!(
as_utc_full(&txt2ts("2010-01-02T14:15:16.456+00:00")),
"2010-01-02 14:15:16.456"
);
assert_eq!(
as_utc_full(&txt2ts("2010-01-01T01:02:03.700-16:00")),
"2010-01-01 17:02:03.7"
);
assert_eq!(
as_utc_full(&txt2ts("2020-12-31T23:58:59.123456789+00:00")),
"2020-12-31 23:58:59.123456789"
);
assert_eq!(
as_utc_full(&txt2ts("2020-12-31T23:58:59.123456789+00:00")),
"2020-12-31 23:58:59.123456789"
);
}
#[test]
fn test_utc_week() {
assert_eq!(
as_utc_iso_week(&txt2ts("2010-01-03T00:00:00+00:00")),
"2009-W53"
);
assert_eq!(
as_utc_iso_week(&txt2ts("2017-01-02T00:00:00+00:00")),
"2017-W01"
);
assert_eq!(
as_utc_iso_week(&txt2ts("2017-01-02T00:00:00+02:00")),
"2016-W52"
);
assert_eq!(
as_utc_iso_week(&txt2ts("2017-01-02T00:00:00-02:00")),
"2017-W01"
);
}
#[test]
fn test_utc_week_date() {
assert_eq!(
as_utc_iso_week_date(&txt2ts("2010-01-03T00:00:00+00:00")),
"2009-W53-7"
);
assert_eq!(
as_utc_iso_week_date(&txt2ts("2017-01-02T00:00:00+00:00")),
"2017-W01-1"
);
assert_eq!(
as_utc_iso_week_date(&txt2ts("2017-01-02T00:00:00+02:00")),
"2016-W52-7"
);
assert_eq!(
as_utc_iso_week_date(&txt2ts("2017-01-01T22:00:00+00:00")),
"2016-W52-7"
);
assert_eq!(
as_utc_iso_week_date(&txt2ts("2017-01-01T22:00:00-02:00")),
"2017-W01-1"
);
}
#[test]
fn test_utc_date() {
assert_eq!(
as_utc_date(&txt2ts("2010-01-01T15:00:00+16:00")),
"2009-12-31"
);
assert_eq!(
as_utc_date(&txt2ts("2010-01-01T08:02:03-16:00")),
"2010-01-02"
);
assert_eq!(
as_utc_date(&txt2ts("2020-12-31T23:59:59.999999999+00:00")),
"2020-12-31"
);
}
#[test]
fn test_utc_month() {
assert_eq!(
as_utc_month(&txt2ts("2010-01-01T15:00:00+16:00")),
"2009-12"
);
assert_eq!(
as_utc_month(&txt2ts("2010-01-31T08:02:03-16:00")),
"2010-02"
);
assert_eq!(
as_utc_month(&txt2ts("2020-12-31T23:59:59.999999999+00:00")),
"2020-12"
);
}
#[test]
fn test_utc_year() {
assert_eq!(as_utc_year(&txt2ts("2010-01-01T15:00:00+16:00")), "2009");
assert_eq!(as_utc_year(&txt2ts("2010-12-31T08:02:03-16:00")), "2011");
assert_eq!(
as_utc_year(&txt2ts("2020-12-31T23:59:59.999999999+00:00")),
"2020"
);
}
#[test]
fn test_zoned_seconds() {
let utc_tz = TimeZone::get("UTC").unwrap();
let helsinki_tz = TimeZone::get("Europe/Helsinki").unwrap();
assert_eq!(
as_tz_seconds(&txt2ts("2010-01-02T00:00:00+00:00"), utc_tz.clone()),
"2010-01-02 00:00:00"
);
assert_eq!(
as_tz_seconds(&txt2ts("2010-01-02T00:00:00+00:00"), helsinki_tz.clone()),
"2010-01-02 02:00:00"
);
assert_eq!(
as_tz_seconds(&txt2ts("2022-06-24T00:00:00+00:00"), utc_tz.clone()),
"2022-06-24 00:00:00"
);
assert_eq!(
as_tz_seconds(&txt2ts("2022-06-24T00:00:00+00:00"), helsinki_tz.clone()),
"2022-06-24 03:00:00"
);
assert_eq!(
as_tz_seconds(&txt2ts("2010-01-02T00:00:00+16:00"), utc_tz),
"2010-01-01 08:00:00"
);
assert_eq!(
as_tz_seconds(&txt2ts("2010-01-02T00:00:00+16:00"), helsinki_tz),
"2010-01-01 10:00:00"
);
}
#[test]
fn test_zoned_full() {
let utc_tz = TimeZone::get("UTC").unwrap();
let helsinki_tz = TimeZone::get("Europe/Helsinki").unwrap();
assert_eq!(
as_tz_full(&txt2ts("2010-01-02T00:00:00+00:00"), utc_tz.clone()),
"2010-01-02 00:00:00"
);
assert_eq!(
as_tz_full(&txt2ts("2010-01-02T00:00:00+00:00"), helsinki_tz.clone()),
"2010-01-02 02:00:00"
);
assert_eq!(
as_tz_full(&txt2ts("2022-06-24T00:00:00+00:00"), utc_tz.clone()),
"2022-06-24 00:00:00"
);
assert_eq!(
as_tz_full(&txt2ts("2022-06-24T00:00:00+00:00"), helsinki_tz.clone()),
"2022-06-24 03:00:00"
);
assert_eq!(
as_tz_full(&txt2ts("2010-01-02T00:00:00.123+16:00"), utc_tz),
"2010-01-01 08:00:00.123"
);
assert_eq!(
as_tz_full(&txt2ts("2010-01-02T00:00:00.123+16:00"), helsinki_tz),
"2010-01-01 10:00:00.123"
);
}
#[test]
fn test_zoned_date() {
let utc_tz = TimeZone::get("UTC").unwrap();
let helsinki_tz = TimeZone::get("Europe/Helsinki").unwrap();
assert_eq!(
as_tz_date(&txt2ts("2010-01-02T00:00:00+00:00"), utc_tz.clone()),
"2010-01-02"
);
assert_eq!(
as_tz_date(
&txt2ts("2009-12-31T21:59:59.999999999+00:00"),
helsinki_tz.clone()
),
"2009-12-31"
);
assert_eq!(
as_tz_date(&txt2ts("2009-12-31T22:00:00+00:00"), helsinki_tz.clone()),
"2010-01-01"
);
assert_eq!(
as_tz_date(&txt2ts("2022-06-24T00:00:00+00:00"), utc_tz),
"2022-06-24"
);
assert_eq!(
as_tz_date(&txt2ts("2022-06-23T21:00:00+00:00"), helsinki_tz),
"2022-06-24"
);
}
#[test]
fn test_zoned_month() {
let utc_tz = TimeZone::get("UTC").unwrap();
let helsinki_tz = TimeZone::get("Europe/Helsinki").unwrap();
assert_eq!(
as_tz_month(&txt2ts("2010-01-02T00:00:00+00:00"), utc_tz.clone()),
"2010-01"
);
assert_eq!(
as_tz_month(
&txt2ts("2009-12-31T21:59:59.999999999+00:00"),
helsinki_tz.clone()
),
"2009-12"
);
assert_eq!(
as_tz_month(&txt2ts("2009-12-31T22:00:00+00:00"), helsinki_tz.clone()),
"2010-01"
);
assert_eq!(
as_tz_month(&txt2ts("2022-06-30T00:00:00+00:00"), utc_tz),
"2022-06"
);
assert_eq!(
as_tz_month(&txt2ts("2022-06-30T21:00:00+00:00"), helsinki_tz),
"2022-07"
);
}
#[test]
fn test_zoned_year() {
let utc_tz = TimeZone::get("UTC").unwrap();
let helsinki_tz = TimeZone::get("Europe/Helsinki").unwrap();
assert_eq!(
as_tz_year(&txt2ts("2010-01-02T00:00:00+00:00"), utc_tz),
"2010"
);
assert_eq!(
as_tz_year(
&txt2ts("2009-12-31T21:59:59.999999999+00:00"),
helsinki_tz.clone()
),
"2009"
);
assert_eq!(
as_tz_year(&txt2ts("2009-12-31T22:00:00+00:00"), helsinki_tz),
"2010"
);
}
#[test]
fn test_zoned_week() {
let utc_tz = TimeZone::get("UTC").unwrap();
let helsinki_tz = TimeZone::get("Europe/Helsinki").unwrap();
assert_eq!(
as_tz_iso_week(&txt2ts("2010-01-03T00:00:00+00:00"), utc_tz.clone()),
"2009-W53"
);
assert_eq!(
as_tz_iso_week(
&txt2ts("2010-01-03T21:59:59.999999999+00:00"),
helsinki_tz.clone()
),
"2009-W53"
);
assert_eq!(
as_tz_iso_week(&txt2ts("2010-01-04T00:00:00+00:00"), utc_tz.clone()),
"2010-W01"
);
assert_eq!(
as_tz_iso_week(&txt2ts("2010-01-03T22:00:00+00:00"), helsinki_tz.clone()),
"2010-W01"
);
assert_eq!(
as_tz_iso_week(
&txt2ts("2022-06-19T23:59:59.999999999+00:00"),
utc_tz.clone()
),
"2022-W24"
);
assert_eq!(
as_tz_iso_week(
&txt2ts("2022-06-19T20:59:59.999999999+00:00"),
helsinki_tz.clone()
),
"2022-W24"
);
assert_eq!(
as_tz_iso_week(&txt2ts("2022-06-20T00:00:00+00:00"), utc_tz.clone()),
"2022-W25"
);
assert_eq!(
as_tz_iso_week(&txt2ts("2022-06-19T21:00:00+00:00"), helsinki_tz.clone()),
"2022-W25"
);
}
#[test]
fn test_zoned_week_date() {
let utc_tz = TimeZone::get("UTC").unwrap();
let helsinki_tz = TimeZone::get("Europe/Helsinki").unwrap();
assert_eq!(
as_tz_iso_week_date(&txt2ts("2010-01-03T00:00:00+00:00"), utc_tz.clone()),
"2009-W53-7"
);
assert_eq!(
as_tz_iso_week_date(
&txt2ts("2010-01-03T21:59:59.999999999+00:00"),
helsinki_tz.clone()
),
"2009-W53-7"
);
assert_eq!(
as_tz_iso_week_date(&txt2ts("2010-01-04T00:00:00+00:00"), utc_tz.clone()),
"2010-W01-1"
);
assert_eq!(
as_tz_iso_week_date(&txt2ts("2010-01-03T22:00:00+00:00"), helsinki_tz.clone()),
"2010-W01-1"
);
assert_eq!(
as_tz_iso_week_date(
&txt2ts("2022-06-19T23:59:59.999999999+00:00"),
utc_tz.clone()
),
"2022-W24-7"
);
assert_eq!(
as_tz_iso_week_date(
&txt2ts("2022-06-19T20:59:59.999999999+00:00"),
helsinki_tz.clone()
),
"2022-W24-7"
);
assert_eq!(
as_tz_iso_week_date(&txt2ts("2022-06-20T00:00:00+00:00"), utc_tz.clone()),
"2022-W25-1"
);
assert_eq!(
as_tz_iso_week_date(&txt2ts("2022-06-19T21:00:00+00:00"), helsinki_tz.clone()),
"2022-W25-1"
);
}
}