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

TZInfo::Timezone.get('America/New_York').utc_to_local(nil) = -18000 #39

Closed
augnustin opened this issue Jun 15, 2015 · 1 comment
Closed

TZInfo::Timezone.get('America/New_York').utc_to_local(nil) = -18000 #39

augnustin opened this issue Jun 15, 2015 · 1 comment

Comments

@augnustin
Copy link

@augnustin augnustin commented Jun 15, 2015

I would expect:

TZInfo::Timezone.get('America/New_York').utc_to_local(nil) # to equal nil

Because the time difference of nothing is nothing.

Used in code, it goes like:

TZInfo::Timezone.get('America/New_York').utc_to_local(Object.updated_at) # Object.updated_at can be nil

which consequently requires a if Object.updated_at appended, which is IMHO less elegant.

@philr
Copy link
Member

@philr philr commented Jan 3, 2017

Sorry for the delay in responding to this.

I've decided to make TZInfo raise an exception when passed a nil time (this is now implemented on the master branch). On balance, I think it is better to be explicit about handling nil values.

As for keeping the code elegant, you could add a method to the Time class in your project and then use it along with the safe navigation (&.) operator. For example:

class Time
  def to_new_york
    TZInfo::Timezone.get('America/New_York').utc_to_local(self)
  end
end

object.updated_at&.to_new_york
@philr philr closed this Jan 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.