Skip to content

Instantly share code, notes, and snippets.

@aledalgrande
Created March 28, 2012 13:28
Show Gist options
  • Select an option

  • Save aledalgrande/2226164 to your computer and use it in GitHub Desktop.

Select an option

Save aledalgrande/2226164 to your computer and use it in GitHub Desktop.
How to convert DateTime to Time reliably (monkey patching)
class Date
def to_gm_time
to_real_time(new_offset, :gm)
end
def to_local_time
to_real_time(new_offset(DateTime.now.offset-offset), :local)
end
def to_real_time(dest=new_offset, method=:utc) # don't use to_time because it could be overridden by ActiveSupport if it's required after this file
usec = (dest.sec_fraction * 10**6).to_i
Time.send(method, dest.year, dest.month, dest.day, dest.hour, dest.min,
dest.sec, usec)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment