1

I have the following code in Python:

start_time = (datetime.min+ timedelta(hours=9)).strftime('%I:%M %p')

This gives the value '09:00 AM' and to add 30 minutes to the above I can do:

start_time + timedelta(minutes=30)
  1. In Ruby what would be the equivalent of timedelta(hours=9)?

  2. How can I create a start_time similar to the above Python code in Ruby?

Kristján
  • 18,165
  • 5
  • 50
  • 62
jimcgh
  • 5,637
  • 4
  • 27
  • 37
  • Does this help? http://stackoverflow.com/questions/1679266/can-ruby-print-out-time-difference-duration-readily/1679963#1679963 – idjaw Nov 06 '15 at 13:28
  • Welcome to Stack Overflow. You are supposed to write the code and when it doesn't work, ask how to fix the specific problem. As is, you want us to write the code for you, which is not how it works. – the Tin Man Nov 06 '15 at 14:16
  • @The Tin Man: I dont want to sound rude but in this case if i knew how to write the ruby equivalent i wouldnt have asked this question. I knew the logic of what i wanted to accomplish and i shared that, all i wanted to know was the ruby syntax for the same as i was not able to figure it out myself. Anyways thanks for welcoming me to stack overflow ;) – jimcgh Nov 07 '15 at 00:23
  • While it'd seem appropriate to ask "how do I write this code in another language" questions on Stack Overflow, that's not what the site is for. Instead, you have to do the research, and try, and once you do that, if you fail, ask a specific question about your attempt. As is, your question is premature. I'm the one who told you why you're getting close votes, there are other users who silently voted the same way, so others feel the same way. – the Tin Man Nov 07 '15 at 00:26
  • @The Tin Man: Thanks i get your point, but my ideas on how to do the same in ruby were not even close, so didnt want to post them. Also another main reason for not posting was i wanted to know an unbiased solution. Anyways, will post whatever i can from now onwards. Thanks for your feedback – jimcgh Nov 07 '15 at 00:43
  • 1
    I think this question is headed in the direction of [SO's documentation initiative](http://meta.stackoverflow.com/questions/303865/warlords-of-documentation-a-proposed-expansion-of-stack-overflow), and isn't unreasonable to answer in the meantime (obviously, since I did). All of the information is in the `Time` and `DateTime` documentation, but there's a lot of extraneous stuff to grok in there that someone with a little more experience can distill quickly to get the OP started. At this point in Ruby's life, the broad strokes are unlikely to change, so could be helpful to many future askers. – Kristján Nov 07 '15 at 01:33

1 Answers1

3

The simple equivalent of Python's datetime would be Ruby's Time.

The Time initializer accepts year, month, day, hour, minute, second, offset, so instantiating the earliest possible Time is start_time = Time.new(0).

Time will perform addition in seconds, so you can add 9 hours with start_time + (60 * 60 * 9) or 30 minutes with start_time + (60 * 30).

I'm not sure if Time#strftime is an exact match to Python's, but you can still get 09:00 AM with some_time.strftime('%I:%M %p').

Kristján
  • 18,165
  • 5
  • 50
  • 62