Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Time.utc #5321

Merged
merged 3 commits into from Nov 28, 2017
Merged

Add Time.utc #5321

merged 3 commits into from Nov 28, 2017

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Nov 23, 2017

It is fairly common to create a Time instance in UTC. Until now, to create such an instance, you'd have to use Time.new with kind: Time::Kind::Utc.

This PR adds a shortcut for this specific task, named Time.utc which accepts the same arguments as Time#now with date and time fields or with seconds and nanoseconds and sets the kind to Utc. It fit's nicely with the already existing Time.utc_now.

This makes it less verbose to use UTC times for developers and it will be easier to replace the current Time#kind with a proper time zone implementation (see #2490).

As a minor edit I also replaced direct comparisons of Time#kind with Time::Kind::Utc with Time.utc? which serves the same purpose.

@@ -19,7 +19,10 @@ require "crystal/system/time"
# Time.new(2016, 2, 15) # => 2016-02-15 00:00:00
#
# # Specifying a time
# Time.new(2016, 2, 15, 10, 20, 30) # => 2016-02-15 10:20:30 UTC
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc line was actually wrong, Time.new(2016, 2, 15, 10, 20, 30) currently returns a time of kind Unspecified, which does not have a UTC timezone.

@straight-shoota straight-shoota force-pushed the jm-time-utc branch 2 times, most recently from 3a9cb40 to 1de9188 Compare November 23, 2017 14:03
Creating a UTC time instance is fairly commmon and should
not bother with internals instead have a simple API for this.
This fits nicely with already existing `Time.utc_now`.
HTTP.parse_time("Sun, 06 Nov 1994 08:49:37 GMT").should eq(time)
end

it "parses RFC 1123 without day name" do
time = Time.new(1994, 11, 6, 8, 49, 37)
time = Time.utc(1994, 11, 6, 8, 49, 37)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be UTC (following line check it's a GMT time)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is completely valid.

  • HTTP dates represented in the RFC 1123 format are usually marked as GMT but are really UTC times, because the standard doesn't recognize UTC. This is also how the Crystal implementation works.
  • This method is used to create a time instance refering to a specific point in time. For the equality check, the individual time zones do not really matter, because the values are compared based on UTC anyway. It has just to be assured that both, the RFC 1123 string representation and the local comparison date refere to the same point in time. And that, they do.
  • Apart from that, there is not really a difference between dates in GMT and UTC zones at all in Crystal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, forgot GMT was a timezone.

@luislavena
Copy link
Contributor

Thank you @straight-shoota for your contribution!

I like the Time.utc constructor, definitely will help saving some typing 😃

Changes LGTM.

@RX14
Copy link
Contributor

RX14 commented Nov 24, 2017

I'm not sure about it, is using kind: Time::Kind::UTC really that difficult?

@luislavena
Copy link
Contributor

I'm not sure about it, is using kind: Time::Kind::UTC really that difficult?

It depends, if you're writing a lot of time-related calculations and generations, typing that every single time can result tedious. Use your clipboard buffer to hold that single snippet or adding that as snippet of your editor is also tedious.

There are several examples were those shortcuts were added to the codebase that reduced typing and core decided that was a win for the developers without bending the Crystal core rule of no aliases.

But again, it definitely depends on usage. 🤷‍♂️

@Sija
Copy link
Contributor

Sija commented Nov 24, 2017

@RX14 Same question can be applied to many other instances where shorthand notation is just preferred because of brevity/speed/semantic/etc...

@straight-shoota
Copy link
Member Author

@RX14 kind: Time::Kind::UTC is really tedious and it is about to change when we support real time zone. In fact, I'm currently working on a proposal for this.
It is very common to need a reference to an exact instance of time. Just look at the specs in this PR
For this, it is best to use UTC because it is unambiguous. People should use that and it should be easy.

@luislavena
Copy link
Contributor

Hello @mverzilli and @bcardiff, can you guys weight in and comment/approve this change?

Thank you.
❤️ ❤️ ❤️

@mverzilli mverzilli added this to the Next milestone Nov 28, 2017
@mverzilli mverzilli merged commit 1978c8b into crystal-lang:master Nov 28, 2017
@mverzilli
Copy link

Cool stuff, thanks @straight-shoota!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants