Created
August 18, 2013 18:54
-
-
Save tamouse/6263292 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./test | |
Current time from strftime: 08/18/2013 | |
Parsed time: | |
Month: 8 | |
Day: 18 | |
Year: 2013 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment