Add support for epoch time (%s) to Calendar strftime (#12380)

This commit is contained in:
Scott Bradshaw
2023-02-07 09:58:33 +01:00
committed by GitHub
parent 22b7686fa8
commit b5d883c8bb
2 changed files with 49 additions and 0 deletions
+29
View File
@@ -464,6 +464,7 @@ defmodule Calendar do
p | "AM" or "PM" (noon is "PM", midnight as "AM") | AM, PM
P | "am" or "pm" (noon is "pm", midnight as "am") | am, pm
q | Quarter | 1, 2, 3, 4
s | Number of seconds since the Epoch, 1970-01-01 00:00:00+0000 (UTC) | 1565888877
S | Second | 00, 59, 60
u | Day of the week | 1 (Monday), 7 (Sunday)
x | Preferred date (without time) representation | 2018-10-17
@@ -804,6 +805,34 @@ defmodule Calendar do
parse(rest, datetime, format_options, [result | acc])
end
# Epoch time for DateTime with time zones
defp format_modifiers(
"s" <> rest,
_width,
_pad,
datetime = %{utc_offset: _utc_offset, std_offset: _std_offset},
format_options,
acc
) do
result =
datetime
|> DateTime.shift_zone!("Etc/UTC")
|> NaiveDateTime.diff(~N[1970-01-01 00:00:00])
|> Integer.to_string()
parse(rest, datetime, format_options, [result | acc])
end
# Epoch time
defp format_modifiers("s" <> rest, _width, _pad, datetime, format_options, acc) do
result =
datetime
|> NaiveDateTime.diff(~N[1970-01-01 00:00:00])
|> Integer.to_string()
parse(rest, datetime, format_options, [result | acc])
end
# +hhmm/-hhmm time zone offset from UTC (empty string if naive)
defp format_modifiers(
"z" <> rest,
+20
View File
@@ -336,6 +336,26 @@ defmodule CalendarTest do
assert Calendar.strftime(~N[2019-08-15 17:07:57], "%010A") == "00Thursday"
end
test "formats Epoch time with %s" do
assert Calendar.strftime(~N[2019-08-15 17:07:57], "%s") == "1565888877"
datetime = %DateTime{
year: 2019,
month: 8,
day: 15,
hour: 17,
minute: 7,
second: 57,
microsecond: {0, 0},
time_zone: "Europe/Berlin",
zone_abbr: "CET",
utc_offset: 3600,
std_offset: 0
}
assert Calendar.strftime(datetime, "%s") == "1565885277"
end
test "formats datetime with all options and modifiers" do
assert Calendar.strftime(
~U[2019-08-15 17:07:57.001Z],