Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2004 10:12 AM
тАО09-13-2004 10:12 AM
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
Solved! Go to Solution.
- Tags:
- mktime
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2004 11:01 AM
тАО09-13-2004 11:01 AM
SolutionYou 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2004 11:04 AM
тАО09-13-2004 11:04 AM
Re: mktime()
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2004 11:12 AM
тАО09-13-2004 11:12 AM
Re: mktime()
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2004 11:18 AM
тАО09-13-2004 11:18 AM
Re: mktime()
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 ?.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2004 11:55 AM
тАО09-13-2004 11:55 AM
Re: mktime()
============================================#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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2004 03:45 PM
тАО09-13-2004 03:45 PM
Re: mktime()
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-14-2004 03:59 AM
тАО09-14-2004 03:59 AM
Re: mktime()
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-14-2004 06:02 AM
тАО09-14-2004 06:02 AM