Finally fixed the timezone
3 min read
In a recent change I tried to handle the time zone like this:
def time_from_string(string)
date_matches = string.match(/(\d{4})-(\d{2})-(\d{2})-(\d{4})/)
Time.zone = 'Bucharest'
time_zone = Time.zone.formatted_offset
# A little hack to account for daylight savings of when the post was created
time_zone = '+03:00' if Time.strptime("#{date_matches}", '%Y-%m-%d-%H%M').dst?
time = Date._strptime("#{date_matches[4]} #{time_zone}", '%H%M %:z')
DateTime.new(date_matches[1].to_i, date_matches[2].to_i, date_matches[3].to_i, time[:hour], time[:min], 0, time[:zone]).in_time_zone
endBut that means it will create a time object based on the current time zone, which is not correct; the post was created in a different time zone. So the comparison with the current date will always be off. So I added the sinatra-activerecord gem and came up with this solution:
def time_from_string(string)
date_matches = string.match(/(\d{4})-(\d{2})-(\d{2})-(\d{4})/)
# I always want to link the post creation date to my local time, no matter if I wrote the post on a trip.
time_zone = 'EET'
# It returns true if the date was during dst interval (which differs from year to year)
time_zone = 'EEST' if Time.strptime("#{date_matches}", '%Y-%m-%d-%H%M').dst?
time = Date._strptime("#{date_matches[4]} #{time_zone}", '%H%M %Z')
DateTime.new(date_matches[1].to_i, date_matches[2].to_i, date_matches[3].to_i, time[:hour], time[:min], 0, time[:zone]).to_time
endUpdate, May 18, 2015: Apparently I had to set the timezone for heroku, as well - heroku config:add TZ=Europe/Bucharest. This finally did the trick.