Is Ruby or Rails the best way to convert Eastern Time to UTC?

I do not mean

myDateTime = DateTime.now myDateTime.new_offset(Rational(0, 24)) 

or

 Time.now.utc 

What I have is a text date indicated in Eastern Time.

I can convert this text date to DateTime. Let me call it eastern_date_time .

Now we have a variable containing DateTime, but nothing knows it east, except for us. Converting it in itself would be very burdensome. If the date is summer time savings (DST) (from March 8 to November 1 of this year), we must add 4 hours to our east_time_time var to get UTC, and if the date is in standard time (ST), we would need to add 5 hours to our east_date_time variable.

How can we indicate that what we have is an Eastern DateTime, and then convert it to UTC ... something that will determine if the date is in DST / ST, and whether it’s correctly applied for 4 or 5 hours?

I want to convert any date that I receive in UTC to be stored in my database.

EDIT:

Using `in_time_zone ', I cannot convert my eastern text time to UTC. How can I achieve this? For instance...

 text_time = "Nov 27, 2015 4:30 PM" #given as Eastern myEasternDateTime = DateTime.parse text_time # => Fri, 27 Nov 2015 16:30:00 +0000 #now we need to specify that this myEasternDateTime is in fact eastern. However, it our default UTC. If we use in_time_zone, it just converts the date at UTC to Eastern myEasternDateTime.in_time_zone('Eastern Time (US & Canada)') # => Fri, 27 Nov 2015 11:30:00 EST -05:00 myEasternDateTime.utc # => Fri, 27 Nov 2015 16:30:00 +0000 

This is not what we want. We must indicate that myEasterDateTime is actually oriental ... so when we do myEasterDateTime.utc at 16:30:00, we get 20:30:00.

How can i do this?

+5
source share
4 answers

I got it this way using time zone suggestions.

 time_text_1 = "Apr 20, 2015 4:30PM" #Scraped as an Eastern Time, with no offset of -5:00 from UTC included time_text_2 = "Nov 20, 2015 4:30PM" #Scraped as an Eastern Time, with no offset of -5:00 from UTC included Time.zone = 'Eastern Time (US & Canada)' my_time_1 = Time.zone.parse time_text_1 # Output: Mon, 20 Apr 2015 16:30:00 EDT -04:00 my_time_2 = Time.zone.parse time_text_2 # Output: Fri, 20 Nov 2015 16:30:00 EST -05:00 my_time_1.utc # Output: 2015-04-20 20:30:00 UTC my_time_2.utc # Output: 2015-11-20 21:30:00 UTC 
+1
source

In the DateTime class was time_in_zone :

 now.time_in_zone('UTC') 

Since then it has been renamed in_time_zone :

 DateTime.now.in_time_zone('US/Pacific') => Wed, 22 Apr 2015 12:36:33 PDT -07:00 
+2
source

Time class objects have a method called dst? which basically tells you whether DST is applicable or not. So you can basically determine if DST / ST is applicable and decide what to add - 4 or 5.

eg. Time.now.dst? If it returns true, add 4, otherwise add 5.

+1
source

In the edited message, your timeline requires an offset from UTC.

EDIT III: Based on the comments (just having a line designed to represent Eastern Time and needing DST accounting, etc.)

 text_time = "Nov 27, 2015 4:30 PM" the_offset = Time.zone_offset('EST') / (60*60) eastern_time = DateTime.parse(text_time).change(offset: the_offset.to_s) # Fri, 27 Nov 2015 16:30:00 -0500 eastern_time.utc # Fri, 27 Nov 2015 21:30:00 +0000 
+1
source

All Articles