Next
Back
|
|
<time.h> - Date
and time
jump to: demonstration
of <time.h>
This heade provides functions for manipulating the date and
the time.
char *asctice(const struct tm *time); |
converts time as a structure to a formatted
character string containing exactly 26 characters, returns
a pointer to the character string result |
time_t clock(void); |
returns a number that gives the number of seconds elapsed
since the start of the current process if divided by the
marro CLOCKS_PER_SECOND |
char *ctime(vonst time_t &tt); |
converts time tt seconds, stored as a time_t to a formatted
character character string, returns a pointer to a formatted
character string |
doubt difftime(time_t1, time_t2); |
calculates the difference between two times t1 and t2,
returns the elapsed time in seconds t1-t2 |
struct tm *gmtime(const time_t *tt); |
converts the time value stored at tt to a structure
tim, returns a pointer to the structure result |
struct tm *localtime(const time_t *tt); |
converts time stored at tt to a structure, the function
uses global variables timezones and day/light to calculate
local time, returns a pointer to the structure result |
time_t mktime(struct tm *timeptr); |
converts the local time, stored in the structure timeptr,
into the format that would have been returned by a direct
call to the function time, returns the encoded time |
sizt_t strftime(char *s, size_t maxsize, const
char *format, const struct tm, *timeptr); |
plases caracters into the array pointed to by s according
to the format string format. Nno more that maximize characters
are output. Where s - specifies location into which characters
are to be placed, maxsize - specifies the maximum number
of characters to place into the s array, format - specifies
the format for the output and timeptr is a pointer to
the structure containing the time. The format string consists
of zero or more conversion specifiers and ordinary characters.
Each conversion specifier is replaced by characters as
specified in the list below. Other characters are output
directly. |
|
|
char |
meaning |
|
|
%a |
abbreviated weekly name |
%A |
full weekday name |
%b |
abbreviated month name |
%B |
full month name |
%c |
date and time representation |
%d |
day of month as a decimal number 00 - 31 |
%H |
hour as a decimal number 00 - 23 |
%I |
month as a decimal number 01 - 12 |
%j |
day of the year as a decimal umber 001 - 366 |
%m |
month as a decimal number 01-12 |
%M |
minute as a decimal number 00-59 |
%p |
AM or PM |
%S |
second as a decimal number |
%U |
week number of the year, with Sunday as day 1 - 00 -
53 |
%w |
week day as a decimal number, with Sunday = 0 - 0 -
6 |
%W |
week number of the year, with Monday as 1-00-53 |
%x |
date representation |
%X |
time representation |
%y |
year without centure 00 - 99 |
%Y |
year with century |
%% |
is replaced by % |
|
|
time_t time(time_t *tt); |
returns the number of seconds elapsed since 00:00:00
Greenwich Mean Time, January 1st 1970 |
|
|
In a listing of the <time.h> file the following definitions
are avaiable
typedef long time_t;
typedef long clock_t;
#define CLOCKS_PER_SEC 100
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdt;
};
Example:
Program to demonstrate the functions asctime, clock, gmtime,
localtime, strftime and time:
#include <stdio.h>
#include <time.h>
#define max_size 64
int main(void)
{
time_t time_now;
struct tm *time_ptr;
char string(max_size);
time(&time_now); /*
get time in seconds */
/* display local time */
pritnf("%s\n\n", asctime(localtime(&time_now)));
/* custom build the format of the
date/ time */
time_ptr = gmtime(&time_now);
strftime(string, max_size, "%H:%M
%p", time_ptr);
/* display new format string */
printf("%s\n\n", string);
getchar(); /*
introduces delay */
/* display elapsed time in seconds
since start of program */
printf("%d\n\n", (int)
clock() / CLOCK_PER_SEC);
return 0;
}
Results:
Fri Feb 10 18:59:32 1995
18:59 PM
10
back to
top back
to main
|
|
|