
help-get current date using time.h
Groovy hepcat Marcus Wunderlich was jivin' on 15 Oct 1998 16:57:17 GMT
in comp.lang.c.
help-get current date using time.h's a cool scene! Dig it!
Quote:
>tm* current_time;
There is no tm type. Try struct tm instead.
Quote:
>time_t* time_ptr;
>time(time_ptr);
time_ptr must be initialised for this to work. A better way is to
create a time_t, not a time_t*, and simply assign the return from
time() to it. Pass NULL as an argument to time().
time_t t;
t = time(NULL);
Quote:
>current_time=locattime(time_ptr);
current_time will now contain a struct containing members
representing different parts of the local time and date. struct tm is
defined as follows:
struct tm
{
int tm_sec; /* seconds after the minute (0 - 60) */
int tm_min; /* minutes after the hour (0 - 60) */
int tm_hour; /* hours since midnight (0 - 23) */
int tm_mday; /* day of month (0 - 31) */
int tm_mon; /* month since January (0 - 11) */
int tm_year; /* years since 1900 */
int tm_wday; /* weekday (days since sunday) (0 - 6) */
int tm_yday; /* day since 1st january (0 - 365) */
int tm_isdst; /* daylight saving flag (0 = standard time, +ve =
daylight saving time, -ve = not available) */
Quote:
}
So, to display the current local time, you could do something like
this:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t;
struct tm *lt;
t = time(NULL);
lt = localtime(&t);
printf("The time (HH:MM:SS) is %02d:%02d:%02d.\n", lt->tm_hour,
lt->tm_min, lt->tm_sec);
return 0;
Quote:
}
--
----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----
-------------- Shaggy was here! ---------------
http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============