Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6112129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:42:35+00:00 2026-05-23T14:42:35+00:00

I want to be able to parse a Time from a string in Ruby

  • 0

I want to be able to parse a Time from a string in Ruby (1.8.7), where the string does not contain any time zone information. I would like to treat the string as though it were in any of a number of time zones specified in this type of format: ‘America/New_York’.

Time string example:

'2010-02-05 01:00:01'

I have spent quite a while trying to figure this one out.

I did find a similar question, but its answer does not apply in my case: How do I get Ruby to parse time as if it were in a different time zone?

The problem with the above solution is that my time zones cannot all be represented in the 3-letter format supported by Time.parse (http://www.ruby-doc.org/stdlib-1.8.7/libdoc/time/rdoc/classes/Time.html#M004931).

Is there a good way to accomplish what I’m trying to do here?

Edit: Made my answer actually appear as an answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:42:35+00:00Added an answer on May 23, 2026 at 2:42 pm

    Here’s what I came up with using the tzinfo gem as suggested, though it seems rather complicated and unintuitive to me. As an end result I get the time parsed as though it were in the time zone I wanted, though represented by a Time object in UTC. I can also display it in the time zone I want using tzinfo’s strftime:

    jruby-1.6.1 :003 > time = '2010-05-01 01:00:00'
     => "2010-05-01 01:00:00" 
    jruby-1.6.1 :004 > tz = TZInfo::Timezone.get('America/New_York')
     => #<TZInfo::DataTimezone: America/New_York> 
    jruby-1.6.1 :005 > time += ' UTC'
     => "2010-05-01 01:00:00 UTC" 
    jruby-1.6.1 :006 > time = Time.parse(time)
     => Sat May 01 01:00:00 UTC 2010 
    jruby-1.6.1 :007 > time = tz.local_to_utc(time)
     => Sat May 01 05:00:00 UTC 2010 
    jruby-1.6.1 :010 > tz.strftime('%Y-%m-%d %H:%M:%S %Z', time)
     => "2010-05-01 01:00:00 EDT" 
    

    I believe this will suit my needs, but I wonder if I can get the Time to actually be in the timezone above (instead of just UTC).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into
I want to be able to play back video from a specific time using
I want to be able to parse this date-time format 2011-12-02T16:18:12.479-05:00 into an mm/dd/yy
I want to be able to parse the following times using ParseExact() function: 01:02:03
Background I want to be able to parse Javascript source in a Delphi Application.
I am able to parse the HTML but I want to extract the warning
So I want to be able to parse, and evaluate, dice expressions in C#.
I'm trying to write an Android game and I would like to be able
I want to get data from webservice using NSXml Parser. I am able to
I want to be able to retrieve a users calendar and events from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.