Converting a date string into a time zone specific format - ruby-on-rails

Rails 5.2.3
I have a date:
"Apr-03-2013 17:47:00"
I have a date zone:
"America/Los_Angeles"
I am trying to turn it into a string:
"Apr-03-2013 17:47:00 Pacific Daylight Time (GMT-07)"
The best I can come up with is:
time_obj = ActiveSupport::TimeZone["America/Los_Angeles"].parse("2013-04-03 17:47:00")
Time.at(time_obj).strftime("%b-%d-%Y %H:%M:%S %Z")
Which gives me:
"Apr-03-2013 17:47:00 PDT"
Any ideas?

I believe you need custom logic and/or you own database of timezones to get it exactly like that.
Using %Z with strftime is going to give you what ever your OS likes and there are a few disclaimers in the ruby docs.
One idea that you might get some mileage out of: If you are starting with a time zone identifier like "America/Los_Angeles" then you can use ActiveSupport::TimeZone::MAPPING to get a friendlier name, or at least a Rails time zone name.
eg:
ActiveSupport::TimeZone::MAPPING.key("America/Los_Angeles")
=> "Pacific Time (US & Canada)"
But that won't work for every identifier:
ActiveSupport::TimeZone::MAPPING.key("America/Detroit")
=> nil
You can see which ones will map like this:
TZInfo::Country.get('US').zone_identifiers.map {|ident| [ident, ActiveSupport::TimeZone::MAPPING.key(ident)] }
So in that case you need to fall back to the identifier you have, or perhaps this approach might work.
Then you'd need to deal with the daylight savings part, here you can use dst?
ActiveSupport::TimeZone["America/Los_Angeles"].parse("2013-04-03 17:47:00").dst?
Then you'd need to splice all that together! ... and add the offset as well.

Related

Parsing a TimeZone name string that includes GMT offset hours

I did find this question but I am still stumbling around looking for a simple solution to the following:
An API call returns the following format which looks like they are using Time.zone.to_s
irb> ShopifyAPI::Shop.current.timezone
=> "(GMT-08:00) Pacific Time (US & Canada)"
I would like to parse the "(GMT-08:00) Pacific Time (US & Canada)" into a Ruby class and output the TimeZone name "Pacific Time (US & Canada)"
Alternately I could just strip the "(GMT-08:00)" offset and be left with a clean TimeZone name "Pacific Time (US & Canada)" but this seems like a messy string editing solution.
ShopifyAPI::Shop.current returns properties documented here. Yes, timezone is one of them, but it is intended to be a display name, not something you should parse.
Instead, use the iana_timezone property, which will give you the IANA time zone identifier, such as America/Los_Angeles. These are compatible with Rails, as well as Ruby's tzinfo gem, and also are used in many other platforms.
irb> ShopifyAPI::Shop.current.timezone
=> "(GMT-08:00) Pacific Time (US & Canada)"
irb> ShopifyAPI::Shop.current.iana_timezone
=> "America/Los_Angeles"
If you want to get a Rails time zone from there, you can use the MAPPING constant defined in ActiveSupport::TimeZone. Though I'd avoid it if possible, for the reasons mentioned in the timezone tag wiki in the "Rails Time Zone Identifiers" section at the bottom.
If you are confident about the API and string format you are going to receive, you can manipulate string as
string.partition(')').last.strip
# => Pacific Time (US & Canada)

How do I get my own TimeZone in ruby?

looking for a way to get my own time zone that I can pass to config.time_zone in application.rb order to set it in Rails. Background: I'm allowing the user to specify their timezone in a yaml config, but if it's not set, I want to explicitly use the box's timezone. I have to have a value set because I'm using it as reference to convert timezones for display -- each user logging into the Rails app can set their personal timezone and I will do the conversion for them.
However, there seems to be no good API for doing this in Ruby or Rails. Time.now.zone returns the 3-letter code (EDT or CDT, for example), but you can't pass this because it's not specific enough -- the TZInfo class will only accept the "long" descriptions.
Here's what I'm doing now, which seems pretty hacky:
time_zone = CONFIG[:time_zone] # set your local time zone here. use rake time:zones:local to choose a value, or use UTC.
unless time_zone
# try to detect one
if File.exists?('/etc/localtime')
path = File.readlink('/etc/localtime')
items = path.split("zoneinfo/")
if items.length == 2
time_zone = items[1]
end
end
unless time_zone
puts "*** Warning: no time zone is set in config/config.yaml and could not detect system time. Using UTC as the default time; behavior may be unexpected."
time_zone = "UTC"
end
end
config.time_zone = time_zone
Any better ideas guys?
Any reason why this wouldn't work?:
Time.now.zone
Edit:
Before getting the zone, you can do Time.now.gmt_offset to get your GMT offset in seconds. After that, you can do Time.now.zone to get your zone code.
You can try the following:
off_set = Time.now.gmt_offset
p ActiveSupport::TimeZone[off_set].name # "Atlantic Time (Canada)"
or
p ActiveSupport::TimeZone[off_set].tzinfo.name # "America/Halifax"
Since the 3-letter codes are not precise enough, have the user specify the full name from the IANA time zone database.
Then you can use Time.zone = 'America/Halifax' (for example).
Time.zone.name returns back the IANA name, and Time.zone.now returns the current time in the specified timezone.
To find the timezone in this format programically, you may be able to use /etc/timezone if it's available. Other systems have other methods (if there is even one available).
If you want the time zone offset in hours, as some databases do (like Postgres), you can use this method:
2.3.1 :063 > Time.zone.formatted_offset
"-07:00"
or this approach if you need the offset in seconds:
2.3.1 :059 > Time.zone.utc_offset
-25200

Rails: How to parse date-time string into a specific time zone

I'm using Rails 3.2 and ruby 1.9.3 on Debian. I have an app that collects a date, time, and timezone in the form of strings via an HTML form. Something like this:
start_date: "04-15-2010",
start_time: "10:00:00",
timezone: "Central Time (US & Canada)"
What I'd like to do is parse these 3 elements into a single date that is saved into my database as UTC, which in this case would add 7 hours to the start time, once it's in the UTC time zone.
So the stored time would be 17:00 once it's in the DB as UTC instead of the received Central time.
I have tried something like this to parse the date:
ActiveSupport::TimeZone[timezone].at DateTime.strptime("{ 2012-04-09 20:00:00 }", "{ %Y-%m-%d %H:%M:%S }").to_i
However, I'm not able to incorporate the time zone into the resulting time with %Z. It either doesn't parse or the time is interpreted as UTC not Central time. So my question is, how to coerce a date string into a certain time zone without changing the value of the actual date/time stored. I'd like to be able to parse the string into a date/time object that includes the correct time zone with it at that time so that future time zone conversions are accurate. I've looked all over and can't find a way to do this. It's strange, since this seems like something common one does with dates inputted from HTML forms. Thank you for any help.
Try this:
zone = "Central Time (US & Canada)"
ActiveSupport::TimeZone[zone].parse("2013-04-03 17:47:00")
Use String#in_time_zone (Rails 4+)
I personally prefer using String#in_time_zone:
>> '22.09.1986 10:30'.in_time_zone('Central Time (US & Canada)')
# => Mon, 22 Sep 1986 10:30:00 CDT -05:00
This parses the date and time in the String into the time zone provided.
%Z is the correct way to specify a Time zone name. Have you tried the following ?
date_and_time = '%m-%d-%Y %H:%M:%S %Z'
DateTime.strptime("04-15-2010 10:00:00 Central Time (US & Canada)",date_and_time)
This is the method that I came up with. Not the prettiest, but it works. Allows parsing the string using a specified format, and then turning it into the format that I know Time.zone.parse requires.
class ActiveSupport::TimeZone
def strptime(time, format='%m/%d/%Y')
formatted = Time.strptime(time, format).strftime('%Y-%m-%d %T')
parse(formatted)
end
end
Then you can do something like what was mentioned in another question, but with a specified format:
zone = "Central Time (US & Canada)"
ActiveSupport::TimeZone[zone].strptime('2013-04-03', '%Y-%m-%d')
Or if you already have a time zone set:
Time.zone = "Central Time (US & Canada)"
Time.zone.strptime('01/13/2006')
I used a default format of %m/%d/%Y because that's what my user input is most of the time. You can customize this to your needs, or use the default format DateTime uses which is believe is iso8601 (%FT%T%z)
I've finally found the dirty, yet definitive way to do this.
First, parse the string using plain Ruby Time.strptime like this:
time = Time.strptime('12 : 00 : PM', '%I : %M : %p')
This way you get the parsed Time, but not yet in correct timezone. To fix that, let's convert the time to string form and parse it with the standard ActiveSupport::TimeZone#parse
Time.zone.parse(time.to_s)
The result is the ActiveSupport::TimeWithZone with our time parsed into the correct timezone.
The reason why we have to do it this way is that neither ActiveSupport::TimeZone nor ActiveSupport::TimeWithZone support the strptime method. So we have to parse the Time with core Ruby strptime that does not have timezone information, convert it to format acceptable in ActiveSupport objects and then parse it yet again.
To have DateTime take the date string and attach a timezone other than UTC without changing the values of the date string , use this, its easy , doesnt break on leap day :)
xx = DateTime.strptime("9/1/15 #{object.time_zone}", "%m/%d/%Y %Z")
Convert specific date format in UTC.
ActiveSupport::TimeZone['UTC'].parse(Time.strptime('01/24/2019T16:10:16', "%m/%d/%YT%H:%M:%S").asctime)

How to parse date such that it is in EST

I am using Rails 3.1. Here is my configuration
config.time_zone = "Eastern Time (US & Canada)"
Now user enters Nov 30, 2011 at 7:00 PM. How do I parse this text so that after parsing I get the result in EST?
You can do so by calling time = ActiveSupport::TimeZone.new('EST').parse('Nov 30, 2011 at 7:00 PM'). Also you can parse the time string as being in user-specific or default timezone by calling Time.zone.parse. You can convert the result time into any timezone thanm i.e. Time.zone.now.in_time_zone('Asia/Yekaterinburg')
Also there is no need in do any manual conversions of timezones before storing the time to database as Rails does is automatically.
You should store all values in UTC and render them in user's local time. "Eastern Time" is a loose concept at best and changes on a fairly regular basis as politicians decide to extend or contract Daylight Saving Time.
Generally you can do this with the ActiveSupport::TimeZone methods local_to_utc and utc_to_local conversion methods.

User friendly timezone names from tz database format

I have timezones in the following array format:
'America/New_York' => '(GMT-05:00) Eastern Time (US & Canada)',
'Europe/Lisbon' => '(GMT) Greenwich Mean Time : Lisbon',
etc.
How do I go about displaying a user-friendly summer/daylight savings time dependent timezone identifier to the user?
For example, displaying the time now in New York would append "(EDT)" to the time, which would make sense for local users. I want to avoid having to display ((GMT-05:00) Eastern Time (US & Canada)) or just (GMT-05:00), which isn't strictly accurate all year round.
Ideally then, is there a web service/database that can take a tz string in the format "America/New_York", and a timestamp as paramters and return the abbreviation in the formats here?
strftime's %Z format specifier gives you this abbreviation. You didn't say what programming language you are using, but most programming languages give you access to strftime in one way or another.
Python:
import pytz
from datetime import datetime
here = pytz.timezone('Asia/Tokyo')
print here.localize(datetime.utcnow()).strftime("%Z")
there = pytz.timezone('America/Montreal')
print there.localize(datetime.utcnow()).strftime("%Z")
PHP:
date_default_timezone_set("Asia/Tokyo");
echo strftime("%Z");
date_default_timezone_set("America/Montreal");
echo strftime("%Z");

Resources