@@ -1056,7 +1056,7 @@ defmodule DateTime do
1056
1056
@ doc """
1057
1057
Converts the given DateTime to Unix time.
1058
1058
1059
- The DateTime is expected to be UTC using the ISO calendar
1059
+ The DateTime is expected to be using the ISO calendar
1060
1060
with a year greater than or equal to 1970.
1061
1061
1062
1062
It will return the integer with the given unit,
@@ -1067,12 +1067,19 @@ 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()
1074
+ 1416517099
1070
1075
"""
1071
1076
@ spec to_unix ( DateTime . t , System . time_unit ) :: non_neg_integer
1072
- def to_unix ( % DateTime { std_offset: 0 , utc_offset: 0 , time_zone: "Etc/UTC" ,
1077
+ def to_unix ( % DateTime { std_offset: std_offset , utc_offset: utc_offset ,
1073
1078
hour: hour , minute: minute , second: second , microsecond: { microsecond , _ } ,
1074
1079
year: year , month: month , day: day } , unit \\ :seconds ) when year >= 1970 do
1075
1080
seconds = :calendar . datetime_to_gregorian_seconds ( { { year , month , day } , { hour , minute , second } } )
1081
+ |> Kernel . - ( utc_offset )
1082
+ |> Kernel . - ( std_offset )
1076
1083
System . convert_time_unit ( ( seconds - @ unix_epoch ) * 1_000_000 + microsecond , :microseconds , unit )
1077
1084
end
1078
1085
0 commit comments