LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2010, 03:15 PM   #1
lxvor
LQ Newbie
 
Registered: Jan 2009
Posts: 13

Rep: Reputation: 0
why does strptime (c++) seem to not correctly parse date/time string????


so I'll come right to the point. here is the problem code using strptime() function.

==================
#include <stdio.h>
#include <string.h>
#include <time.h>

int main()
{
memset(&tm_obj, 0 , sizeof(tm_obj));
strptime("2009-12-24T9:47:06", "%Y-%m-%dT%H:%M:%S", &tm_obj);

printf("year: %d\n", tm_obj.tm_year);
printf("month: %d\n", tm_obj.tm_mon);
printf("day: %d\n", tm_obj.tm_mday);
printf("hour: %d\n", tm_obj.tm_hour);
printf("min: %d\n", tm_obj.tm_min);
printf("sec: %d\n", tm_obj.tm_sec);
return 0;
}
output:
-----------
year: 109
month: 11
day: 24
hour: 9
min: 47
sec: 6
===================

Why are the Month and Year so messed up?? I am using strptime() according to the man page. Does anyone have a clue.

I would really appreciate if somebody could explain this to me.

Thanks.
 
Old 02-08-2010, 03:23 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Ive never used any of these functinons. However, a quick look at a man page of the function seems that strptime creates a time structure, from the given string representation of a time. However, the time struct it creates is still not useful to directly extract the day, etc, out of it. It seems there is another function strftime that has similar specifiers to extract the necessary information and put it in a buffer.

The man page I saw was this: http://linux.about.com/library/cmd/blcmdl3_strptime.htm. See the "Example" section, for both creation of the struct and extracting fields from it.

Last edited by nadroj; 02-08-2010 at 03:24 PM.
 
Old 02-08-2010, 05:08 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by lxvor View Post
Why are the Month and Year so messed up?
The month and year are correct. Here is the definition of "struct tm" from the man page for "localtime":
Code:
       Broken-down time is stored in the structure tm which is
       defined in <time.h> as follows:

              struct tm {
                  int tm_sec;         /* seconds */
                  int tm_min;         /* minutes */
                  int tm_hour;        /* hours */
                  int tm_mday;        /* day of the month */
                  int tm_mon;         /* month */
                  int tm_year;        /* year */
                  int tm_wday;        /* day of the week */
                  int tm_yday;        /* day in the year */
                  int tm_isdst;       /* daylight saving time */
              };

       The members of the tm structure are:

       tm_sec The number of seconds after the minute, normally in the
       range 0 to 59, but can be up to 60 to allow for leap seconds.

       tm_min The number of minutes after the hour, in the range 0 to
       59.

       tm_hour
              The number of hours past midnight, in the range 0 to 23.

       tm_mday
              The day of the month, in the range 1 to 31.

       tm_mon The number of months since January, in the range 0 to 11.

       tm_year
              The number of years since 1900.

       tm_wday
              The number of days since Sunday, in the range 0 to 6.

       tm_yday
              The number of days since January 1, in the range 0 to 365.

       tm_isdst
              A  flag that indicates whether daylight saving time is in
              effect at the time described.  The value is positive if
              daylight saving time is in effect, zero if it is not, and
              negative if the information is not available.
Note that "tm_mon" is in the range 0 to 11 inclusive (Thus the month 11 in your output) and that "tm_year" starts at 1900 (Thus the year 109 in your output).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
Date/Time not updating correctly. PeteBass Linux - Software 1 05-30-2007 08:48 AM
c++ parse date string?? blizunt7 Programming 25 05-18-2006 03:12 PM
time strptime problem tara Programming 2 01-15-2006 10:59 PM
How to correctly set date/time? Jakerohs Linux - General 5 01-08-2004 03:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration