[Proposal] Non-naive DateTime sigil

287 vistas
Ir al primer mensaje no leído

Gregory McIntyre

no leída,
26 jun 2017, 10:05:49 p.m.26/6/17
para elixir-lang-core
Love Elixir! So good!

Recently we found ourselves writing a lot of DateTime literals to feed into our unit tests like this:

DateTime.from_naive!(~N[2016-05-05 01:01:42], "Etc/UTC")

Is there a simpler way to specify human readable timestamp literals in code? Could there be? I'm not sure what the best sigil would be, but something like this?

~Z[2016-05-05T01:01:42Z]

(That is ISO 8601 format.)

Love and peace and many thanks,
Greg

Wojtek Mach

no leída,
27 jun 2017, 10:29:09 a.m.27/6/17
para elixir-lang-core
I think that'd be a nice addition. Even if it does not eventually land in Elixir, anyone can define the `~Z` (or whatever) sigil just for tests as shown at the end of the post.

I usually deal with just `NaiveDateTime`s but on the rare occasion that I used `DateTime` not having the sigil wasn't as bad as the incredibly verbose inspect output of DateTime's (the whole struct). Thus, if there was an unambiguous text representation (that contains timezone) which could be used in both the sigil and inspect, I think that would be a great addition to the language. While inspect could handle any struct (assuming it was correctly constructed, leaving this to the developer), sigil probably would only work for Etc/UTC, until Elixir ships with tzdata.

defmodule FooCase do
 
use ExUnit.CaseTemplate

 
using do
    quote
do
     
def sigil_Z(binary, _opts) do
       
{:ok, datetime, 0} = DateTime.from_iso8601(binary)
        datetime
     
end
   
end
 
end
end


defmodule
FooTest do
 
use FooCase


  test
"the truth" do
   
assert ~Z[2015-01-23T23:50:07Z] == DateTime.from_naive!(~N[2015-01-23T23:50:07], "Etc/UTC")
 
end
end

Michał Muskała

no leída,
27 jun 2017, 10:40:12 a.m.27/6/17
para elixir-lang-core
I'm not 100% convinced about the sigil since it would only work with UTC.
What I would love to see, however, is an Inspect implementation for DateTime. The structs are extremely hard to read and see what's happening. Even having a simple inspect implementation should make it much easier.

Michał.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/d59e56aa-dde9-426b-9303-a62f2576e396%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wojtek Mach

no leída,
27 jun 2017, 10:49:41 a.m.27/6/17
para elixir-lang-core
There's DateTime.to_string/1 which seems perfect to use, I'm looking into it:

iex(1)> DateTime.utc_now
#DateTime<2017-06-27 14:46:54.331343Z>

iex
(3)> %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET", hour: 23, minute: 0, second: 7, microsecond: {0, 0}, utc_offset: 3600, std_offset: 0, time_zone: "Europe/Warsaw"}
#DateTime<2000-02-29 23:00:07+01:00 CET Europe/Warsaw>

Wojtek Mach

no leída,
8 sept 2017, 5:10:05 p.m.8/9/17
para elixir-lang-core
For anyone interested, I've implemented the ~Z sigil to experiment with in a personal project: https://gist.github.com/wojtekmach/b66d1bd1eadf17cc57f7ae64f0f6d4b1
This allows me to do:

$ iex -S mix
iex
(1)> DateTime.utc_now
~Z[2017-09-08 21:04:17.404239]
iex
(2)> Calendar.DateTime.shift_zone!(~Z[2017-09-08 21:04:17.404239], "America/New_York")
#DateTime<2017-09-08 17:04:17.404239-04:00 EDT America/New_York>


The Code.compiler_options.ignore_module_conflict is obviously a hack, so looking forward to hearing if it's a big no-no :)
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos