#464 Parsing Dates

cheeser Tue 17 Feb 2009

I've looked through the API but I can't find anything like fromStr(Str pattern) for parsing dates/times. Am I missing something?

brian Tue 17 Feb 2009

I don't have a generic DateTime which takes a pattern yet.

But I do have dedicated methods for parsing some common formats:

  • DateTime.fromStr // Fan format ISO + timezone
  • DateTime.fromIso
  • DateTime.fromHttpStr

What is the format you are parsing?

cheeser Tue 17 Feb 2009

"Sun Feb 15 03:47:00 +0000 2009"

So currenly what i'm doing it splitting it, reordering it, converting that "Feb" to a number, then using fromStr(). And that works, but is a bit cumbersome. On that note, is there a nicer way to pad "2" to "02" than check if the original Int is < 10 and concatting? I couldn't find anything in the docs for that either.

brian Wed 18 Feb 2009

Sun Feb 15 03:47:00 +0000 2009

Is that seriously what twitter uses as a time format in its protocol? I would think everybody uses ISO 8601 by now.

Anyways I really need to add support for parsing an arbitrary pattern. My plan is to add a DateTime.fromLocale which will parse with an arbitrary pattern (inverse of toLocale). Although many of these HTTP like formats use english weekday/month, so I will probably always accept English or the weekday/month of the current locale.

Since it has been on my todo list for about 2 years now I guess I'll knock it out this week.

On that note, is there a nicer way to pad "2" to "02" than check if the original Int is < 10 and concatting?

I ran across that on Sun myself, and just pushed a Str.padr and padl method to hg. I also will be adding a Int.toLocale method that takes an arbitrary pattern like java's NumberFormat.

cheeser Wed 18 Feb 2009

Is that seriously what twitter uses as a time format in its protocol? I would think everybody uses ISO 8601 by now.

Anyways I really need to add support for parsing an arbitrary pattern. My plan is to add a DateTime.fromLocale which will parse with an arbitrary pattern (inverse of toLocale). Although many of these HTTP like formats use english weekday/month, so I will probably always accept English or the weekday/month of the current locale.

Since it has been on my todo list for about 2 years now I guess I'll knock it out this week.

Fantastic. I'm not proud of the code I had to write to get around that. :)

On that note, is there a nicer way to pad "2" to "02" than check if the original Int is < 10 and concatting?

I ran across that on Sun myself, and just pushed a Str.padr and padl method to hg. I also will be adding a Int.toLocale method that takes an arbitrary pattern like java's NumberFormat.

Great. Thanks.

Login or Signup to reply.