Now that we have added a timezone database to Elixir, there is more work being done with DateTime structs and the lack of a convenient way of creating them, compared to the a NaiveDateTime, is becoming apparent:
DateTime.from_naive(~N[2010-10-10 10:10:10], "Etc/UTC")
Therefore I would like to propose to add the ~U sigil, that returns a DateTime in UTC:
~U[2010-10-10 10:10:10Z]
The offset should always be Z or 0000. Thoughts?
/cc @wojtekmach @lau
This will make my (questionable) https://hex.pm/packages/sigil_z package obsolete, definitely 馃憤
Somehow ~Z feels more memorable given the Zulu timezone strategy. So interesting that its also what @wojtekmach has in his package. Overall a very welcome addition
Z also sounds good to me, given that the Z or 0000 are required.
I also prefer ~Z.
However, I would not require Z or 0000, just the same expression as the naive date time ~N: ~Z[2010-10-10 10:10:10] 馃檪
Alright. And then we raise if any offset is given? It is worth noting that ~N ignores the offset and I don't think that would be appropriate for ~Z.
(...)Z is unambiguous but (...)+00:00 isn't quite equivalent so agreed, I would raise on it too (or any other offset). I'm curious about @lau's feedback.
I think it's a good idea.
I also think including Z at the end of the string like initially proposed by @josevalim is convenient, helpful and good design. If the sigil uses ~Z instead of ~U it would look like this: ~Z[2010-10-10 10:10:10Z]. It might seem redundant to have Z twice at first glance, but it's not. The first one has to be there because that's just how sigils work in Elixir. The second one because that's the standard (ISO and various RFCs) way to display a datetime in UTC.
If people copy paste the part inside the bracket, the data about it being in UTC will be preserved. Some of these sigils will also end up being written to logs file that can be parsed by software. In a log file this is more valuable if the string conforms to standards for indicating the offset. When simply reading a sigil as a programmer it will have the Z in the right place and make reading the datetimes consistent.
Following that, when Elixir is parsing and validating sigils I also think that a zero offset should be required the same way it is required in DateTime.from_iso8601/1 (Z or 00:00 etc.). For consistency, complying with standards, and reinforcing that way of indicating that it is UTC.
Something that might be obvious, but just to be clear: to me it makes sense to only display datetimes with this sigil if they are in the "Etc/UTC" timezone - not just any datetime with a total UTC offset of 0.
Thanks @lau. If Z or 00:00 is required at the end, then I would prefer the sigil to be ~U, otherwise people will ask why Z is required twice. Given @lau's arguments, I think ~U+Z is a better choice.
Thoughts @kipcole9, @fertapric and @wojtekmach?
Something that might be obvious, but just to be clear: to me it makes sense to only display datetimes with this sigil if they are in the
"Etc/UTC"timezone - not just any datetime with a total UTC offset of 0.
I had not thought about the displaying part but yes, we should do that too, as you describe.
Sounds good to me.
I think
~U+Z is a better choice.
Agreed.
My main concern is that the ~U+Z syntax is closer to DateTime.from_iso8601/2 than DateTime.from_naive(..., "Etc/UTC") and users might expect it to work with UTC offsets: ~U[2015-01-23T23:50:07.123+02:30].
@fertapric we can raise a very good error message in those cases. And given that U stands to UTC, it should hopefully be understandable why we only accept so. :)
Close in favor of #8824.