@@ -1067,19 +1067,21 @@ defmodule DateTime do
1067
1067
iex> 1464096368 |> DateTime.from_unix!() |> DateTime.to_unix()
1068
1068
1464096368
1069
1069
1070
- iex> %DateTime{calendar: Calendar.ISO, day: 20, hour: 18, microsecond: {273806, 6}, minute: 58 ,
1071
- ...> month: 11, second: 19, time_zone: "America/Montevideo",
1072
- ...> utc_offset: -10800, std_offset: 3600, year: 2014, zone_abbr: "UYST"}
1073
- ...> |> DateTime.to_unix()
1070
+ iex> dt = %DateTime{calendar: Calendar.ISO, day: 20, hour: 18, microsecond: {273806, 6},
1071
+ ...> minute: 58, month: 11, second: 19, time_zone: "America/Montevideo",
1072
+ ...> utc_offset: -10800, std_offset: 3600, year: 2014, zone_abbr: "UYST"}
1073
+ iex> DateTime.to_unix(dt )
1074
1074
1416517099
1075
+
1075
1076
"""
1076
1077
@ spec to_unix ( DateTime . t , System . time_unit ) :: non_neg_integer
1077
1078
def to_unix ( % DateTime { std_offset: std_offset , utc_offset: utc_offset ,
1078
1079
hour: hour , minute: minute , second: second , microsecond: { microsecond , _ } ,
1079
1080
year: year , month: month , day: day } , unit \\ :seconds ) when year >= 1970 do
1080
- seconds = :calendar . datetime_to_gregorian_seconds ( { { year , month , day } , { hour , minute , second } } )
1081
- |> Kernel . - ( utc_offset )
1082
- |> Kernel . - ( std_offset )
1081
+ seconds =
1082
+ :calendar . datetime_to_gregorian_seconds ( { { year , month , day } , { hour , minute , second } } )
1083
+ |> Kernel . - ( utc_offset )
1084
+ |> Kernel . - ( std_offset )
1083
1085
System . convert_time_unit ( ( seconds - @ unix_epoch ) * 1_000_000 + microsecond , :microseconds , unit )
1084
1086
end
1085
1087
0 commit comments