ctime() — Convert Time to Character String
Format
#include <time.h>
char *ctime(const time_t *time);
Language Level
ANSI
Threadsafe
No
Use ctime_r()
instead.
Locale Sensitive
The behavior of this function might be affected by the LC_TOD category of the current locale. For more information, see Understanding CCSIDs and Locales.
Description
The ctime()
function
converts the time value pointed to by time to
local time in the form of a character string. A time value is usually
obtained by a call to the time()
function.
ctime()
contains
exactly 26 characters and has the format: "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
For
example: Mon Jul 16 02:03:55 1987\n\0
The ctime()
function
uses a 24-hour clock format. The days are abbreviated to: Sun, Mon, Tue, Wed, Thu, Fri,
and Sat. The months are abbreviated to: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov,
and Dec. All fields have a constant width. Dates with only
one digit are preceded with a zero. The new-line character (\n)
and the null character (\0) occupy the last two positions
of the string.
Return Value
ctime()
function
returns a pointer to the character string result. If the function
is unsuccessful, it returns NULL. A call to the ctime()
function
is equivalent to: asctime(localtime(&anytime))
asctime()
and ctime()
functions,
and other time functions can use a common, statically allocated buffer
to hold the return string. Each call to one of these functions might
destroy the result of the previous call. The asctime_r()
, ctime_r()
, gmtime_r()
, and localtime_r()
functions
do not use a common, statically allocated buffer to hold the return
string. These functions can be used in place of asctime()
, ctime()
, gmtime()
, and localtime()
if reentrancy
is desired.Example
time()
.
It then prints a message giving the current date and time. #include <time.h>
#include <stdio.h>
int main(void)
{
time_t ltime;
time(<ime);
printf("the time is %s", ctime(<ime));
}
Related Information
- asctime() — Convert Time to Character String
- asctime_r() — Convert Time to Character String (Restartable)
- ctime_r() — Convert Time to Character String (Restartable)
- ctime64() — Convert Time to Character String
- ctime64_r() — Convert Time to Character String (Restartable)
- gmtime() — Convert Time
- gmtime64() — Convert Time
- gmtime64_r() — Convert Time (Restartable)
- gmtime_r() — Convert Time (Restartable)
- localtime() — Convert Time
- localtime64() — Convert Time
- localtime64_r() — Convert Time (Restartable)
- localtime_r() — Convert Time (Restartable)
- mktime() — Convert Local Time
- mktime64() — Convert Local Time
- setlocale() — Set Locale
- strftime() — Convert Date/Time to String
- time() — Determine Current Time
- time64() — Determine Current Time
- printf() — Print Formatted Characters
- <time.h>