C Function Asctime
## C Library Function - asctime()
The `asctime()` function is a built-in function in the C standard library defined in the `` header file. It converts a broken-down time structure (`struct tm`) into a human-readable, null-terminated C string representing the date and time.
---
## Syntax
```c
char *asctime(const struct tm *timeptr);
```
### Parameters
* **`timeptr`**: A pointer to a `const struct tm` structure containing the calendar time broken down into its individual components.
The `struct tm` structure is defined in `` as follows:
```c
struct tm {
int tm_sec; /* Seconds: range 0 to 59 (can be up to 60 to allow for leap seconds) */
int tm_min; /* Minutes: range 0 to 59 */
int tm_hour; /* Hours: range 0 to 23 */
int tm_mday; /* Day of the month: range 1 to 31 */
int tm_mon; /* Month: range 0 to 11 (January = 0) */
int tm_year; /* Year: Number of years since 1900 */
int tm_wday; /* Day of the week: range 0 to 6 (Sunday = 0) */
int tm_yday; /* Day of the year: range 0 to 365 */
int tm_isdst; /* Daylight Saving Time flag: positive if DST is in effect,
0 if not, negative if information is not available */
};
```
### Return Value
The function returns a pointer to a static string containing the formatted date and time. The format of the returned string is:
$$\text{\texttt{Www Mmm dd hh:mm:ss yyyy\textbackslash n}}$$
* **`Www`**: Three-letter abbreviation for the day of the week (e.g., `Mon`, `Tue`, `Sat`).
* **`Mmm`**: Three-letter abbreviation for the month (e.g., `Jan`, `Mar`, `Dec`).
* **`dd`**: Two-digit day of the month (padded with a space if the value is less than 10).
* **`hh:mm:ss`**: The hour, minute, and second formatted as two-digit numbers separated by colons.
* **`yyyy`**: The four-digit year.
* The string is terminated by a newline character (`\n`) and a null terminator (`\0`).
---
## Code Example
The following example demonstrates how to manually populate a `tm` structure and convert it into a readable string using `asctime()`.
```c
#include
#include
#include
int main()
{
struct tm t;
// Manually populating the time structure
t.tm_sec = 10; // Seconds
t.tm_min = 10; // Minutes
t.tm_hour = 6; // Hours (6 AM)
t.tm_mday = 25; // Day of the month
t.tm_mon = 2; // Month (March: 0 = Jan, 1 = Feb, 2 = Mar)
t.tm_year = 89; // Year (89 represents 1900 + 89 = 1989)
t.tm_wday = 6; // Day of the week (Saturday: 0 = Sun, 6 = Sat)
// Convert the structure to a string and print it
puts(asctime(&t));
return 0;
}
```
### Output
```text
Sat Mar 25 06:10:10 1989
```
---
## Important Considerations
### 1. Thread Safety and Shared Buffer
The `asctime()` function returns a pointer to a **statically allocated buffer** that is shared by both `asctime()` and `ctime()`. Each call to either of these functions overwrites the contents of this buffer.
* If you need to preserve the returned string, copy it to your own buffer using `strcpy()` or `strncpy()` before calling these functions again.
* In multi-threaded environments, using `asctime()` can lead to race conditions.
### 2. Modern Alternatives
Because of the thread-safety issues associated with `asctime()`, modern standards and platforms recommend safer alternatives:
* **`asctime_r()`**: The POSIX thread-safe version, which requires you to pass a user-allocated buffer to store the result.
* **`asctime_s()`**: The C11 standard bounds-checked alternative.
* **`strftime()`**: A highly customizable and safe formatting function that should generally be preferred over `asctime()` for production code.
YouTip