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

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

Comments

Projects
None yet
2 participants
@augnustin

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

This comment has been minimized.

Show comment
Hide comment
@philr

philr Jan 3, 2017

Member

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
Member

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