@@ -464,6 +464,7 @@ defmodule Calendar do
464
464
p | "AM" or "PM" (noon is "PM", midnight as "AM") | AM, PM
465
465
P | "am" or "pm" (noon is "pm", midnight as "am") | am, pm
466
466
q | Quarter | 1, 2, 3, 4
467
+ s | Number of seconds since the Epoch, 1970-01-01 00:00:00+0000 (UTC) | 1565888877
467
468
S | Second | 00, 59, 60
468
469
u | Day of the week | 1 (Monday), 7 (Sunday)
469
470
x | Preferred date (without time) representation | 2018-10-17
@@ -804,6 +805,34 @@ defmodule Calendar do
804
805
parse ( rest , datetime , format_options , [ result | acc ] )
805
806
end
806
807
808
+ # Epoch time for DateTime with time zones
809
+ defp format_modifiers (
810
+ "s" <> rest ,
811
+ _width ,
812
+ _pad ,
813
+ datetime = % { utc_offset: _utc_offset , std_offset: _std_offset } ,
814
+ format_options ,
815
+ acc
816
+ ) do
817
+ result =
818
+ datetime
819
+ |> DateTime . shift_zone! ( "Etc/UTC" )
820
+ |> NaiveDateTime . diff ( ~N[ 1970-01-01 00:00:00] )
821
+ |> Integer . to_string ( )
822
+
823
+ parse ( rest , datetime , format_options , [ result | acc ] )
824
+ end
825
+
826
+ # Epoch time
827
+ defp format_modifiers ( "s" <> rest , _width , _pad , datetime , format_options , acc ) do
828
+ result =
829
+ datetime
830
+ |> NaiveDateTime . diff ( ~N[ 1970-01-01 00:00:00] )
831
+ |> Integer . to_string ( )
832
+
833
+ parse ( rest , datetime , format_options , [ result | acc ] )
834
+ end
835
+
807
836
# +hhmm/-hhmm time zone offset from UTC (empty string if naive)
808
837
defp format_modifiers (
809
838
"z" <> rest ,
0 commit comments