public
Created

Showing date parsing problem using "%m/%d/%Y" as format. According to system level documentation (man 3 strptime) the formats between strftime and strptime should be reflexive. At the clib level, they are, but at the ruby level, they are not.

  • Download Gist
ruby-2.0.0-example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$ irb -r date
irb(main):001:0> format = "%m/%d/%Y"
=> "%m/%d/%Y"
irb(main):002:0> Date.today.strftime(format)
=> "08/18/2013"
irb(main):003:0> Date.parse(Date.today.strftime(format),format)
ArgumentError: invalid date
from (irb):3:in `parse'
from (irb):3
from /Users/tamara/.rubies/ruby-2.0.0-p427/bin/irb:12:in `<main>'
irb(main):004:0> Date.today.to_s
=> "2013-08-18"
irb(main):005:0> Date.parse(Date.today.to_s)
=> #<Date: 2013-08-18 ((2456523j,0s,0n),+0s,2299161j)>
irb(main):006:0> quit
$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
$ irb -v
irb 0.9.6(09/06/30)
test output
1 2 3 4 5 6
$ ./test
Current time from strftime: 08/18/2013
Parsed time:
Month: 8
Day: 18
Year: 2013
test.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <stdio.h>
#include <time.h>
 
int main(void)
{
time_t now;
now = time(NULL);
 
struct tm *now_tm;
now_tm = gmtime(&now);
 
char *format = "%m/%d/%Y";
char buf[25];
strftime(buf, sizeof(buf), format, now_tm);
 
printf("Current time from strftime: %s\n", buf);
 
struct tm now2_tm;
strptime(buf, format, &now2_tm);
printf("Parsed time: \n");
printf("Month:\t%4d\n",now2_tm.tm_mon+1);
printf("Day:\t%4d\n",now2_tm.tm_mday);
printf("Year:\t%4d\n",now2_tm.tm_year+1900);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.