Solved

mktime() returns -1

Posted on 2000-03-01
9
279 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:Jan Louwerens
Comment Utility
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
Comment Utility
oops! sorry, nietod, guess you beat me to the punch...
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
yeah, well, it was already written... I just cut and pasted it...
0
 

Author Comment

by:gsbabu
Comment Utility
thanks to both
--gsbabu
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
  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 …
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.

763 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

6 Experts available now in Live!

Get 1:1 Help Now