Created
March 28, 2012 13:28
-
-
Save aledalgrande/2226164 to your computer and use it in GitHub Desktop.
How to convert DateTime to Time reliably (monkey patching)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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