Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match the YYYY-MM-DDThh:mm:ssTZD timezone format in ruby

I would like to check the iso8601 format for the date entered in ruby , like start_date = "2011/05/05" should be matched for the format 2011-05-05T00:00:00-04:00 and errors returned accordingly. Should we use regex here or any method is present for this?

like image 950
Bijendra Avatar asked Jan 18 '12 11:01

Bijendra


People also ask

What is the format for the date-time in the text?

The date-time is inserted in the format YYYY-MM-DDThh:mm:ssTZD . Attribute Values: This attribute contains single value YYYY-MM-DDThh:mm:ssTZD which is used to specify the date and time when the text was deleted.

How to convert UTC timecode to Z?

If you look at the original post you will see that the date was reported with a 'Z' at the end. That is the way it comes from Excel (At least until MS change the connector back). The UTC timecode will convert directly using formatdatetime.

What time zone is the date in?

Actually, the date is in ISO 8601 UTC time zone format. A formatDateTime () will work just fine without the extra steps. But be aware Excel using this format was due to a regression bug.

What does YYYYY mean in SQL Server?

YYYY: It sets the year of datetime object (e.g. 2009). MM: It sets the month of datetime object (e.g. 05 for March). DD: It sets the day of the month of datetime object (e.g. 04). T: It is a required separator. hh: It sets the hour of datetime object (e.g. 18 for 06.00pm).


1 Answers

Sounds like you want Time.iso8601:

require 'time'
iso = Time.iso8601(start_date)

See this blog post for more information.

EDIT: Here's a short but complete test program which works:

require 'time'
text = "2011-05-05T00:00:00-04:00"
parsed = Time.iso8601(text)
puts parsed

Output:

Thu May 05 04:00:00 UTC 2011
like image 194
Jon Skeet Avatar answered Sep 29 '22 07:09

Jon Skeet