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#to_local_in #6572
Add Time#to_local_in #6572
Conversation
src/time.cr
Outdated
# ``` | ||
# time = Time.now(Time::Location.new(-3600)) | ||
# time2 = Time.from_wall_clock(time, Time::Location.new(14400)) | ||
# time2.to_s("%Y-%m-%d %H:%M:%S") == time1.to_s("%Y-%m-%d %H:%M:%S") # => true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: time1
This looks a bit redundant: Can we somehow provide an instance method instead? |
It would of course also work as an instance method. This way it is not likely to be confused with I'm definitely open to change it to an instance method. Suggestions are welcome! |
I'm not sure it reads well (grammatically), so if we can't come up with an instance name the class method is fine. |
I'm fine with the class method but could also live with something like |
src/time.cr
Outdated
# ``` | ||
# time = Time.now | ||
# time2 = Time.new(time.year, time.month, time.day, time.hour, time.minute, time.second, nanosecond: time.nanosecond, location: location) | ||
# ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be helpful to talk about how this differs from Time#in
. Right now it is difficult to tell which one to use and how they are different
src/time.cr
Outdated
# Create a new `Time` instance that preserves the same wall clock reading than | ||
# *time* but in a different *location*. | ||
# Create a new `Time` instance that preserves the same local date-time | ||
# representation (wall clock) as *time* but observed in a different *location*. | ||
# | ||
# ``` | ||
# time = Time.now(Time::Location.new(-3600)) | ||
# time2 = Time.from_wall_clock(time, Time::Location.new(14400)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can use two real timezones like New York and Tokyo and then call the vars new_york_time
, tokyo_wall_clock_time
or so? (Admittedly not great names yet). time
, time2
gets confusing fast here :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to not be a class method.
If |
@bcardiff |
274f577
to
e0380d6
Compare
If there was a local date time representation (floating time), this could be expressed for example like in the Java 8 Date Time API as |
e0380d6
to
ab88d5d
Compare
Rebased and renamed |
672c8c0
to
2d25868
Compare
src/time.cr
Outdated
# ``` | ||
# tokyo = Time.now(Time::Location.load("Asia/Tokyo")) | ||
# new_york = Time.new(tokyo.year, tokyo.month, tokyo.day, tokyo.hour, tokyo.minute, tokyo.second, nanosecond: tokyo.nanosecond, location: location) | ||
# ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too abstract. The naming doesn't give much hint at what the method means to achieve, and the documentation doesn't really help until the last example.
The following would be more helpful, and enough to understand:
Creates a new
Time
instance with only the location changed. Unlike#in
that will shift the date time so the new Time instance still refers to the same UTC timestamp,#to_local_in
keeps the same date and time but for another location.For example:
halloween = Time.utc(2018, 10, 31, 17, 30, 0) tokyo = halloween.to_local_in(Time::Location.load("Asia/Tokyo")) new_york = halloween.to_local_in(Time::Location.load("America/New_York")) tokyo.to_s # => 2018-10-31 17:30:00 Asia/Tokyo new_york.to_s # => 2018-09-31 17:30:00 America/New_York
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creates a new Time instance with only the location changed.
This reduces the time instance to it's local date-time representation, but in fact, this method also changes the instant on the time-line.
But I'll try to improve the documentation.
src/time.cr
Outdated
# | ||
# The result of this method is equivalent to going through the calendrical | ||
# constructor like this: | ||
# Unlike `#in`, which always references the same instant in time, `#to_local_in` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which always preserves the same instant in time
Let me squash this before merging 👍 |
018adee
to
8767fc8
Compare
Closes #5789