use crate::format::TimeFormatter;
use crate::Error;
use super::{check_all, check_format, get_format_err, MockTime};
#[test]
#[rustfmt::skip]
fn test_format_year_4_digits() {
let times = [
MockTime { year: -1111, ..Default::default() },
MockTime { year: -11, ..Default::default() },
MockTime { year: 1, ..Default::default() },
MockTime { year: 1111, ..Default::default() },
];
check_all(×, "'%Y'", &["'-1111'", "'-0011'", "'0001'", "'1111'"]);
check_all(×, "'%1Y'", &["'-1111'", "'-11'", "'1'", "'1111'"]);
check_all(×, "'%4Y'", &["'-1111'", "'-011'", "'0001'", "'1111'"]);
check_all(×, "'%-_5Y'", &["'-1111'", "'-11'", "'1'", "'1111'"]);
check_all(×, "'%-05Y'", &["'-1111'", "'-11'", "'1'", "'1111'"]);
check_all(×, "'%0_5Y'", &["'-1111'", "' -11'", "' 1'", "' 1111'"]);
check_all(×, "'%_05Y'", &["'-1111'", "'-0011'", "'00001'", "'01111'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_year_div_100() {
let times = [
MockTime { year: -1111, ..Default::default() },
MockTime { year: -11, ..Default::default() },
MockTime { year: 1, ..Default::default() },
MockTime { year: 1111, ..Default::default() },
];
check_all(×, "'%C'", &["'-12'", "'-1'", "'00'", "'11'"]);
check_all(×, "'%1C'", &["'-12'", "'-1'", "'0'", "'11'"]);
check_all(×, "'%4C'", &["'-012'", "'-001'", "'0000'", "'0011'"]);
check_all(×, "'%-_4C'", &["'-12'", "'-1'", "'0'", "'11'"]);
check_all(×, "'%-04C'", &["'-12'", "'-1'", "'0'", "'11'"]);
check_all(×, "'%0_4C'", &["' -12'", "' -1'", "' 0'", "' 11'"]);
check_all(×, "'%_04C'", &["'-012'", "'-001'", "'0000'", "'0011'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_year_rem_100() {
let times = [
MockTime { year: -1111, ..Default::default() },
MockTime { year: -11, ..Default::default() },
MockTime { year: 1, ..Default::default() },
MockTime { year: 1111, ..Default::default() },
];
check_all(×, "'%y'", &["'89'", "'89'", "'01'", "'11'"]);
check_all(×, "'%1y'", &["'89'", "'89'", "'1'", "'11'"]);
check_all(×, "'%4y'", &["'0089'", "'0089'", "'0001'", "'0011'"]);
check_all(×, "'%-_y'", &["'89'", "'89'", "'1'", "'11'"]);
check_all(×, "'%-0y'", &["'89'", "'89'", "'1'", "'11'"]);
check_all(×, "'%0_y'", &["'89'", "'89'", "' 1'", "'11'"]);
check_all(×, "'%_0y'", &["'89'", "'89'", "'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_month() {
let times = [
MockTime { month: 1, ..Default::default() },
MockTime { month: 11, ..Default::default() },
];
check_all(×, "'%m'", &["'01'", "'11'"]);
check_all(×, "'%1m'", &["'1'", "'11'"]);
check_all(×, "'%4m'", &["'0001'", "'0011'"]);
check_all(×, "'%-_m'", &["'1'", "'11'"]);
check_all(×, "'%-0m'", &["'1'", "'11'"]);
check_all(×, "'%0_m'", &["' 1'", "'11'"]);
check_all(×, "'%_0m'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_month_name() {
let times = [MockTime { month: 7, ..Default::default() }];
check_all(×, "'%B'", &["'July'"]);
check_all(×, "'%1B'", &["'July'"]);
check_all(×, "'%6B'", &["' July'"]);
check_all(×, "'%-_#^6B'", &["'JULY'"]);
check_all(×, "'%-0^6B'", &["'JULY'"]);
check_all(×, "'%0_#6B'", &["' JULY'"]);
check_all(×, "'%_06B'", &["'00July'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_month_name_abbr() {
let times = [MockTime { month: 7, ..Default::default() }];
check_all(×, "'%b'", &["'Jul'"]);
check_all(×, "'%1b'", &["'Jul'"]);
check_all(×, "'%6b'", &["' Jul'"]);
check_all(×, "'%##b'", &["'JUL'"]);
check_all(×, "'%-_#^6b'", &["'JUL'"]);
check_all(×, "'%-0^6b'", &["'JUL'"]);
check_all(×, "'%0_#6b'", &["' JUL'"]);
check_all(×, "'%_06b'", &["'000Jul'"]);
check_all(×, "'%h'", &["'Jul'"]);
check_all(×, "'%1h'", &["'Jul'"]);
check_all(×, "'%6h'", &["' Jul'"]);
check_all(×, "'%-_#^6h'", &["'JUL'"]);
check_all(×, "'%-0^6h'", &["'JUL'"]);
check_all(×, "'%0_#6h'", &["' JUL'"]);
check_all(×, "'%_06h'", &["'000Jul'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_month_day_zero() {
let times = [
MockTime { day: 1, ..Default::default() },
MockTime { day: 11, ..Default::default() },
];
check_all(×, "'%d'", &["'01'", "'11'"]);
check_all(×, "'%1d'", &["'1'", "'11'"]);
check_all(×, "'%4d'", &["'0001'", "'0011'"]);
check_all(×, "'%-_d'", &["'1'", "'11'"]);
check_all(×, "'%-0d'", &["'1'", "'11'"]);
check_all(×, "'%0_d'", &["' 1'", "'11'"]);
check_all(×, "'%_0d'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_month_day_space() {
let times = [
MockTime { day: 1, ..Default::default() },
MockTime { day: 11, ..Default::default() },
];
check_all(×, "'%e'", &["' 1'", "'11'"]);
check_all(×, "'%1e'", &["'1'", "'11'"]);
check_all(×, "'%4e'", &["' 1'", "' 11'"]);
check_all(×, "'%-_e'", &["'1'", "'11'"]);
check_all(×, "'%-0e'", &["'1'", "'11'"]);
check_all(×, "'%0_e'", &["' 1'", "'11'"]);
check_all(×, "'%_0e'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_year_day() {
let times = [
MockTime { day_of_year: 1, ..Default::default() },
MockTime { day_of_year: 300, ..Default::default() },
];
check_all(×, "'%j'", &["'001'", "'300'"]);
check_all(×, "'%1j'", &["'1'", "'300'"]);
check_all(×, "'%4j'", &["'0001'", "'0300'"]);
check_all(×, "'%-_j'", &["'1'", "'300'"]);
check_all(×, "'%-0j'", &["'1'", "'300'"]);
check_all(×, "'%0_j'", &["' 1'", "'300'"]);
check_all(×, "'%_0j'", &["'001'", "'300'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_hour_24h_zero() {
let times = [
MockTime { hour: 1, ..Default::default() },
MockTime { hour: 11, ..Default::default() },
];
check_all(×, "'%H'", &["'01'", "'11'"]);
check_all(×, "'%1H'", &["'1'", "'11'"]);
check_all(×, "'%4H'", &["'0001'", "'0011'"]);
check_all(×, "'%-_H'", &["'1'", "'11'"]);
check_all(×, "'%-0H'", &["'1'", "'11'"]);
check_all(×, "'%0_H'", &["' 1'", "'11'"]);
check_all(×, "'%_0H'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_hour_24h_space() {
let times = [
MockTime { hour: 1, ..Default::default() },
MockTime { hour: 11, ..Default::default() },
];
check_all(×, "'%k'", &["' 1'", "'11'"]);
check_all(×, "'%1k'", &["'1'", "'11'"]);
check_all(×, "'%4k'", &["' 1'", "' 11'"]);
check_all(×, "'%-_k'", &["'1'", "'11'"]);
check_all(×, "'%-0k'", &["'1'", "'11'"]);
check_all(×, "'%0_k'", &["' 1'", "'11'"]);
check_all(×, "'%_0k'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_hour_12h_zero() {
let times = [
MockTime { hour: 14, ..Default::default() },
MockTime { hour: 0, ..Default::default() },
];
check_all(×, "'%I'", &["'02'", "'12'"]);
check_all(×, "'%1I'", &["'2'", "'12'"]);
check_all(×, "'%4I'", &["'0002'", "'0012'"]);
check_all(×, "'%-_I'", &["'2'", "'12'"]);
check_all(×, "'%-0I'", &["'2'", "'12'"]);
check_all(×, "'%0_I'", &["' 2'", "'12'"]);
check_all(×, "'%_0I'", &["'02'", "'12'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_hour_12h_space() {
let times = [
MockTime { hour: 14, ..Default::default() },
MockTime { hour: 0, ..Default::default() },
];
check_all(×, "'%l'", &["' 2'", "'12'"]);
check_all(×, "'%1l'", &["'2'", "'12'"]);
check_all(×, "'%4l'", &["' 2'", "' 12'"]);
check_all(×, "'%-_l'", &["'2'", "'12'"]);
check_all(×, "'%-0l'", &["'2'", "'12'"]);
check_all(×, "'%0_l'", &["' 2'", "'12'"]);
check_all(×, "'%_0l'", &["'02'", "'12'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_meridian_lower() {
let times = [
MockTime { hour: 11, ..Default::default() },
MockTime { hour: 12, ..Default::default() },
];
check_all(×, "'%P'", &["'am'", "'pm'"]);
check_all(×, "'%1P'", &["'am'", "'pm'"]);
check_all(×, "'%4P'", &["' am'", "' pm'"]);
check_all(×, "'%-_#^4P'", &["'AM'", "'PM'"]);
check_all(×, "'%-0^4P'", &["'AM'", "'PM'"]);
check_all(×, "'%0_#4P'", &["' AM'", "' PM'"]);
check_all(×, "'%_04P'", &["'00am'", "'00pm'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_meridian_upper() {
let times = [
MockTime { hour: 11, ..Default::default() },
MockTime { hour: 12, ..Default::default() },
];
check_all(×, "'%p'", &["'AM'", "'PM'"]);
check_all(×, "'%1p'", &["'AM'", "'PM'"]);
check_all(×, "'%4p'", &["' AM'", "' PM'"]);
check_all(×, "'%-_#^4p'", &["'am'", "'pm'"]);
check_all(×, "'%-0^4p'", &["'AM'", "'PM'"]);
check_all(×, "'%0_#4p'", &["' am'", "' pm'"]);
check_all(×, "'%_04p'", &["'00AM'", "'00PM'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_minute() {
let times = [
MockTime { minute: 1, ..Default::default() },
MockTime { minute: 11, ..Default::default() },
];
check_all(×, "'%M'", &["'01'", "'11'"]);
check_all(×, "'%1M'", &["'1'", "'11'"]);
check_all(×, "'%4M'", &["'0001'", "'0011'"]);
check_all(×, "'%-_M'", &["'1'", "'11'"]);
check_all(×, "'%-0M'", &["'1'", "'11'"]);
check_all(×, "'%0_M'", &["' 1'", "'11'"]);
check_all(×, "'%_0M'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_second() {
let times = [
MockTime { second: 1, ..Default::default() },
MockTime { second: 11, ..Default::default() },
];
check_all(×, "'%S'", &["'01'", "'11'"]);
check_all(×, "'%1S'", &["'1'", "'11'"]);
check_all(×, "'%4S'", &["'0001'", "'0011'"]);
check_all(×, "'%-_S'", &["'1'", "'11'"]);
check_all(×, "'%-0S'", &["'1'", "'11'"]);
check_all(×, "'%0_S'", &["' 1'", "'11'"]);
check_all(×, "'%_0S'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_milli_second() {
let times = [
MockTime { nanoseconds: 1, ..Default::default() },
MockTime { nanoseconds: 123_456_789, ..Default::default() },
];
check_all(×, "'%L'", &["'000'", "'123'"]);
check_all(×, "'%00L'", &["'000'", "'123'"]);
check_all(×, "'%0L'", &["'000'", "'123'"]);
check_all(×, "'%1L'", &["'0'", "'1'"]);
check_all(×, "'%2L'", &["'00'", "'12'"]);
check_all(×, "'%3L'", &["'000'", "'123'"]);
check_all(×, "'%4L'", &["'0000'", "'1234'"]);
check_all(×, "'%5L'", &["'00000'", "'12345'"]);
check_all(×, "'%6L'", &["'000000'", "'123456'"]);
check_all(×, "'%7L'", &["'0000000'", "'1234567'"]);
check_all(×, "'%8L'", &["'00000000'", "'12345678'"]);
check_all(×, "'%9L'", &["'000000001'", "'123456789'"]);
check_all(×, "'%12L'", &["'000000001000'", "'123456789000'"]);
check_all(×, "'%-12L'", &["'000000001000'", "'123456789000'"]);
check_all(×, "'%_12L'", &["'000000001000'", "'123456789000'"]);
check_all(×, "'%012L'", &["'000000001000'", "'123456789000'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_fractional_second() {
let times = [
MockTime { nanoseconds: 1, ..Default::default() },
MockTime { nanoseconds: 123_456_789, ..Default::default() },
];
check_all(×, "'%N'", &["'000000001'", "'123456789'"]);
check_all(×, "'%00N'", &["'000000001'", "'123456789'"]);
check_all(×, "'%0N'", &["'000000001'", "'123456789'"]);
check_all(×, "'%1N'", &["'0'", "'1'"]);
check_all(×, "'%2N'", &["'00'", "'12'"]);
check_all(×, "'%3N'", &["'000'", "'123'"]);
check_all(×, "'%4N'", &["'0000'", "'1234'"]);
check_all(×, "'%5N'", &["'00000'", "'12345'"]);
check_all(×, "'%6N'", &["'000000'", "'123456'"]);
check_all(×, "'%7N'", &["'0000000'", "'1234567'"]);
check_all(×, "'%8N'", &["'00000000'", "'12345678'"]);
check_all(×, "'%9N'", &["'000000001'", "'123456789'"]);
check_all(×, "'%12N'", &["'000000001000'", "'123456789000'"]);
check_all(×, "'%-12N'", &["'000000001000'", "'123456789000'"]);
check_all(×, "'%_12N'", &["'000000001000'", "'123456789000'"]);
check_all(×, "'%012N'", &["'000000001000'", "'123456789000'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_time_zone_offset_hour_minute() {
let times = [
MockTime { is_utc: true, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 561, ..Default::default() },
MockTime { is_utc: false, utc_offset: 3600, ..Default::default() },
];
check_all(×, "'%z'", &["'+0000'", "'+0000'", "'+0009'", "'+0100'"]);
check_all(×, "'%1z'", &["'+0000'", "'+0000'", "'+0009'", "'+0100'"]);
check_all(×, "'%6z'", &["'+00000'", "'+00000'", "'+00009'", "'+00100'"]);
check_all(×, "'%-6z'", &["'-00000'", "'+00000'", "'+00009'", "'+00100'"]);
check_all(×, "'%-_6z'", &["' -000'", "' +000'", "' +009'", "' +100'"]);
check_all(×, "'%-06z'", &["'-00000'", "'+00000'", "'+00009'", "'+00100'"]);
check_all(×, "'%0_6z'", &["' +000'", "' +000'", "' +009'", "' +100'"]);
check_all(×, "'%_06z'", &["'+00000'", "'+00000'", "'+00009'", "'+00100'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_time_zone_offset_hour_minute_colon() {
let times = [
MockTime { is_utc: true, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 561, ..Default::default() },
MockTime { is_utc: false, utc_offset: 3600, ..Default::default() },
];
check_all(×, "'%:z'", &["'+00:00'", "'+00:00'", "'+00:09'", "'+01:00'"]);
check_all(×, "'%1:z'", &["'+00:00'", "'+00:00'", "'+00:09'", "'+01:00'"]);
check_all(×, "'%7:z'", &["'+000:00'", "'+000:00'", "'+000:09'", "'+001:00'"]);
check_all(×, "'%-7:z'", &["'-000:00'", "'+000:00'", "'+000:09'", "'+001:00'"]);
check_all(×, "'%-_7:z'", &["' -0:00'", "' +0:00'", "' +0:09'", "' +1:00'"]);
check_all(×, "'%-07:z'", &["'-000:00'", "'+000:00'", "'+000:09'", "'+001:00'"]);
check_all(×, "'%0_7:z'", &["' +0:00'", "' +0:00'", "' +0:09'", "' +1:00'"]);
check_all(×, "'%_07:z'", &["'+000:00'", "'+000:00'", "'+000:09'", "'+001:00'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_time_zone_offset_hour_minute_second_colon() {
let times = [
MockTime { is_utc: true, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 561, ..Default::default() },
MockTime { is_utc: false, utc_offset: 3600, ..Default::default() },
];
check_all(×, "'%::z'", &["'+00:00:00'", "'+00:00:00'", "'+00:09:21'", "'+01:00:00'"]);
check_all(×, "'%1::z'", &["'+00:00:00'", "'+00:00:00'", "'+00:09:21'", "'+01:00:00'"]);
check_all(×, "'%10::z'", &["'+000:00:00'", "'+000:00:00'", "'+000:09:21'", "'+001:00:00'"]);
check_all(×, "'%-10::z'", &["'-000:00:00'", "'+000:00:00'", "'+000:09:21'", "'+001:00:00'"]);
check_all(×, "'%-_10::z'", &["' -0:00:00'", "' +0:00:00'", "' +0:09:21'", "' +1:00:00'"]);
check_all(×, "'%-010::z'", &["'-000:00:00'", "'+000:00:00'", "'+000:09:21'", "'+001:00:00'"]);
check_all(×, "'%0_10::z'", &["' +0:00:00'", "' +0:00:00'", "' +0:09:21'", "' +1:00:00'"]);
check_all(×, "'%_010::z'", &["'+000:00:00'", "'+000:00:00'", "'+000:09:21'", "'+001:00:00'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_time_zone_offset_colon_minimal() {
let times = [
MockTime { is_utc: true, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 0, ..Default::default() },
MockTime { is_utc: false, utc_offset: 540, ..Default::default() },
MockTime { is_utc: false, utc_offset: 561, ..Default::default() },
MockTime { is_utc: false, utc_offset: 3600, ..Default::default() },
];
check_all(×, "'%:::z'", &["'+00'", "'+00'", "'+00:09'", "'+00:09:21'", "'+01'"]);
check_all(×, "'%1:::z'", &["'+00'", "'+00'", "'+00:09'", "'+00:09:21'", "'+01'"]);
check_all(×, "'%10:::z'", &["'+000000000'", "'+000000000'", "'+000000:09'", "'+000:09:21'", "'+000000001'"]);
check_all(×, "'%-10:::z'", &["'-000000000'", "'+000000000'", "'+000000:09'", "'+000:09:21'", "'+000000001'"]);
check_all(×, "'%-_10:::z'", &["' -0'", "' +0'", "' +0:09'", "' +0:09:21'", "' +1'"]);
check_all(×, "'%-010:::z'", &["'-000000000'", "'+000000000'", "'+000000:09'", "'+000:09:21'", "'+000000001'"]);
check_all(×, "'%0_10:::z'", &["' +0'", "' +0'", "' +0:09'", "' +0:09:21'", "' +1'"]);
check_all(×, "'%_010:::z'", &["'+000000000'", "'+000000000'", "'+000000:09'", "'+000:09:21'", "'+000000001'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_time_zone_name() {
let times = [
MockTime { time_zone: "", ..Default::default() },
MockTime { time_zone: "UTC", ..Default::default() },
MockTime { time_zone: "+0000", ..Default::default() },
];
check_all(×, "'%Z'", &["''", "'UTC'" , "'+0000'"]);
check_all(×, "'%1Z'", &["''", "'UTC'" , "'+0000'"]);
check_all(×, "'%6Z'", &["''", "' UTC'", "' +0000'"]);
check_all(×, "'%-_#^6Z'", &["''", "'utc'" , "'+0000'"]);
check_all(×, "'%-0^6Z'", &["''", "'UTC'" , "'+0000'"]);
check_all(×, "'%0_#6Z'", &["''", "' utc'", "' +0000'"]);
check_all(×, "'%_06Z'", &["''", "'000UTC'", "'0+0000'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_week_day_name() {
let times = [MockTime { day_of_week: 1, ..Default::default() }];
check_all(×, "'%A'", &["'Monday'"]);
check_all(×, "'%1A'", &["'Monday'"]);
check_all(×, "'%8A'", &["' Monday'"]);
check_all(×, "'%-_#^8A'", &["'MONDAY'"]);
check_all(×, "'%-0^8A'", &["'MONDAY'"]);
check_all(×, "'%0_#8A'", &["' MONDAY'"]);
check_all(×, "'%_08A'", &["'00Monday'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_week_day_name_abbr() {
let times = [MockTime { day_of_week: 1, ..Default::default() }];
check_all(×, "'%a'", &["'Mon'"]);
check_all(×, "'%1a'", &["'Mon'"]);
check_all(×, "'%8a'", &["' Mon'"]);
check_all(×, "'%-_#^8a'", &["'MON'"]);
check_all(×, "'%-0^8a'", &["'MON'"]);
check_all(×, "'%0_#8a'", &["' MON'"]);
check_all(×, "'%_08a'", &["'00000Mon'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_week_day_from_1() {
let times = [MockTime { day_of_week: 0, ..Default::default() }];
check_all(×, "'%u'", &["'7'"]);
check_all(×, "'%1u'", &["'7'"]);
check_all(×, "'%4u'", &["'0007'"]);
check_all(×, "'%-_4u'", &["'7'"]);
check_all(×, "'%-04u'", &["'7'"]);
check_all(×, "'%0_4u'", &["' 7'"]);
check_all(×, "'%_04u'", &["'0007'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_week_day_from_0() {
let times = [MockTime { day_of_week: 0, ..Default::default() }];
check_all(×, "'%w'", &["'0'"]);
check_all(×, "'%1w'", &["'0'"]);
check_all(×, "'%4w'", &["'0000'"]);
check_all(×, "'%-_4w'", &["'0'"]);
check_all(×, "'%-04w'", &["'0'"]);
check_all(×, "'%0_4w'", &["' 0'"]);
check_all(×, "'%_04w'", &["'0000'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_year_iso_8601() {
let times = [
MockTime { year: -1111, day_of_year: 30, ..Default::default() },
MockTime { year: -11, day_of_year: 30, ..Default::default() },
MockTime { year: 1, day_of_year: 30, ..Default::default() },
MockTime { year: 1111, day_of_year: 30, ..Default::default() },
];
check_all(×, "'%G'", &["'-1111'", "'-0011'", "'0001'", "'1111'"]);
check_all(×, "'%1G'", &["'-1111'", "'-11'", "'1'", "'1111'"]);
check_all(×, "'%4G'", &["'-1111'", "'-011'", "'0001'", "'1111'"]);
check_all(×, "'%-_5G'", &["'-1111'", "'-11'", "'1'", "'1111'"]);
check_all(×, "'%-05G'", &["'-1111'", "'-11'", "'1'", "'1111'"]);
check_all(×, "'%0_5G'", &["'-1111'", "' -11'", "' 1'", "' 1111'"]);
check_all(×, "'%_05G'", &["'-1111'", "'-0011'", "'00001'", "'01111'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_year_iso_8601_rem_100() {
let times = [
MockTime { year: -1111, day_of_year: 30, ..Default::default() },
MockTime { year: -11, day_of_year: 30, ..Default::default() },
MockTime { year: 1, day_of_year: 30, ..Default::default() },
MockTime { year: 1111, day_of_year: 30, ..Default::default() },
];
check_all(×, "'%g'", &["'89'", "'89'", "'01'", "'11'"]);
check_all(×, "'%1g'", &["'89'", "'89'", "'1'", "'11'"]);
check_all(×, "'%4g'", &["'0089'", "'0089'", "'0001'", "'0011'"]);
check_all(×, "'%-_g'", &["'89'", "'89'", "'1'", "'11'"]);
check_all(×, "'%-0g'", &["'89'", "'89'", "'1'", "'11'"]);
check_all(×, "'%0_g'", &["'89'", "'89'", "' 1'", "'11'"]);
check_all(×, "'%_0g'", &["'89'", "'89'", "'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_week_number_iso_8601() {
let times = [
MockTime { year: 2000, day_of_year: 7, ..Default::default() },
MockTime { year: 2000, day_of_year: 80, ..Default::default() },
];
check_all(×, "'%V'", &["'01'", "'11'"]);
check_all(×, "'%1V'", &["'1'", "'11'"]);
check_all(×, "'%4V'", &["'0001'", "'0011'"]);
check_all(×, "'%-_V'", &["'1'", "'11'"]);
check_all(×, "'%-0V'", &["'1'", "'11'"]);
check_all(×, "'%0_V'", &["' 1'", "'11'"]);
check_all(×, "'%_0V'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_week_number_from_sunday() {
let times = [
MockTime { year: 2000, day_of_year: 7, ..Default::default() },
MockTime { year: 2000, day_of_year: 77, ..Default::default() },
];
check_all(×, "'%U'", &["'01'", "'11'"]);
check_all(×, "'%1U'", &["'1'", "'11'"]);
check_all(×, "'%4U'", &["'0001'", "'0011'"]);
check_all(×, "'%-_U'", &["'1'", "'11'"]);
check_all(×, "'%-0U'", &["'1'", "'11'"]);
check_all(×, "'%0_U'", &["' 1'", "'11'"]);
check_all(×, "'%_0U'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_week_number_from_monday() {
let times = [
MockTime { year: 2000, day_of_year: 7, ..Default::default() },
MockTime { year: 2000, day_of_year: 77, ..Default::default() },
];
check_all(×, "'%W'", &["'01'", "'11'"]);
check_all(×, "'%1W'", &["'1'", "'11'"]);
check_all(×, "'%4W'", &["'0001'", "'0011'"]);
check_all(×, "'%-_W'", &["'1'", "'11'"]);
check_all(×, "'%-0W'", &["'1'", "'11'"]);
check_all(×, "'%0_W'", &["' 1'", "'11'"]);
check_all(×, "'%_0W'", &["'01'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_seconds_since_epoch() {
let times = [
MockTime { to_int: 1, ..Default::default() },
MockTime { to_int: 11, ..Default::default() },
];
check_all(×, "'%s'", &["'1'", "'11'"]);
check_all(×, "'%1s'", &["'1'", "'11'"]);
check_all(×, "'%4s'", &["'0001'", "'0011'"]);
check_all(×, "'%-_s'", &["'1'", "'11'"]);
check_all(×, "'%-0s'", &["'1'", "'11'"]);
check_all(×, "'%0_s'", &["'1'", "'11'"]);
check_all(×, "'%_0s'", &["'1'", "'11'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_newline() {
let times = [MockTime::default()];
check_all(×, "'%n'", &["'\n'"]);
check_all(×, "'%1n'", &["'\n'"]);
check_all(×, "'%6n'", &["' \n'"]);
check_all(×, "'%-_#^6n'", &["'\n'"]);
check_all(×, "'%-0^6n'", &["'\n'"]);
check_all(×, "'%0_#6n'", &["' \n'"]);
check_all(×, "'%_06n'", &["'00000\n'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_tabulation() {
let times = [MockTime::default()];
check_all(×, "'%t'", &["'\t'"]);
check_all(×, "'%1t'", &["'\t'"]);
check_all(×, "'%6t'", &["' \t'"]);
check_all(×, "'%-_#^6t'", &["'\t'"]);
check_all(×, "'%-0^6t'", &["'\t'"]);
check_all(×, "'%0_#6t'", &["' \t'"]);
check_all(×, "'%_06t'", &["'00000\t'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_percent() {
let times = [MockTime::default()];
check_all(×, "'%%'", &["'%'"]);
check_all(×, "'%%Q'", &["'%Q'"]);
check_all(×, "'%%%%%%%Q'", &["'%%%%Q'"]);
check_all(×, "'%1%'", &["'%'"]);
check_all(×, "'%6%'", &["' %'"]);
check_all(×, "'%-_#^6%'", &["'%'"]);
check_all(×, "'%-0^6%'", &["'%'"]);
check_all(×, "'%0_#6%'", &["' %'"]);
check_all(×, "'%_06%'", &["'00000%'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_combination_date_time() {
let times = [
MockTime::new(971, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, ""),
MockTime::new(-971, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, ""),
];
check_all(×, "'%c'", &["'Thu Jan 1 00:00:00 0971'", "'Thu Jan 1 00:00:00 -0971'"]);
check_all(×, "'%1c'", &["'Thu Jan 1 00:00:00 0971'", "'Thu Jan 1 00:00:00 -0971'"]);
check_all(×, "'%30c'", &["' Thu Jan 1 00:00:00 0971'", "' Thu Jan 1 00:00:00 -0971'"]);
check_all(×, "'%-^_#30c'", &["' THU JAN 1 00:00:00 0971'", "' THU JAN 1 00:00:00 -0971'"]);
check_all(×, "'%-0^30c'", &["'000000THU JAN 1 00:00:00 0971'", "'00000THU JAN 1 00:00:00 -0971'"]);
check_all(×, "'%0_#30c'", &["' Thu Jan 1 00:00:00 0971'", "' Thu Jan 1 00:00:00 -0971'"]);
check_all(×, "'%_030c'", &["'000000Thu Jan 1 00:00:00 0971'", "'00000Thu Jan 1 00:00:00 -0971'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_combination_date() {
let times = [
MockTime { year: 1234, month: 5, day: 6, ..Default::default() },
MockTime { year: -1234, month: 5, day: 6, ..Default::default() },
];
check_all(×, "'%D'", &["'05/06/34'", "'05/06/66'"]);
check_all(×, "'%1D'", &["'05/06/34'", "'05/06/66'"]);
check_all(×, "'%10D'", &["' 05/06/34'", "' 05/06/66'"]);
check_all(×, "'%-^_#10D'", &["' 05/06/34'", "' 05/06/66'"]);
check_all(×, "'%-0^10D'", &["'0005/06/34'", "'0005/06/66'"]);
check_all(×, "'%0_#10D'", &["' 05/06/34'", "' 05/06/66'"]);
check_all(×, "'%_010D'", &["'0005/06/34'", "'0005/06/66'"]);
check_all(×, "'%x'", &["'05/06/34'", "'05/06/66'"]);
check_all(×, "'%1x'", &["'05/06/34'", "'05/06/66'"]);
check_all(×, "'%10x'", &["' 05/06/34'", "' 05/06/66'"]);
check_all(×, "'%-^_#10x'", &["' 05/06/34'", "' 05/06/66'"]);
check_all(×, "'%-0^10x'", &["'0005/06/34'", "'0005/06/66'"]);
check_all(×, "'%0_#10x'", &["' 05/06/34'", "' 05/06/66'"]);
check_all(×, "'%_010x'", &["'0005/06/34'", "'0005/06/66'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_combination_iso_8601() {
let times = [
MockTime { year: 234, month: 5, day: 6, ..Default::default() },
MockTime { year: -234, month: 5, day: 6, ..Default::default() },
];
check_all(×, "'%F'", &["'0234-05-06'", "'-0234-05-06'"]);
check_all(×, "'%1F'", &["'0234-05-06'", "'-0234-05-06'"]);
check_all(×, "'%12F'", &["' 0234-05-06'", "' -0234-05-06'"]);
check_all(×, "'%-^_#12F'", &["' 0234-05-06'", "' -0234-05-06'"]);
check_all(×, "'%-0^12F'", &["'000234-05-06'", "'0-0234-05-06'"]);
check_all(×, "'%0_#12F'", &["' 0234-05-06'", "' -0234-05-06'"]);
check_all(×, "'%_012F'", &["'000234-05-06'", "'0-0234-05-06'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_combination_vms_date() {
let times = [
MockTime { year: 1234, month: 7, day: 6, ..Default::default() },
MockTime { year: -1234, month: 7, day: 6, ..Default::default() },
];
check_all(×, "'%v'", &["' 6-JUL-1234'", "' 6-JUL--1234'"]);
check_all(×, "'%1v'", &["' 6-JUL-1234'", "' 6-JUL--1234'"]);
check_all(×, "'%13v'", &["' 6-JUL-1234'", "' 6-JUL--1234'"]);
check_all(×, "'%-^_#13v'", &["' 6-JUL-1234'", "' 6-JUL--1234'"]);
check_all(×, "'%-0^13v'", &["'00 6-JUL-1234'", "'0 6-JUL--1234'"]);
check_all(×, "'%0_#13v'", &["' 6-JUL-1234'", "' 6-JUL--1234'"]);
check_all(×, "'%_013v'", &["'00 6-JUL-1234'", "'0 6-JUL--1234'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_combination_time_12h() {
let times = [
MockTime { hour: 11, minute: 2, second: 3, ..Default::default() },
MockTime { hour: 12, minute: 2, second: 3, ..Default::default() },
];
check_all(×, "'%r'", &["'11:02:03 AM'", "'12:02:03 PM'"]);
check_all(×, "'%1r'", &["'11:02:03 AM'", "'12:02:03 PM'"]);
check_all(×, "'%13r'", &["' 11:02:03 AM'", "' 12:02:03 PM'"]);
check_all(×, "'%-^_#13r'", &["' 11:02:03 AM'", "' 12:02:03 PM'"]);
check_all(×, "'%-0^13r'", &["'0011:02:03 AM'", "'0012:02:03 PM'"]);
check_all(×, "'%0_#13r'", &["' 11:02:03 AM'", "' 12:02:03 PM'"]);
check_all(×, "'%_013r'", &["'0011:02:03 AM'", "'0012:02:03 PM'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_combination_hour_minute_24h() {
let times = [MockTime { hour: 13, minute: 2, ..Default::default() }];
check_all(×, "'%R'", &["'13:02'"]);
check_all(×, "'%1R'", &["'13:02'"]);
check_all(×, "'%7R'", &["' 13:02'"]);
check_all(×, "'%-^_#7R'", &["' 13:02'"]);
check_all(×, "'%-0^7R'", &["'0013:02'"]);
check_all(×, "'%0_#7R'", &["' 13:02'"]);
check_all(×, "'%_07R'", &["'0013:02'"]);
}
#[test]
#[rustfmt::skip]
fn test_format_combination_time_24h() {
let times = [MockTime { hour: 13, minute: 2, second: 3, ..Default::default() }];
check_all(×, "'%T'", &["'13:02:03'"]);
check_all(×, "'%1T'", &["'13:02:03'"]);
check_all(×, "'%10T'", &["' 13:02:03'"]);
check_all(×, "'%-^_#10T'", &["' 13:02:03'"]);
check_all(×, "'%-0^10T'", &["'0013:02:03'"]);
check_all(×, "'%0_#10T'", &["' 13:02:03'"]);
check_all(×, "'%_010T'", &["'0013:02:03'"]);
check_all(×, "'%X'", &["'13:02:03'"]);
check_all(×, "'%1X'", &["'13:02:03'"]);
check_all(×, "'%10X'", &["' 13:02:03'"]);
check_all(×, "'%-^_#10X'", &["' 13:02:03'"]);
check_all(×, "'%-0^10X'", &["'0013:02:03'"]);
check_all(×, "'%0_#10X'", &["' 13:02:03'"]);
check_all(×, "'%_010X'", &["'0013:02:03'"]);
}
#[test]
fn test_format_invalid() {
let time = MockTime::default();
for format in ["%", "%-4", "%-", "%-_"] {
let err = get_format_err(&time, format);
assert!(matches!(err, Error::InvalidFormatString));
}
}
#[test]
fn test_format_literal() {
let time = MockTime::default();
check_format(&time, "% ", "% ");
check_format(&time, "%-4 ", "%-4 ");
check_format(&time, "%- ", "%- ");
check_format(&time, "%-_ ", "%-_ ");
check_format(&time, "'%:'", "'%:'");
check_format(&time, "'%::'", "'%::'");
check_format(&time, "'%:::'", "'%:::'");
check_format(&time, "'%:::m'", "'%:::m'");
check_format(&time, "'%::::z'", "'%::::z'");
}
#[test]
fn test_format_with_modifiers() {
let time = MockTime::new(1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, "");
check_format(&time, "%EY, %Oy, %EE, %OO", "1970, 70, %EE, %OO");
}
#[test]
fn test_format_large_width() {
let time = MockTime::new(1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, "");
check_format(&time, "%-100000000m", "1");
check_format(&time, "%2147483648m", "%2147483648m");
let err = get_format_err(&time, "%1000m");
assert!(matches!(err, Error::WriteZero));
}
#[cfg(feature = "alloc")]
#[test]
fn test_format_formatted_string_too_large() {
use alloc::vec::Vec;
let time = MockTime::new(1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, "");
let mut buf = Vec::new();
let result = TimeFormatter::new(&time, "%4718593m").fmt(&mut buf);
assert_eq!(buf.len(), 4_718_592);
assert!(matches!(result, Err(Error::FormattedStringTooLarge)));
}
#[test]
fn test_format_small_buffer() {
let time = MockTime::new(1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, "");
let mut buf = [0u8; 3];
let result = TimeFormatter::new(&time, "%Y").fmt(&mut &mut buf[..]);
assert!(matches!(result, Err(Error::WriteZero)));
}
#[test]
fn test_format_empty() {
let time = MockTime::default();
check_format(&time, "", "");
}
#[test]
fn test_format_copies_non_specifier_bytes() {
let times = [
MockTime::new(1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, ""),
MockTime::new(-1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, ""),
];
check_all(
×,
"The year is '%Y' and the month is '%B' today.",
&[
"The year is '1970' and the month is 'January' today.",
"The year is '-1970' and the month is 'January' today.",
],
);
}
#[test]
fn test_chrono_pr_966_week_numbers() {
let times = [
MockTime::new(2020, 1, 12, 0, 0, 0, 0, 0, 12, 0, false, 0, ""),
MockTime::new(2019, 1, 13, 0, 0, 0, 0, 0, 13, 0, false, 0, ""),
MockTime::new(2007, 12, 31, 0, 0, 0, 0, 1, 365, 0, false, 0, ""),
];
check_all(
×,
"%Y-%W-%w",
&[
"2020-01-0",
"2019-01-0",
"2007-53-1",
],
);
}
#[test]
#[cfg(feature = "alloc")]
fn test_multibyte_utf8_characters_after_percent_treated_as_literal() {
use alloc::string::String;
let time = MockTime::new(1970, 1, 1, 0, 0, 0, 0, 4, 1, 0, false, 0, "");
for format in [
"\u{1e}%\u{02c9}0",
"%\u{076f}\u{2}",
"%\u{01af}",
"%10\u{94}\u{6}",
"%100000\u{94}\u{6}",
] {
let mut buf = String::new();
crate::fmt::strftime(&time, format, &mut buf).unwrap();
assert_eq!(buf, format);
}
}