Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Last active September 8, 2017 21:06
Show Gist options
  • Save wojtekmach/b66d1bd1eadf17cc57f7ae64f0f6d4b1 to your computer and use it in GitHub Desktop.
Save wojtekmach/b66d1bd1eadf17cc57f7ae64f0f6d4b1 to your computer and use it in GitHub Desktop.
import_if_available Ext.DateTime
# 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