asctime() Function
The asctime()
function in C time.h converts a broken-down time, represented by a struct tm
, into a human-readable string that displays the date and time in a standardized format.
Syntax of asctime()
char* asctime(const struct tm *timeptr);
Parameters
Parameter | Description |
---|---|
timeptr | A pointer to a struct tm containing the calendar time information. |
The function interprets the contents of the provided time structure and converts it into a string of the format "Www Mmm dd hh:mm:ss yyyy"
. The resulting string is stored in an internal static buffer and is terminated with a newline character and a null-terminator. Keep in mind that subsequent calls to asctime()
may overwrite the content of this buffer.
Return Value
The function returns a pointer to a C-string containing the formatted date and time. This string is stored in a static internal buffer that may be altered by later calls to asctime()
or related functions.
Examples for asctime()
Example 1: Displaying the Current Local Time
This example shows how to obtain the current local time, convert it to a struct tm
using localtime()
, and then display the formatted string using asctime()
.
Program
#include <stdio.h>
#include <time.h>
int main() {
time_t currentTime = time(NULL);
struct tm *localTime = localtime(¤tTime);
printf("Current local time: %s", asctime(localTime));
return 0;
}
Explanation:
- The
time()
function retrieves the current time. localtime()
converts the time to astruct tm
representing local time.asctime()
converts thisstruct tm
to a formatted string.- The resulting string is printed to display the current local time.
Program Output:
Current local time: Sun Feb 23 04:59:21 2025
Example 2: Converting a Custom Time Structure
This example demonstrates how to manually set a struct tm
with specific date and time values and then convert it to a human-readable string using asctime()
.
Program
#include <stdio.h>
#include <time.h>
int main() {
struct tm customTime;
customTime.tm_year = 2023 - 1900; /* Years since 1900 */
customTime.tm_mon = 7; /* August (0-based index: 7 means August) */
customTime.tm_mday = 15;
customTime.tm_hour = 14;
customTime.tm_min = 30;
customTime.tm_sec = 45;
customTime.tm_wday = 2; /* Tuesday (0 = Sunday, 1 = Monday, 2 = Tuesday) */
customTime.tm_yday = 0; /* Not used by asctime() */
customTime.tm_isdst = -1; /* Not used by asctime() */
printf("Custom time: %s", asctime(&customTime));
return 0;
}
Explanation:
- A
struct tm
namedcustomTime
is manually initialized with specific date and time values. - The year is set by subtracting 1900, as required by the
struct tm
structure. asctime()
converts the structure into a formatted string.- The resulting string is printed to display the custom date and time.
Program Output:
Custom time: Tue Aug 15 14:30:45 2023
Example 3: Demonstrating the Static Buffer Behavior
This example illustrates that the string returned by asctime()
is stored in a static buffer, which may be overwritten by subsequent calls to the function.
Program
#include <stdio.h>
#include <time.h>
int main() {
time_t currentTime = time(NULL);
struct tm *tmInfo = localtime(¤tTime);
char *firstCall = asctime(tmInfo);
char *secondCall = asctime(tmInfo);
printf("First call: %s", firstCall);
printf("Second call: %s", secondCall);
return 0;
}
Explanation:
- The current local time is obtained and converted to a
struct tm
usinglocaltime()
. asctime()
is called twice with the samestruct tm
, returning pointers to the same internal static buffer.- The output of both calls is identical, demonstrating that the buffer is reused and may be overwritten by subsequent calls.
Program Output:
First call: Sun Feb 23 04:59:38 2025
Second call: Sun Feb 23 04:59:38 2025