views:

24

answers:

1

Hello,

I've been using Chronic, the natural language parser and its awesome. The problem I'm running into now is I cant parse the military time its give me back in to some form of AM/PM time that would be normal for a user to see.

 <%= Chronic.parse("next monday") %>

yields => Mon Jul 05 12:00:00 -0500 2010

Is there a way to go backwards so I can parse "Mon Jul 05 12:00:00 -0500 2010" into "Monday July 5th 5:00 AM" or even better yet just "5:00 AM"?

Wierd one I know, but I thought someone must have dealt with this before.

+2  A: 

Chronic is NOT returning the string

Mon Jul 05 12:00:00 -0500 2010

Rather, Chronic is returning an instance of the Time class

Since the erb <%= xxx %> wants a string, .to_s is being called automatically. So what's really happening:

<%= Chronic.parse("next monday").to_s %>

Sounds like you want a different output format. Use method strftime

<%= Chronic.parse("next monday").strftime("%I:%M %p") %>
  ==> 05:00 AM

or
<%= Chronic.parse("next monday").strftime("%A %B %I:%M %p") %>
  ==> Monday July 5 05:00 AM

(I'll leave it as an exercise to the reader to figure out how to make it July 5th rather than July 5 or 5:00 AM rather than 05:00 AM. Or ask another Q on SO)

Added:

Also note that you need to be aware of Timezones. The Chronic parsing is using local time and local timezones. (Local to the server.) That's what the -0500 means. You need to decide how to handle timezones in your app.

Larry K