if you ever want the undeniable compulsion to break your computer in half start a project with TIMEZONES

SERIOUSLY FUCKING HATE TIMEZONES.

Localize a UTC TIME

DateTime.now.in_time_zone('Eastern Time (US & Canada)')

What I’m trying to do “should not” be that difficult. Just trying to convert a good ole local time into UTC.

On 11/19/2016 I found this stackoverflow… http://stackoverflow.com/questions/2695837/rails-convert-utc-datetime-to-another-time-zone … fucking miracle.

One of the problems is Ruby has an implementation of Timezones and Rails has a “just different enough to be annoying as fuck” implementation of Timezones.

Ruby’s Timezone Implementation: TZInfo Rail’s Timezone Implementation: ActiveSupport::TimeZone

One thing you might wonder when it comes to timezones is what timezones are available? Of course we ain’t some kinda dumbass, I know there’s the Central Timezone, Mountain Timezone and even an Eastern Timezone (but no one uses the eastern one). But we know there’s one timezone that TRUMPS EM ALL, THE MOTHA FUCKN NERD HOLY GRAIL: UTC. This isn’t really getting my point across. THE THING THAT REALLY PISSED ME OFF TODAY WAS TRYING TO LIST ALL THE AVAILABLE TIMEZONES FROM TZInfo. NO ONE HAS POSTED THIS ONE LINE OF CODE ON THE INTERNET UNTIL TODAY. FUCK FUCK FUCK FUCK FUCK FUCK FUCK ALL YA’LL. AND IT’S NOT LIKE I ACTUALLY FOUND THE COMMAND SOMEWHERE ON THE INTENRET, I simply got lucky and fucking guessed and Lord have MERCY IT FUCKING WORKED. Here is the command…

TZInfo::Timezone.all

Now, be aware, that lists all the RUBY recognized timezones. They may not work with the Rails Timezones.

ActiveSupport::TimeZone.all

Let’s move our discussion to TZInfo for the moment. This is an amazing stackoverflow answer. http://stackoverflow.com/questions/9962038/how-do-i-calculate-the-offset-in-hours-of-a-given-timezone-from-utc-in-ruby

> tz = TZInfo::Timezone.get('America/New_York')
> tz.current_period.utc_total_offset / 60 / 60
=> -5

Gets you the timezone offset from UTC. Then you can use that offset to convert local times to UTC.

DateTimeObject + 4.hours

Get the current hour of UTC

Time.current.utc.hour

Get the current day of UTC

Time.current.utc.day

Get the current month of UTC

Time.current.utc.month

Other Important shit

"30/Nov/2009 16:29:30 +0100".to_datetime
DateTime.now.new_offset(0).cweek
DateTime.now.new_offset(0).beginning_of_week
DateTime.now.new_offset(0).end_of_week

Post Content