Escolar Documentos
Profissional Documentos
Cultura Documentos
h)
time
Get the current calendar time as a time_t object.
The function returns this value, and if the argument is not a null pointer, the value is also
set to the object pointed by timer.
Parameters
Alternatively, this parameter can be a null pointer, in which case the parameter is not
used, but a time_t object is still returned by the function.
Return Value
If the argument is not a null pointer, the return value is the same as the one stored in the
location pointed by the argument.
If the function could not retrieve the calendar time, it returns a -1 value.
Example
/* time example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
return 0;
}
Localtime
Convert time_t to tm as local time
Uses the time pointed by timer to fill a tm structure with the values that represent the
corresponding local time.
Parameters
timer -Pointer to a time_t value representing a calendar time (see time_t).
Return Value
This structure is statically allocated and shared by the functions gmtime and localtime.
Each time either one of these functions is called the content of this structure is
overwritten.
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;
}
Strftime
Format time to string
Copies into ptr the content of format, expanding its format tags into the corresponding
values as specified by timeptr, with a limit of maxsize characters.
Parameters
format
C string containing any combination of regular characters and special format specifiers.
These format specifiers are replaced by the function to the corresponding values to
represent the time specified in timeptr. They all begin with a percentage (%) sign, and are:
* The specifiers whose description is marked with an asterisk (*) are locale-dependent.
timeptr
Pointer to a tm structure that contains a calendar time broken down into its components
(see tm).
/* strftime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return 0;
}
ctime
char * ctime ( const time_t * timer );
Parameters
Return Value
The array which holds this string is statically allocated and shared by both the ctime and
asctime functions. Each time either one of these functions is called the content of this
array is overwritten.
Example
/* ctime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
time ( &rawtime );
printf ( "The current local time is: %s", ctime (&rawtime) );
return 0;
}
time
time_t time ( time_t * timer );
The function returns this value, and if the argument is not a null pointer, the value is also
set to the object pointed by timer.
Parameters
Pointer to an object of type time_t, where the time value is stored.
Alternatively, this parameter can be a null pointer, in which case the parameter is not
used, but a time_t object is still returned by the function.
Return Value
The current calendar time as a time_t object.
If the argument is not a null pointer, the return value is the same as the one stored in the
location pointed by the argument.
If the function could not retrieve the calendar time, it returns a -1 value.
Example
/* time example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
return 0;
}
mktime
time_t mktime ( struct tm * timeptr );
The original values of the members tm_wday and tm_yday of timeptr are ignored, and the
ranges of values for the rest of its members are not restricted to their normal values (like
tm_mday being between 1 and 31).
The object pointed by timeptr is modified, setting the tm_wday and tm_yday to their
appropiate values, and modifying the other members as necessary to values within the
normal range representing the specified time.
Parameters
timeptr
Pointer to a tm structure that contains a calendar time broken down into its components
(see tm).
Return Value
int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
return 0;
}
Difftime
Return Value
The difference in seconds (time2-time1) as a floating point double.
Example
/* difftime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t start,end;
char szInput [256];
double dif;
time (&start);
printf ("Please, enter your name: ");
gets (szInput);
time (&end);
dif = difftime (end,start);
printf ("Hi %s.\n", szInput);
printf ("It took you %.2lf seconds to type your name.\n", dif );
return 0;
}
clock_t
Clock type
Type capable of representing clock tick counts and support arithmetical operations.
This type is returned by the clock function of the <ctime> header to represent the number
of click ticks since the beginning of the program execution.
In <ctime>, it is used in the function strftime as the type of its parameter maxsize and as
its return value. In both cases it used to express counts of characters.
time_t
Time type
Type capable of representing times and support arithmetical operations.
This type is returned by the time function and is used as parameter by some other
functions of the <ctime> header.
Time structures
struct tm
Structure containing a calendar date and time broken down into its components.
The structure contains nine members of type int, which are (in any order):
1 int tm_sec;
2 int tm_min;
3 int tm_hour;
4 int tm_mday;
5 int tm_mon;
6 int tm_year;
7 int tm_wday;
8 int tm_yday;
9 int tm_isdst;
* tm_sec is generally 0-59. Extra range to accommodate for leap seconds in certain
systems.
Asctime
Interprets the contents of the tm structure pointed by timeptr as a calendar time and
converts it to a C string containing a human-readable version of the corresponding date
and time.
Where Www is the weekday, Mmm the month in letters, dd the day of the month,
hh:mm:ss the time, and yyyy the year.
The string is followed by a new-line character ('\n') and the terminating null-character.
Parameters
timeptr - Pointer to a tm structure that contains a calendar time broken down into its
components (see tm).
Return Value
A C string containing the date and time information in a human-readable format.
The array which holds this string is statically allocated and shared by both the ctime and
asctime functions. Each time either one of these functions is called the content of this
array is overwritten.
Example
/* asctime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "The current date/time is: %s", asctime (timeinfo) );
return 0;
}