Operating System - HP-UX
1757029 Members
1909 Online
108858 Solutions
New Discussion юеВ

Problem with "time" in C programming

 
SOLVED
Go to solution
Henry Chua
Super Advisor

Problem with "time" in C programming

I see some syntax on a c program script which I dun quite understand...hope u guys can help..
Does this require "time.h"?
"
long now;
struct tm *c_time;

now= time(0);
c_time= localtime(&now); "

and what dictate the value of c_time and now?

Thanks in advance
11 REPLIES 11
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Problem with "time" in C programming

# man 2 localtime

c_time is now a pointer to a struct that contains the broken down elements of the local time

The header file contains declarations of all relevant
functions and externals. It also contains the tm structure, which
includes the following members:

int tm_sec; /* seconds after the minute ├в [0,61] */
int tm_min; /* minutes after the hour ├в [0,59] */
int tm_hour; /* hours ├в [0,23] */
int tm_mday; /* day of month ├в [1,31] */
int tm_mon; /* month of year ├в [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday ├в [0,6] */
int tm_yday; /* days since January 1 ├в [0,365] */
int tm_isdst; /* daylight savings time flag */

And strict ANSI wise that code isn't even good:

--8<---
#incluse
#include

:
:

time_t now;
struct tm *c_time;

now = time (0);
c_time= localtime (&now);

(void)printf ("Now: %02d-%02d-%04d %02d:%02d:%02d\n", c_time->tm_mday, c_time->tm_mon + 1, c_time->tm_year + 1900, c_time->tm_hour, c_time->tm_min, c_time->tm_sec);
-->8---

Enjo
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Problem with "time" in C programming

Yes you need #inclide and I don't like your use of 0 in place of NULL.

The time() function simply returns the number of seconds since 00:00:00 1-Jan-1970 UTC. If you send a NULL pointer to time the number of seconds is returned; if you send in a non-null pointer to time() the the value is stored in the variable pointed to.

int main()
{
timt_t now = 0;
struct tm *t = NULL;

now = time((time_t *) NULL);
t = localtime(&now);
(void) printf("Year: %d\n",t->tm_year + 1900);
return(0);
} /* main */
If it ain't broke, I can fix that.
Henry Chua
Super Advisor

Re: Problem with "time" in C programming

Thanks for the info guys,

Just another question.. if my time(); = 1117524722, how can i decode them into ,year,month,day,hour,min,sec?

regards
Henry
Nguyen Anh Tien
Honored Contributor

Re: Problem with "time" in C programming

Yes. Of course. You must include time.h lib.
pls refer my program as example
tienna
HP is simple
Amit Agarwal_1
Trusted Contributor

Re: Problem with "time" in C programming

You can use strftime(). You can print the date in the format of your choice. You will need to pass c_time as last argument to strftime().
Hein van den Heuvel
Honored Contributor

Re: Problem with "time" in C programming

Henry,

Just re-read procura's reply. It's all there.

Now, instead of feeding now with time(), feed it your actual value!? (of course I would no longer call it 'now' but create a new time_t sometime; :-).

In quick perl examples:

# perl -e "print scalar localtime(1117524722)"
Tue May 31 03:32:02 2005

# perl -e "print join (',',localtime(1117524722))"
2,32,3,31,4,105,2,150,1
#


hth,
Hein.

sw_5
New Member

Re: Problem with "time" in C programming

In addition, you can use mktime() to convert 1117524722 back to the year, month, etc.
sw_5
New Member

Re: Problem with "time" in C programming

I have a question? All the samples about localtime() everywhere never free the struct. Could someone kindly let me know why?

c_time= localtime(&now);
delete c_time; // or use free for C
sw_5
New Member

Re: Problem with "time" in C programming

Sorry, scrap the previous suggestion for using mktime(). That is for reverse-direction from year, mon -> time in seconds.