Current section
Files
Jump to
Current section
Files
src/glrss_parser/util.gleam
@external(erlang, "glrss_parser_ffi", "current_year")
@external(javascript, "../glrss_parser_ffi.mjs", "currentYear")
fn current_year() -> Int
@internal
pub fn year_2digit_to_full(year_2digit) {
//> 2024
let current_year = current_year()
//> 2000
let current_century_year = current_year / 100 * 100
// + 1 for dates slightly in the future.
//> 25
let current_2digit = current_year % 100 + 1
case year_2digit > current_2digit {
True ->
// In the 1900s
year_2digit + current_century_year - 100
// 80 -> 1980
False ->
// In the 2000s
year_2digit + current_century_year
// 24 -> 2024
}
}
@internal
pub fn try_unwrap(
result: Result(a, e),
unwrap: b,
apply fun: fn(a) -> Result(b, e),
) -> b {
case result {
Ok(a) ->
case fun(a) {
Ok(b) -> b
Error(_) -> unwrap
}
Error(_) -> unwrap
}
}