LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-27-2004, 04:14 PM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
Angry mktime() returns -1


Ah, another question from me.

Im trying to convert a mysql-esque time string to a unix timestamp, but the
mktime function keeps reuurning -1.

I tried the tm_isdst as -1, 0 and 1 to no avail.

Heres my function, thanks for any help

[FORMAT]
time_t mysql_time_to_unix_time(char * mysqltimestr)
{
struct tm * tm;

tm= new struct tm;

sscanf(mysqltimestr,"%4d",&tm->tm_year);
sscanf(&mysqltimestr[5],"%2d",&tm->tm_mon);
sscanf(&mysqltimestr[8],"%2d",&tm->tm_mday);
sscanf(&mysqltimestr[11],"%2d",&tm->tm_hour);
sscanf(&mysqltimestr[14],"%2d",&tm->tm_min);
sscanf(&mysqltimestr[17],"%2d",&tm->tm_sec);

printf("Year=%d\n",tm->tm_year);
printf("Month=%d\n",tm->tm_mon);
printf("Day=%d\n",tm->tm_mday);
printf("Hour=%d\n",tm->tm_hour);
printf("Minute=%d\n",tm->tm_min);
printf("Second=%d\n",tm->tm_sec);

delete tm;

return mktime(tm);
}
[/FORMAT]
 
Old 01-27-2004, 04:20 PM   #2
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
Sorry , before any of you call me a dumbass heres the real function (still doesnt work)
time_t mysql_time_to_unix_time(char * mysqltimestr)
{
struct tm * tm;
time_t t;
tm= new struct tm;

sscanf(mysqltimestr,"%4d",&tm->tm_year);
sscanf(&mysqltimestr[5],"%2d",&tm->tm_mon);
sscanf(&mysqltimestr[8],"%2d",&tm->tm_mday);
sscanf(&mysqltimestr[11],"%2d",&tm->tm_hour);
sscanf(&mysqltimestr[14],"%2d",&tm->tm_min);
sscanf(&mysqltimestr[17],"%2d",&tm->tm_sec);

printf("Year=%d\n",tm->tm_year);
printf("Month=%d\n",tm->tm_mon);
printf("Day=%d\n",tm->tm_mday);
printf("Hour=%d\n",tm->tm_hour);
printf("Minute=%d\n",tm->tm_min);
printf("Second=%d\n",tm->tm_sec);

t=mktime(tm);

delete tm;

return t;
}
 
Old 01-27-2004, 04:38 PM   #3
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
Never mind, I figured it out myself ... I had to reduce the month by 1 and the year by 1900
 
  


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
fscanf returns -1 trutnev Programming 2 06-23-2005 08:50 AM
Carriage Returns Thorkyl Linux - Software 7 06-28-2004 05:42 PM
setup returns after doing nothing?? dfong63 Slackware - Installation 5 06-04-2004 01:40 AM
c difftime returns zero mr.neil Programming 3 06-02-2004 12:43 PM
Bind returns -1 slackwarefan Programming 18 05-31-2004 12:36 AM

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

All times are GMT -5. The time now is 04:52 PM.

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