54

Here's one way, but can you think of a more idiomatic way?

>> Time.use_zone('Sydney'){ Time.zone.parse('2011-04-12 2pm') }
=> Tue, 12 Apr 2011 14:00:00 EST +10:00
kkurian
  • 3,844
  • 3
  • 30
  • 49
Gabe Hollombe
  • 7,847
  • 4
  • 39
  • 44
  • in your original question, you did not mention that you did not want now (even though the example you show is using a specific time), thus you should not have downvoted the answers..IMHO! – Zabba May 18 '11 at 00:27
  • @Zabba The original question asked how to "create a specific time" which, coupled with my example, seemed clear enough to me that I wasn't interested in Time.now style responses. I edited the title of this question for extra clarity because it obviously wasn't clear enough. Still, the purpose of this question on Stack is to find good answers to my specific question. Down-voting is my way to show that I don't think the posted answer(s) are relevant. That's all. – Gabe Hollombe May 18 '11 at 01:13

5 Answers5

48

I think you're looking for

Time.find_zone('Alaska').local(2011,1,1)
=> Sat, 01 Jan 2011 00:00:00 AKST -09:00

Time.find_zone('Amsterdam').local(2011,1,1)
=> Sat, 01 Jan 2011 00:00:00 CET +01:00 

Time.find_zone('Sydney').local(2011,1,1)
=> Sat, 01 Jan 2011 00:00:00 EST +11:00  

Time.find_zone('Wellington').local(2011,1,1)
=> Sat, 01 Jan 2011 00:00:00 NZDT +13:00

This also works with parse

Time.find_zone('Sydney').parse('2011-04-12 2pm')
=> Tue, 12 Apr 2011 14:00:00 EST +10:00 
toxaq
  • 6,745
  • 3
  • 46
  • 56
  • 1
    `Time.find_zone('Alaska').local(2011,1,1)` worked the best IMO. Also looks nice and acts declarative. – gregb Nov 12 '14 at 00:38
29

For parsing a date within a specific time zone, you can use ActiveSupport::TimeZone

> ActiveSupport::TimeZone["Sydney"].parse("2011-04-12 2pm")
=> Tue, 12 Apr 2011 14:00:00 EST 10:00

TimeZone API documentation is here: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-c-5B-5D

Jonathan Soifer
  • 2,715
  • 6
  • 27
  • 50
Nick Partridge
  • 1,414
  • 10
  • 11
24

This is what I use:

Time.zone.local(2011, 4, 12, 14, 0)
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
James Healy
  • 14,557
  • 4
  • 33
  • 43
  • although, that's not for a specific zone. It'd be nice if you could do Time.zone("Sydney").local(2011,4,12,14,0) – James Healy May 18 '11 at 04:25
  • I like this solution, because you can use this within the `Time.user_zone`. This way we can do multiple operation within the block along with creating the new time in that specific time zone. – Backnol Aug 09 '16 at 16:33
6

As said above, to create a time in a specific timezone (e.g., 4/10/2014 1:30pm New York):

@event.start_time = Time.find_zone('Eastern Time (US & Canada)').local(2014,4,10,13,30)
=> Thu, 10 Apr 2014 13:30:00 EDT -04:00 

@event.start_time.utc
=> 2014-04-10 17:30:00 UTC

When it is saved to your db, it will be converted to UTC (in Postgres at least if using a timestamp type in your migration), and on future access it will be displayed relative to the application timezone set in config/application.rb

To properly display the local time, we also store the timezone name (e.g., 'Eastern Time (US & Canada)' ) in our database. So, when we want to print the time in our views, we do...

@event.start_time.in_time_zone(@event.timezone)
 => Thu, 10 Apr 2014 13:30:00 EDT -04:00 

To get the abbreviated timezone (e.g., EST)

@event.start_time.in_time_zone(@event.timezone).zone
=> "EDT" 
David D.
  • 143
  • 2
  • 4
-2

How about using the *in_time_zone* helper..

Time.now.in_time_zone('Sydney')
dexter
  • 13,365
  • 5
  • 39
  • 56
  • 1
    If you used this as `Time.parse('2011-04-12 2pm').in_time_zone('Sydney')` it would actually parse it in the local time zone, and then add the offset, making it "Wed, 13 Apr 2011 07:00:00 EST +10:00". – Dylan Markow May 17 '11 at 05:03
  • 1
    Down-voting because I don't want now, I want a specific time, per my example. – Gabe Hollombe May 17 '11 at 23:22