Solved

mktime() returns -1

Posted on 2000-03-01
9
283 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

710 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