1753496 Members
4183 Online
108794 Solutions
New Discussion юеВ

Re: mktime()

 
SOLVED
Go to solution
Sundar_7
Honored Contributor

mktime()


With very limited C skills, I have put together the following prog that takes the date as input and outputs the date in UNIX timestamp format.

I need this to test some password aging scripts we have.

But I cannot seem to get this working and mktime() always returns -1.

=============================================
#include
#include
main(int argc, char *argv[])
{
char *DAYS[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *MONTHS[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int DATE,YEAR,HR,MIN,SEC,i,DAY,MTH,TIMEST;
char WDAY[10],MONTH[10];

strcpy(WDAY,argv[1]);
strcpy(MONTH,argv[2]);
DATE=atoi(argv[3]);
HR=atoi(argv[4]);
MIN=atoi(argv[5]);
SEC=atoi(argv[6]);
YEAR=atoi(argv[7]);

printf("Input to the Program: %s %s %d %d:%d:%d %d\n",WDAY,MONTH,DATE,HR,MIN,SEC,YEAR);

for(i=0;strcmp(DAYS[i],NULL)!= 0;i++)
{
if (strcmp(DAYS[i],WDAY) == 0)
DAY=i;
}

for(i=0;strcmp(MONTHS[i],NULL)!=0;i++)
{
if(strcmp(MONTHS[i],MONTH) == 0)
MTH=i+1;
}

YEAR=YEAR-1900;

printf("%d %d %d %d %d %d %d\n",SEC,MIN,HR,DATE,MTH,YEAR,DAY);

TIMEST=mktime(SEC,MIN,HR,DATE,MTH,YEAR,DAY,0,1);

printf("TIMESTAMP : %d\n",TIMEST);

}
==============================================

# cc some.c
# ./a.out Mon Sep 13 10 37 08 2004
Input to the Program: Mon Sep 13 10:37:8 2004
8 37 10 13 9 104 1
TIMESTAMP : -1
#

==============================================

what am I missing here ?

- Sundar
Learn What to do ,How to do and more importantly When to do ?
8 REPLIES 8
Hein van den Heuvel
Honored Contributor
Solution

Re: mktime()

>>> what am I missing here ?

You should have opted for perl!

Seriously,
Check out 'man mktime'

You'll see the call takes only 1 argument, an array (ok, struct) containing those 9 arguments you passed.
You'll also see it tells you to '#include "
Do that. It declares 'struct tm' for you.

I'm sure you can take it from there....

Hein.


A. Clay Stephenson
Acclaimed Contributor

Re: mktime()

Your fundamental problem is that mktime() expects a pointer to struct tm as an argument not a bunch of integers. You need to build up a struct tm and then send this in:

First declare a struct tm then populate it.

struct tm t;
time_t seconds = 0;

t.tm_sec = SEC % 61;
t.tm_min = MIN % 60;
t.tm_hour = HR % 24;
t.tm_mday = DATE % 32;
t.tm_mon = MTH - 1 /* 0 - 11 */
t.tm_year = YEAR; /* year - 1900 */
t.tm_wday = t.tm_yday = 0; /* ignored */
t.tm_isdst = -1; /* This will may the system decide */

seconds = mktime(&t);
(void) printf("%ld\n",seconds);





If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: mktime()

oh... forgot to add. Check out recent topic:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=681997

It has some perl and shell code examples in this very area. (One of my replies there figures out time in seconds, just does nto print it).

Hein.
Sundar_7
Honored Contributor

Re: mktime()

Thanks for responding.

Yes, I understand mktime() accepts only the structure as the argument.

But I "based" my C prog on a perl excerpt that is listed on the URL

http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_20090888.html

I didnt want to just copy and use the perl code since I dont understand PERL and wanted to write something similar in C, which I understand a little bit.

the perl code in the site seem to call mktime with bunch of integers( $timestamp = mktime($sec, $min, $hour, $date, $month, $year, $wday, 0, -1);) and it is working. Wondering why a similar call to mktime() from a C program returns -1 ?.
Learn What to do ,How to do and more importantly When to do ?
Sundar_7
Honored Contributor

Re: mktime()

hmm..I got this working, atlast :-)

============================================#include
#include
struct t_ptr
{
int SEC;
int MIN;
int HR;
int DATE;
int MTH;
int YEAR;
int DAY;
int YDAY;
int ISDST;
};

main(int argc, char *argv[])
{
char *DAYS[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *MONTHS[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct t_ptr t1;
int i,TIMEST;
char WDAY[10],MONTH[10];

strcpy(WDAY,argv[1]);
strcpy(MONTH,argv[2]);
t1.DATE=atoi(argv[3]);
t1.HR=atoi(argv[4]);
t1.MIN=atoi(argv[5]);
t1.SEC=atoi(argv[6]);
t1.YEAR=atoi(argv[7]);
t1.YDAY=0;
t1.ISDST=-1;

printf("Input to the Program: %s %s %d %d:%d:%d %d\n",WDAY,MONTH,t1.DATE,t1.HR,t1.MIN,t1.SEC,t1.YEAR);

for(i=0;strcmp(DAYS[i],NULL)!= 0;i++)
{
if (strcmp(DAYS[i],WDAY) == 0)
t1.DAY=i;
}

for(i=0;strcmp(MONTHS[i],NULL)!=0;i++)
{
if(strcmp(MONTHS[i],MONTH) == 0)
t1.MTH=i;
}

t1.YEAR-=1900;

printf("%d %d %d %d %d %d %d\n",t1.SEC,t1.MIN,t1.HR,t1.DATE,t1.MTH,t1.YEAR,t1.DAY);

TIMEST=mktime(t1);

printf("TIMESTAMP : %d\n",TIMEST);

}
============================================

But still not clear why call to mktime() with bunch of integers always return -1 where as a similar call in perl works ?



Learn What to do ,How to do and more importantly When to do ?
Hein van den Heuvel
Honored Contributor

Re: mktime()

Perl does NOT offer the system naked services.
There is no one-on-one mapping.
It just offers look-a-like, sound-a-like, do-a-like function with a perl flavor to it.

Building structure is trivial in C, hard in PERL. So Perl provides a structure-less interface.

fwiw, I consider your program broken. You have not passed a tm struct as you are supposed to, just something that happend to look an awfull lot like a struct tm. That's confusing, poor practice, error prone and requires additional work. The errors will happen when 'int' suddenly changes to 64 bit or 16 bit. When a port is done. When you moved on. 'they' will keep time.h honest. Noone will take are of you struct t_ptr or will know (at first glance) what it might stand for.

Just do that include time.h and use struct tm.

>> But still not clear why call to mktime() with bunch of integers always return -1 where as a similar call in perl works ?

That's the perl value-add. It tries to make life easy for most, at the price of confusing some.


Cheers,
Hein.
Sundar_7
Honored Contributor

Re: mktime()

Thanks Hein. I understand it is a poor practice to define my own structure instead of using existing one.

I am not a seasoned programmer neither there going to be a quality audit :-). Bottom line is, the above code gets the work done for me.

But yours and Clay's posts are educational, as always.
Learn What to do ,How to do and more importantly When to do ?
Sundar_7
Honored Contributor

Re: mktime()

See above.
Learn What to do ,How to do and more importantly When to do ?