Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub fn DateTime::end_of_year(Self) -> Self
pub fn DateTime::epoch() -> Self
pub fn DateTime::format(Self) -> String
pub fn DateTime::format_fixed(Self) -> String
pub fn DateTime::format_with(Self, String) -> String raise TempoError
pub fn DateTime::from_unix_micros(Int64) -> Self
pub fn DateTime::from_unix_millis(Int64) -> Self
pub fn DateTime::from_unix_nanos(Int64) -> Self
Expand Down
67 changes: 67 additions & 0 deletions src/tempo.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,73 @@ pub fn DateTime::format(self : DateTime) -> String {
format_datetime_without_zone(self) + "Z"
}

///|
/// Format this DateTime with a brace-token pattern.
///
/// Supported tokens are `{YYYY}`, `{MM}`, `{DD}`, `{HH}`, `{mm}`, `{ss}`,
/// `{fff}`, and `{nnnnnnnnn}`. Non-brace text outside tokens is copied
/// literally. Use `{{` and `}}` to emit literal braces. Unknown tokens and
/// unmatched braces raise `TempoError`.
pub fn DateTime::format_with(
self : DateTime,
pattern : String,
) -> String raise TempoError {
let buf = StringBuilder()
let len = pattern.length()
let mut i = 0
while i < len {
if pattern[i] == '{' {
if i + 1 < len && pattern[i + 1] == '{' {
buf.write_string("{")
i += 2
} else {
let start = i + 1
let mut end = start
while end < len && pattern[end] != '}' {
end += 1
}
if end >= len {
raise TempoError("unterminated datetime format token")
}
let token = pattern[start:end].to_owned()
buf.write_string(format_datetime_pattern_token(self, token))
i = end + 1
}
} else if pattern[i] == '}' {
if i + 1 < len && pattern[i + 1] == '}' {
buf.write_string("}")
i += 2
} else {
raise TempoError("unmatched closing brace in datetime format pattern")
}
} else {
buf.write_string(pattern[i:i + 1].to_owned())
i += 1
}
}
buf.to_string()
}

///|
fn format_datetime_pattern_token(
dt : DateTime,
token : String,
) -> String raise TempoError {
let d = dt.date
let t = dt.time
match token {
"YYYY" => pad4_year(d.year)
"MM" => pad2(d.month)
"DD" => pad2(d.day)
"HH" => pad2(t.hour)
"mm" => pad2(t.minute)
"ss" => pad2(t.second)
"fff" => pad3(t.nanosecond / 1_000_000)
"nnnnnnnnn" => pad9(t.nanosecond)
_ => raise TempoError("unknown datetime format token {\{token}}")
}
}

///|
/// Format this DateTime as a UTC timestamp with a fixed-width fractional
/// second; for years 0..9999, the whole output is fixed-width and
Expand Down
39 changes: 39 additions & 0 deletions src/tempo_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,45 @@ test "DateTime::format supports full Int year range" {
)
}

///|
test "DateTime::format_with tokens and literals" {
let dt = @tempo.DateTime::parse("2024-03-15T12:34:56.123456789Z")
assert_eq(dt.format_with("{YYYY}-{MM}-{DD}"), "2024-03-15")
assert_eq(dt.format_with("{HH}:{mm}:{ss}"), "12:34:56")
assert_eq(dt.format_with("{YYYY}/{MM}/{DD} {HH}h{mm}"), "2024/03/15 12h34")
assert_eq(dt.format_with("{fff} {nnnnnnnnn}"), "123 123456789")
let padded = @tempo.DateTime::parse("2024-03-15T12:34:56.007000009Z")
assert_eq(padded.format_with("{fff}/{nnnnnnnnn}"), "007/007000009")
let expanded = @tempo.DateTime::new(
@tempo.Date::new(-44, 3, 15),
@tempo.Time::new(0, 0, 0, 0),
)
assert_eq(expanded.format_with("{YYYY}-{MM}-{DD}"), "-0044-03-15")
}

///|
test "DateTime::format_with rejects invalid patterns" {
let dt = @tempo.DateTime::parse("2024-03-15T12:34:56Z")
assert_true((try? dt.format_with("{ZZ}")) is Err(_))
assert_true((try? dt.format_with("a{YYYY")) is Err(_))
assert_true((try? dt.format_with("a{")) is Err(_))
assert_true((try? dt.format_with("{YYY}")) is Err(_))
assert_true((try? dt.format_with("{}")) is Err(_))
assert_true((try? dt.format_with("{nnnnnnnn}")) is Err(_))
assert_true((try? dt.format_with("{nnnnnnnnnn}")) is Err(_))
assert_true((try? dt.format_with("{Mm}")) is Err(_))
assert_true((try? dt.format_with("}")) is Err(_))
assert_true((try? dt.format_with("{YYYY}}")) is Err(_))
}

///|
test "DateTime::format_with escapes braces" {
let dt = @tempo.DateTime::parse("2024-03-15T12:34:56Z")
assert_eq(dt.format_with("{{x}}"), "{x}")
assert_eq(dt.format_with("{{{YYYY}}}"), "{2024}")
assert_eq(dt.format_with("}}"), "}")
}

///|
test "Time::format no sub-second" {
let t = @tempo.Time::new(0, 0, 0, 0)
Expand Down