@@ -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
@@ -1084,9 +1091,9 @@ defmodule DateTime do
1084
1091
1085
1092
## Examples
1086
1093
1087
- iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CEST ",
1094
+ iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET ",
1088
1095
...> hour: 23, minute: 0, second: 7, microsecond: {0, 1},
1089
- ...> utc_offset: 3600, std_offset: 3600 , time_zone: "Europe/Warsaw"}
1096
+ ...> utc_offset: 3600, std_offset: 0 , time_zone: "Europe/Warsaw"}
1090
1097
iex> DateTime.to_naive(dt)
1091
1098
~N[2000-02-29 23:00:07.0]
1092
1099
@@ -1105,9 +1112,9 @@ defmodule DateTime do
1105
1112
1106
1113
## Examples
1107
1114
1108
- iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CEST ",
1109
- ...> hour: 23, minute: 0, second: 7, microsecond: 0 ,
1110
- ...> utc_offset: 3600, std_offset: 3600 , time_zone: "Europe/Warsaw"}
1115
+ iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET ",
1116
+ ...> hour: 23, minute: 0, second: 7, microsecond: {0, 0} ,
1117
+ ...> utc_offset: 3600, std_offset: 0 , time_zone: "Europe/Warsaw"}
1111
1118
iex> DateTime.to_date(dt)
1112
1119
~D[2000-02-29]
1113
1120
@@ -1124,9 +1131,9 @@ defmodule DateTime do
1124
1131
1125
1132
## Examples
1126
1133
1127
- iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CEST ",
1134
+ iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET ",
1128
1135
...> hour: 23, minute: 0, second: 7, microsecond: {0, 1},
1129
- ...> utc_offset: 3600, std_offset: 3600 , time_zone: "Europe/Warsaw"}
1136
+ ...> utc_offset: 3600, std_offset: 0 , time_zone: "Europe/Warsaw"}
1130
1137
iex> DateTime.to_time(dt)
1131
1138
~T[23:00:07.0]
1132
1139
@@ -1149,23 +1156,23 @@ defmodule DateTime do
1149
1156
1150
1157
### Examples
1151
1158
1152
- iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CEST ",
1153
- ...> hour: 23, minute: 0, second: 7, microsecond: 0 ,
1154
- ...> utc_offset: 3600, std_offset: 3600 , time_zone: "Europe/Warsaw"}
1159
+ iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET ",
1160
+ ...> hour: 23, minute: 0, second: 7, microsecond: {0, 0} ,
1161
+ ...> utc_offset: 3600, std_offset: 0 , time_zone: "Europe/Warsaw"}
1155
1162
iex> DateTime.to_iso8601(dt)
1156
- "2000-02-29T23:00:07+02 :00"
1163
+ "2000-02-29T23:00:07+01 :00"
1157
1164
1158
1165
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "UTC",
1159
- ...> hour: 23, minute: 0, second: 7, microsecond: 0 ,
1166
+ ...> hour: 23, minute: 0, second: 7, microsecond: {0, 0} ,
1160
1167
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
1161
1168
iex> DateTime.to_iso8601(dt)
1162
1169
"2000-02-29T23:00:07Z"
1163
1170
1164
- iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "BRM ",
1165
- ...> hour: 23, minute: 0, second: 7, microsecond: 0 ,
1166
- ...> utc_offset: -12600 , std_offset: 3600 , time_zone: "Brazil /Manaus"}
1171
+ iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT ",
1172
+ ...> hour: 23, minute: 0, second: 7, microsecond: {0, 0} ,
1173
+ ...> utc_offset: -14400 , std_offset: 0 , time_zone: "America /Manaus"}
1167
1174
iex> DateTime.to_iso8601(dt)
1168
- "2000-02-29T23:00:07-02:30 "
1175
+ "2000-02-29T23:00:07-04:00 "
1169
1176
"""
1170
1177
@ spec to_iso8601 ( DateTime . t ) :: String . t
1171
1178
def to_iso8601 ( % DateTime { calendar: Calendar.ISO } = dt ) do
@@ -1177,23 +1184,23 @@ defmodule DateTime do
1177
1184
1178
1185
### Examples
1179
1186
1180
- iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CEST ",
1187
+ iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET ",
1181
1188
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
1182
- ...> utc_offset: 3600, std_offset: 3600 , time_zone: "Europe/Warsaw"}
1189
+ ...> utc_offset: 3600, std_offset: 0 , time_zone: "Europe/Warsaw"}
1183
1190
iex> DateTime.to_string(dt)
1184
- "2000-02-29 23:00:07+02 :00 CEST Europe/Warsaw"
1191
+ "2000-02-29 23:00:07+01 :00 CET Europe/Warsaw"
1185
1192
1186
1193
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "UTC",
1187
1194
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
1188
1195
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
1189
1196
iex> DateTime.to_string(dt)
1190
1197
"2000-02-29 23:00:07Z"
1191
1198
1192
- iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "BRM ",
1199
+ iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT ",
1193
1200
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
1194
- ...> utc_offset: -12600 , std_offset: 3600 , time_zone: "Brazil /Manaus"}
1201
+ ...> utc_offset: -14400 , std_offset: 0 , time_zone: "America /Manaus"}
1195
1202
iex> DateTime.to_string(dt)
1196
- "2000-02-29 23:00:07-02:30 BRM Brazil /Manaus"
1203
+ "2000-02-29 23:00:07-04:00 AMT America /Manaus"
1197
1204
"""
1198
1205
@ spec to_string ( DateTime . t ) :: String . t
1199
1206
def to_string ( % DateTime { calendar: calendar } = dt ) do
0 commit comments