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
Implementing correct offsets for to_local #52
Changes from 1 commit
1860f1c
8744e19
b522aef
4a2c36e
df8073d
6359bb8
89293a5
f1b11fb
b81183a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ class TimeOrDateTime | |
# Constructs a new TimeOrDateTime. timeOrDateTime can be a Time, DateTime | ||
# or Integer. If using a Time or DateTime, any time zone information | ||
# is ignored. | ||
def initialize(timeOrDateTime) | ||
def initialize(timeOrDateTime, ignore_offset = true) | ||
@time = nil | ||
@datetime = nil | ||
@timestamp = nil | ||
|
@@ -23,11 +23,11 @@ def initialize(timeOrDateTime) | |
nsec = @time.nsec | ||
usec = nsec % 1000 == 0 ? nsec / 1000 : Rational(nsec, 1000) | ||
|
||
@time = Time.utc(@time.year, @time.mon, @time.mday, @time.hour, @time.min, @time.sec, usec) unless @time.utc? | ||
@time = Time.utc(@time.year, @time.mon, @time.mday, @time.hour, @time.min, @time.sec, usec) unless @time.utc? || !ignore_offset | ||
@orig = @time | ||
elsif timeOrDateTime.is_a?(DateTime) | ||
@datetime = timeOrDateTime | ||
@datetime = @datetime.new_offset(0) unless @datetime.offset == 0 | ||
@datetime = @datetime.new_offset(0) unless @datetime.offset == 0 || !ignore_offset | ||
@orig = @datetime | ||
else | ||
@timestamp = timeOrDateTime.to_i | ||
|
@@ -255,6 +255,29 @@ def -(seconds) | |
self + (-seconds) | ||
end | ||
|
||
# Sets offset from UTC for the TimeOrDateTime. Returns a new TimeOrDateTime, | ||
# preserving original class. Does NOT changes values of hour-min-second, | ||
# so, (18:00 UTC).offset(3600) will became (18:00 +01:00). | ||
# Considers original value's UTC offset wisely. | ||
def offset(seconds) | ||
return self if seconds == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the |
||
|
||
if @orig.is_a?(DateTime) | ||
off = OffsetRationals.rational_for_offset(seconds) | ||
TimeOrDateTime.new(@orig.new_offset(off), false) | ||
elsif @orig.is_a?(Time) | ||
offset_str = '%+03i:%02i' % [seconds / 3600, seconds / 60 % 60] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, you are right. Didn't find this on time, will change. |
||
|
||
time = @time + seconds | ||
nsec_part = Rational(time.nsec, 1_000_000_000) | ||
time = Time.new(time.year, time.mon, time.mday, time.hour, time.min, time.sec + nsec_part, offset_str) | ||
TimeOrDateTime.new(time, false) | ||
else | ||
# Integer: fallback to "just shift timestamp" | ||
TimeOrDateTime.new(@orig + seconds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This behaves slightly differently to the |
||
end | ||
end | ||
|
||
# Returns true if todt represents the same time and was originally | ||
# constructed with the same type (DateTime, Time or timestamp) as this | ||
# TimeOrDateTime. | ||
|
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 seems to produce a different result depending on whether
DateTime
orTime
is used to initialize theTimeOrDateTime
: