This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import_if_available Ext.DateTime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/ext/datetime.ex | |
defmodule Ext.DateTime do | |
defmacro sigil_Z(string, _) do | |
quote do | |
{:ok, datetime, 0} = DateTime.from_iso8601(unquote(string) <> "Z") | |
datetime | |
end | |
end | |
end | |
old = Code.compiler_options.ignore_module_conflict | |
Code.compiler_options(ignore_module_conflict: true) | |
defimpl Inspect, for: DateTime do | |
def inspect(%{calendar: Calendar.ISO, time_zone: "Etc/UTC"} = datetime, _) do | |
string = DateTime.to_string(datetime) |> String.trim_trailing("Z") | |
"~Z[" <> string <> "]" | |
end | |
def inspect(%{calendar: Calendar.ISO, year: year, month: month, day: day, | |
hour: hour, minute: minute, second: second, microsecond: microsecond, | |
time_zone: time_zone, zone_abbr: zone_abbr, utc_offset: utc_offset, std_offset: std_offset}, _) do | |
"#DateTime<" <> Calendar.ISO.datetime_to_string(year, month, day, hour, minute, second, microsecond, | |
time_zone, zone_abbr, utc_offset, std_offset) <> ">" | |
end | |
def inspect(datetime, opts) do | |
Inspect.Any.inspect(datetime, opts) | |
end | |
end | |
Code.compiler_options(ignore_module_conflict: old) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment