Rails parse datetime in model - ruby-on-rails

I'm getting datetime string from the form and the example value is below:
2014-02-21 13:00:00
I want to be able to convert this into datetime before it gets saved in the model.
Is there a way to do this as Rails 4 seems to put wrong date in there automatically when it gets saved

Adding your timezone as default in application.rb like below should do the trick:
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'

Try
Time.zone.parse("2014-02-21 13:00:00")
instead of
Time.parse("2014-02-21 13:00:00")
as the latter will "assume the string is given in the system's time zone"
Source and more about the topic

Try this to_time or to_date_time method.
It convert string into date time format
"2014-02-21 13:00:00".to_time
"2014-02-21 13:00:00".to_datetime

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)

use table's timestamp column(without timezone) as EST

I have a table 'A' in db which have a column 'x' of type 'timestamp without timezone' and in rails project I have set timezone to 'EST'. Everything works fine when I create/update record from my project.
The problem is that I imported data from another DB using PSQL. 'x' column have the same time as in old DB but when I use 'ActiveRecord' query, I get '+05:00' offset. I am confused how to tell 'ActiveRecord' that that time is already in 'EST'?
here is a solution by me.
=> A.first.x.to_s(:db)
# "1900-01-01 21:47:00"
=> A.first.x
#Mon, 01 Jan 1900 16:47:00 EST -05:00
but is there any other better option ?
Adding following to application.rb must work
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'
or Every zone you wish
You can convert your time in 'EST' time zone in your query.
You can find your answer here

Rails 4 - how to set a timezone for strftime?

Every user in our database can select his/her own timezone. The default timezone of our application Eastern Time:
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'
When I display some dates/times in the application, I usually use for that purpose .strftime. How can I give a parameter to strftime to display the date/time properly (in the given timezone for the specific user)?
Or is there a better way than to format every date/time with using strftime?
EDIT:
Every user has a time_zone string column, where is stored the timezone like this: "Eastern Time (US & Canada)".
As the every date/time will be re-calculated for every timezone, how about the performance? Will this not slow down too much rendering views?
Set your time object in_time_zone() before strftime.
Say you're displaying created_at, you'd do:
object.created_at.in_time_zone(current_user.timezone).strftime("...")

How to strptime a time string as a different from local time zone

When I use Time.strptime in my local computer, I got like this:
ENV['LC_CTYPE']
=>"ja_JP.UTF-8"
Time.strptime("10/12/2015-4:05pm", '%m/%d/%Y-%I:%M%p')
=> 2015-10-12 16:05:00 +0900
How can I parse the string as a different time zone like 'Central Time (US & Canada)'?
I tried with Time.zone = 'Central Time (US & Canada)', but the result was same.
I want to use the function in Rails environment so it's ok to use active_support.
%Z is how you represent timezone in date/time format. Try the following:
time_zone = 'Central Time (US & Canada)'
DateTime.strptime "10/12/2015-4:05pm #{time_zone}", '%m/%d/%Y-%I:%M%p %Z'

How to set the timezone to EST throughout the rails app?

I want to use EST throughout the app. I have set the
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'
what I want is, I have to get all the time I have used like "DateTime.now" and it now returns time in my local timezone but I want it to return date time in EST. Also I have date time range picker and I get the start date and end date and parse the string to time using
(params[:start_date]).to_time
which also returns the date time in local zone and instead i want all those to return in EST. How can I do this? Any help is appreciated.
Thanks in advance.
DateTime.now and Time.now doesn't respect Time.zone and will always use the server time. If the machine you're using is set to a different timezone, these 2 will use that timezone.
to_time on the other hand defaults to local timezone as mentioned in apidock (although just tried this out and I get utc by default). To get these times in the timezone set, append in_time_zone
DateTime.now.in_time_zone
Time.now.in_time_zone
string.to_time.in_time_zone
There are some shortcuts for these though. The following code will return the timestamps in the current timezone
Time.zone.now
Time.zone.parse(string)
Normally, in database date-time would be saved in UTC format unless you have selected a specific TimeZone for Active Record and it would be easier if you have to meddle with multiple TimeZones. But, while you access the values you will get the values in you selected TimeZone.
config.time_zone = 'Eastern Time (US & Canada)
Now, suppose you have a datetime value in UTC from table as
datetime1 = "2014-08-25 10:25:57 +0000"
You can convert it to the selected timezone 'Eastern Time (US & Canada)' as below,
Time.zone.parse(datetime1) => Mon, 25 Aug 2014 06:25:57 EDT -04:00
Then, if you need it in a proper format, use method strftime.
Time.zone.parse("2014-08-25 10:25:57 +0000").strftime("%d-%m-%Y %H:%M:%S") => "25-08-2014 06:25:57"
Hoep it helps :)

Resources