Solved

mktime() returns -1

Posted on 2000-03-01
9
280 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 142
C++ question 3 62
computer science syllabus 3 70
Error C2678: binary '!=': no operator found... 4 42
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now