C++ Programming/Code/Standard C Library/Time&Date
Standard C Time & Date
[edit | edit source ]This section will cover the Time and Date elements of the C Standard Library.
asctime
[edit | edit source ]#include<ctime> char*asctime(conststructtm*ptr);
The function asctime() converts the time in the struct 'ptr' to a character string of the following format:
day month date hours:minutes:seconds year
An example:
Mon Jun 26 12:03:53 2000
clock
[edit | edit source ]#include<ctime> clock_tclock(void);
The clock() function returns the processor time since the program started, or -1 if that information is unavailable. To convert the return value to seconds, divide it by CLOCKS_PER_SEC.
Note:
If your compiler and library is POSIX compliant, then CLOCKS_PER_SEC is always defined as 1000000.
ctime
[edit | edit source ]#include<ctime> char*ctime(consttime_t*time);
The ctime() function converts the calendar time time to local time of the format:
day month date hours:minutes:seconds year
using ctime() is equivalent to
asctime(localtime(tp));
difftime
[edit | edit source ]#include<ctime> doubledifftime(time_ttime2,time_ttime1);
The function difftime() returns time2 - time1, in seconds.
gmtime
[edit | edit source ]#include<ctime> structtm*gmtime(consttime_t*time);
The gmtime() function returns the given time in Coordinated Universal Time (usually Greenwich mean time), unless it's not supported by the system, in which case NULL is returned. Watch out for the static return.
localtime
[edit | edit source ]#include<ctime> structtm*localtime(consttime_t*time);
The function localtime() converts calendar time time into local time. Watch out for the static return.
mktime
[edit | edit source ]#include<ctime> time_tmktime(structtm*time);
The mktime() function converts the local time in time to calendar time, and returns it. If there is an error, -1 is returned.
setlocale
[edit | edit source ]#include<clocale> char*setlocale(intcategory,constchar*locale);
The setlocale() function is used to set and retrieve the current locale. If locale is NULL, the current locale is returned. Otherwise, locale is used to set the locale for the given category.
category can have the following values:
Value | Description |
---|---|
LC_ALL | All of the locale |
LC_TIME | Date and time formatting |
LC_NUMERIC | Number formatting |
LC_COLLATE | String collation and regular expression matching |
LC_CTYPE | Regular expression matching, conversion, case-sensitive comparison, wide character functions, and character classification. |
LC_MONETARY | For monetary formatting |
LC_MESSAGES | For natural language messages |
- Related topics
- (Standard C String & Character) strcoll
strftime
[edit | edit source ]#include<ctime> size_tstrftime(char*str,size_tmaxsize,constchar*fmt,structtm*time);
The function strftime() formats date and time information from time to a format specified by fmt, then stores the result in str (up to maxsize characters). Certain codes may be used in fmt to specify different types of time:
Code | Meaning |
---|---|
%a | abbreviated weekday name (e.g. Fri) |
%A | full weekday name (e.g. Friday) |
%b | abbreviated month name (e.g. Oct) |
%B | full month name (e.g. October) |
%c | the standard date and time string |
%d | day of the month, as a number (01-31) with a leading zero |
%-d | day of the month, as a number (1-31) without a leading zero |
%H | hour, 24 hour format (0-23) |
%I | hour, 12 hour format (1-12) |
%j | day of the year, as a number (1-366) |
%m | month as a number (1-12). |
%M | minute as a number (0-59) |
%p | locale's equivalent of AM or PM |
%S | second as a number (0-59) |
%U | week of the year, (0-53), where week 1 has the first Sunday |
%w | weekday as a decimal (0-6), where Sunday is 0 |
%W | week of the year, (0-53), where week 1 has the first Monday |
%x | standard date string |
%X | standard time string |
%y | year in decimal, without the century (0-99) |
%Y | year in decimal, with the century |
%Z | time zone name |
%% | a percent sign |
Note:
Some versions of Microsoft Visual C++ may use values that range from 0-11 to describe %m (month as a number).
time
[edit | edit source ]#include<ctime> time_ttime(time_t*time);
The function time() returns the current time, or -1 if there is an error. If the argument time is given, then the current time is stored in time.