Solved

mktime() returns -1

Posted on 2000-03-01
9
284 Views
Last Modified: 2010-04-02
Hi

Here's my code,

------
#include <iostream>
#include <time.h>

struct tm tm_str ;

int main(int argc, char* argv[])
{
  tm_str.tm_mday  = (int) argv[1] ;
  tm_str.tm_mon   = (int) argv[2] ;
  tm_str.tm_year  = (int) argv[3] ;

  tm_str.tm_mon   -= 1 ;
  cout << mktime(&tm_str) << endl ;  
}
------

when i compile and execute this i get the output as -1, instead i need to get the number of seconds since from jan 1, 1970. please help me how should i get this.

--gsbabu
0
Comment
Question by:gsbabu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2573764
make sure that the year you're passing in is actually the number of years since 1900. So, for year 2001, tm_year should be 101.
0
 

Author Comment

by:gsbabu
ID: 2573822
Even though the year has been passed correctly, i get -1 as output.
more helps expected.
--gsbabu
0
 
LVL 22

Accepted Solution

by:
nietod earned 10 total points
ID: 2573862
The entries in argv will be strings (char *).  but the members in the tm structure are integers.  These are different types.  Code like

 tm_str.tm_mday  = (int) argv[1] ;

will just set the tm_mday data member to garbage.

You need to set it to the proper integer value _represented_ by the string.  You can use the atoi() funciton for this, like

 tm_str.tm_mday  = atoi(argv[1]);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2573880
oh!! the argv[] array returns char*. You can't just cast the char* to an int. try this instead:


tm_str.tm_mday  = atoi(argv[1]) ;
tm_str.tm_mon   = atoi(argv[2]) ;
tm_str.tm_year  = atoi(argv[3]) ;
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2573881
Here's a snippet of code I use as a utility:

      int year;
      int month;
      int day;
      int hour;
      int minute;
      int second;

....
  year = atoi(argv[0]);
   if ((year < 1900) || (year > 9999))
   {
      cerr << "year must be in range (1900 - 9999)" << endl;
      resultCode = 1;
   }
   year -= 1900; // struct tm stores as years since 1900

   month = atoi(argv[1]);
   if ((month < 1) || (month > 12))
   {
      cerr << "month must be in range (1 - 12)" << endl;
      resultCode = 1;
   }
   month--; // struct tm stores months as 0-11

   day = atoi(argv[2]);
   if ((day < 1) || (day > 31))
   {
      cerr << "day must be in range (1 - 31)" << endl;
      resultCode = 1;
   }

   if (argc >= 4)
      hour = atoi(argv[3]);
   if ((hour < 0) || (hour > 23))
   {
      cerr << "hour must be in range (0 - 23)" << endl;
      resultCode = 1;
   }

   if (argc >= 5)
      minute = atoi(argv[4]);
   if ((minute < 0) || (minute > 59))
   {
      cerr << "minute must be in range (0 - 59)" << endl;
      resultCode = 1;
   }

   if (argc >= 6)
      second = atoi(argv[5]);
   if ((second < 0) || (second > 59))
   {
      cerr << "second must be in range (0 - 59)" << endl;
      resultCode = 1;
   }

....

   struct tm tmDate;

   tmDate.tm_year = year;
   tmDate.tm_mon = month;
   tmDate.tm_mday = day;
   tmDate.tm_hour = hour;
   tmDate.tm_min = minute;
   tmDate.tm_sec = second;

   cout << mktime(&tmDate);

0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2573886
oops! sorry, nietod, guess you beat me to the punch...
0
 
LVL 22

Expert Comment

by:nietod
ID: 2573901
Your code is a little better though.  Much much more than I woudl do for 10 points.
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2573947
yeah, well, it was already written... I just cut and pasted it...
0
 

Author Comment

by:gsbabu
ID: 2574011
thanks to both
--gsbabu
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question