1

I am writing a Ruby script. It needs to accept time stamp from the user and then do some database queries using it. As far as I know the database is storing timestamps in UTC.

I want to let the users specify the time in following format: 2015-01-01 05:00 AM CST

From what Google tells me CST is 6 hours behind UTC, so after conversion to UTC I would expect the time to be 2014-12-31 11:00 PM UTC.

But that is not what I am seeing.

Time.strptime('2015-01-01 05:00 AM CST', '%Y-%m-%d %I:%M %P %Z').utc

prints

2015-01-01 11:00:00 UTC

Shouldn't it have printed 2014-12-31 11:00 PM UTC?

Time.strptime('2015-01-01 05:00 PM CST', '%Y-%m-%d %I:%M %Z').utc

prints

2015-01-01 11:00:00 UTC

The change from AM to PM doesn't seem to make any difference, how can that be?

septerr
  • 6,445
  • 9
  • 50
  • 73

1 Answers1

0

I had it backwards! UTC is AHEAD of CST, so when CST is converted to UTC 6 hours will be ADDED to the CST time!!

Ruby is doing it right.

So 5 AM CST = 11 AM UTC. And 5 PM CST = 11 PM UTC.

The difference in day happens if I test 11 PM CST:

Time.strptime('2015-01-01 11:00 PM CST', '%Y-%m-%d %I:%M %P %Z').utc

prints

2015-01-02 05:00:00 UTC

That is it correctly incremented the day.

septerr
  • 6,445
  • 9
  • 50
  • 73