Solved

mktime() returns -1

Posted on 2000-03-01
9
281 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 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