The <time.h>
header file defines two macros, and declares four types and several functions for manipulating time and date information. Some functions process local time, which may differ from calendar time because of time zone.
size_t
An unsigned integral type of the result of the
sizeof
operator. clock_t time_t
Arithmetic types capable of representing times.
struct tm
Holds the components of a calendar time, called the broken-down time. The structure contains 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 since midnight -- [0,23] */ int tm_mday; /* day of the month -- [1,31] */ int tm_mon; /* months since January -- [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 Saving Time flag -- 0 if */ /* DST not in effect; positive if it is; */ /* negative if information is not available. */
NULL
Expands to an implementation-defined null pointer constant.
CLOCKS_PER_SEC
The number per second of the value returned by the
clock
function.
char *asctime(const struct tm *timeptr);
Converts a broken-down time in the structure pointed to by timeptr into a 26-character string in the form of this example:
Sat Sep 08 08:10:32 1990\n\0
A pointer to the string is returned.
Example
#include <stdio.h>
#include <string.h>
#include <time.h>
void main(void)
{
struct tm t;
t.tm_sec = 9; /* 초 */
t.tm_min = 10; /* 분 */
t.tm_hour = 31; /* 시간 */
t.tm_mday = 18; /* 날짜 */
t.tm_mon = 6; /* 달 */
t.tm_year = 94; /* 년 */
t.tm_wday = 4; /* 요일 */
t.tm_yday = 0; /* 출력되지 않음 */
t.tm_isdst = 0; /* 출력되지 않음 */
printf("%s",asctime(&t));
}
char *ctime(const time_t *timer);
Converts the calendar time pointed to by timer to local time in a string of the form generated by the
asctime
function. A pointer to the string is returned. The ctime
function is equivalent to the following: asctime(localtime(timer))
Example#include <stdio.h>
#include <time.h>
void main(void)
{
time_t t;
time(&t);
printf("Today's date and time: %s\n", ctime(&t));
}
struct tm *gmtime(const time_t *timer);
Converts the calendar time pointed to by timer into a broken-down time expressed as Coordinated Universal Time (UTC). The
gmtime
function returns a pointer to the broken- down time, or a null pointer if UTC is not available. Example
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr = "TZ=PST8PDT";
void main(void)
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t = time(NULL);
area = localtime(&t);
printf("Local time is: %s", asctime(area));
gmt = gmtime(&t);
printf("GMT is: %s", asctime(gmt));
}
struct tm *localtime(const time_t *timer);
Converts the calendar time pointed to by timer into a broken-down time expressed as local time. The
localtime
function returns a pointer to the broken- down time. Example
/* localtime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
Output:
Current local time and date: Sat May 20 17:36:17 2000
size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr);
Places characters into the array pointed to by s as controlled by the string pointed to by format. The format string consists of zero or more conversion specifiers and ordinary multibyte characters. All ordinary multibyte characters (including the terminating null character) are copied unchanged into the array. Each conversion specifier is replaced by the appropriate characters as shown in Table 9-2. The appropriate characters are determined by the
LC_TIME
category of the current locale and by the values contained in the structure pointed to by timeptr.
Table 9-2 strftime Conversion Specifiers
Specifier | Replaced by |
---|---|
%a |
The locale's abbreviated weekday name |
%A |
The locale's full weekday name |
%b |
The locale's abbreviated month name |
%B |
The locale's full month name |
%c |
The locale's appropriate date and time representation |
%d |
The day of the month as a decimal number (01 - 31) |
%H |
The hour (24-hour clock) as a decimal number (00 - 23) |
%I |
The hour (12- hour clock) as a decimal number (01 - 12) |
%j |
The day of the year as a decimal number (001 - 366) |
%m |
The month as a decimal number (01 - 12) |
%M |
The minute as a decimal number (00 - 59) |
%p |
The locale's equivalent of the AM/PM designations associated with a 12-hour clock |
%S |
The second as a decimal number (00 - 61) |
%U |
The week number of the year (the first Sunday as the first day of week 1) as a decimal number (00 - 53) |
%w |
The weekday as a decimal number (0 [Sunday] - 6) |
%W |
The week number of the year (the first Monday as the first day of week 1) as a decimal number (00 - 53) |
%x |
The locale's appropriate date representation |
%X |
The locale's appropriate time representation |
%y |
The year without century as a decimal number (00 - 99) |
%Y |
The year with century as a decimal number |
%Z |
The time zone name or abbreviation, or by no characters if no time zone can be determined |
%% |
% |
If the total number of resulting characters including the terminating null character is not more than maxsize
, the strftime
function returns the number of characters placed into the array pointed to by s, not including the terminating null character. Otherwise, 0 is returned, and the array contents are indeterminate.
Example
/* strftime example */ #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm * timeinfo; char buffer [80]; time ( &rawtime ); timeinfo = localtime ( &rawtime ); strftime (buffer,80,"Now it's %I:%M%p.",timeinfo); puts (buffer); return 0; } |
Example output:
Now it's 03:21PM. |
clock_t clock(void);
Determines the processor time used. The
clock
function returns the processor time used by the program since the beginning of an event related to the program invocation. To determine the time in seconds, divide the return value by the value of the CLOCKS_PER_SEC
macro. If the processor time is not available or cannot be represented, the value returned is (clock_t)-1
. (To measure the time spent in a program, call the clock
function at the start of the program and subtract the return value from that of subsequent calls.) Example
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
void main(void)
{
int i,j=0;
clock_t start, end;
start = clock();
for (i=0;i<300;i++) clrscr();
end = clock();
printf("elapsed time is: %f\n", (end - start) / CLK_TCK);
}
double difftime(time_t time1, time_t time0);
Returns the difference between the two calendar times time1 and time0, expressed in seconds, as a
double
. Example
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
void main(void)
{
time_t first, second;
clrscr();
printf("press any key after some wait\n");
first = time(NULL);
getch();
second = time(NULL);
printf("wait time is: %f seconds\n", difftime (second,
first));
getch();
}
time_t mktime(struct tm *timeptr);
Converts the broken-down time, expressed as local time, in the structure pointed to by timeptr into a calendar time value with the same encoding as that of the values returned by the
time
function (that is, a value of type time_t
), which it returns. If the calendar time cannot be represented, the value (time_t)-1
is returned.
The original values of the tm_wday
and tm_ yday
time components are ignored, and the original values of the other components are not restricted to the ranges indicated in the previous discussion of struct_tm
. Upon successful completion of the function, the values of the tm_wday
and tm_yday
components are set appropriately, and the other components are set to represent the specified calendar time, but with their values forced to the ranges indicated in the discussion of struct_tm
. The final value of tm_wday
is not set until tm_mon
and tm_year
are determined.
Example
/* mktime example: weekday calculator */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
/* prompt user for date */
printf ("Enter year: "); scanf ("%d",&year);
printf ("Enter month: "); scanf ("%d",&month);
printf ("Enter day: "); scanf ("%d",&day);
/* get current timeinfo and modify it to the user's choice */
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
/* call mktime: timeinfo->tm_wday will be set */
mktime ( timeinfo );
printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
return 0;
}
Output:
Enter year: 2000
Enter month: 5
Enter day: 20
That day is a Saturday.
time_t time(time_t *timer);
Returns the current calendar time. If the calendar time is not available, the value
(time_t)-1
is returned. Example
#include <stdio.h>
#include <dos.h>
void main(void)
{
time_t t;
time(&t);
printf("now GMT is %ld\n",t);
}
시간관련 테스트