Surge
Surge

Reputation: 277

Convert DDMON (eg. 23JAN) date format to full date with correct year applied in Ruby

Am looking for a compact solution for converting the dates that come from the airline reservation systems (GDS like Sabre/Galileo/Amadeus) which dont have the year - eg. 23JUN to a standard date, but need to determine the year quickly to tack on in Ruby. (FYI - on these systems date can only be max 355 days from today. So there is no ambiguity of interpreting 12MAY as 2013-05-12 instead of 2012-05-12 assuming today is 12MAY 2012). If today is 25th Dec 2012, and date is entered as 01JAN, this would be 01JAN2013 in the future, and if today is 21JUL 2012, input date of 01JAN would also become 01JAN 2013 as it is a past date in the current year.

So I can do something like:

t = DateTime.now
crsdate = DateTime.strptime("23JAN","%d%b")

if crsdate >= t
  # Year is current year
else
  # Year is next year
end

Is there a more elegant way of doing this?

Upvotes: 0

Views: 97

Answers (1)

Michael Kohl
Michael Kohl

Reputation: 66857

How about this?

t = DateTime.now
crsdate = DateTime.strptime("23JAN","%d%b")
crsdate >= t ? crsdate >>= 12 : crsdate

If you don't need to modify crsdate because you just want to return this, use >> instead of >>=.

Upvotes: 1

Related Questions