12.2. Time Conversion Functions

12.2.1. The asctime function

Synopsis
#include <time.h>

char *asctime (const struct tm * timeptr );

Description

The asctime function converts the broken-down time in the structure pointed to by timeptr into a string in the form:

Sun Sep 16 01:03:52 1973\n\0

using the equivalent of the following algorithm.

char *asctime(const struct tm *timeptr)
{
  static const char wday_name[7][3] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[12][3] = {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result [26];
  sprintf(result, "%.3s %.3s %.2d %.2d:%.2d:%.2d %d\n",
          wday_name[timeptr->tm_wday], 
          mon_name[timeptr->tm_mon],
          timeptr->tm_mday, timeptr->tm_hour, 
          timeptr->tm_min, timeptr->tm_sec, 
          1900 + timeptr->tm_year);
  return result;
}
Returns

The asctime function returns a pointer to the string.

12.2.2. The ctime function

Synopsis
#include <time.h>

struct tm * ctime (const time * timer );

Description

The ctime function converts the calendar time pointed to by timer into a broken-down time expressed as local time.

Returns

The ctime function returns the pointer returned by the asctime function with that broken-down time as argument.

See Also

Section 12.2.4

12.2.3. The gmtime function

Synopsis
#include <time.h>

struct tm * gmtime (const time * timer );

Description

The gmtime function converts the calendar time pointed to by timer into a broken-down time expressed as Coordinated Universal Time (UTC).

Returns

The gmtime function returns a pointer to that object, or a null pointer if UTC is not available.

12.2.4. The localtime function

Synopsis
#include <time.h>

struct tm * localtime (const time * timer );

Description

The localtime function converts the calendar time pointed to by timer into a broken-down time expressed as local time.

Returns

The localtime function returns a pointer to that object.

12.2.5. The strftime function

Synopsis
#include <time.h>

size_t strftime (char * s , size_t maxsize , const char * format , const struct tm * timeptr );

Description

The strftime function places characters into the array pointed to by s as controlled by the string pointed to by format. The format shall be a multi-byte character sequence, beginning and ending in its initial shift state. The format string consists of zero or more conversion specifiers and ordinary multi-byte characters. A conversion specifier consists of a % character followed by a character that determines the behavior of the conversion specifier. All ordinary multi-byte characters (including the terminating null character) are copied unchanged into the array. If copying takes place between objects that overlap, the behavior is undefined. No more than maxsize characters are placed into the array. Each conversion specifier is replaced by appropriate characters as described in the following list. The appropriate characters are determined by the LC_TYPE category of the current locale and by the values contained in the structure pointed to by timeptr.

  %a    is replaced by the locale's abbreviated weekday name (Sun)
  %A    is replaced by the locale's full weekday name (Sunday)
  %b    is replaced by the locale's abbreviated month name (Dec)
  %B    is replaced by the locale's full month name (December)
  %c    is replaced by the locale's date and time (Dec  2 06:55:15 1979)
  %d    is replaced by the day of the month (02)
  %H    is replaced by the hour of the 24-hour day (06)
  %I    is replaced by the hour of the 12-hour day (06)
  %j    is replaced by the day of the year, from 001 (335)
  %m    is replaced by the month of the year, from 01 (12)
  %M    is replaced by the minutes after the hour (55)
  %p    is replaced by the locale's AM/PM indicator (AM)
  %S    is replaced by the seconds after the minute (15)
  %U    is replaced by the Sunday week of the year, from 00 (48)
  %w    is replaced by the day of the week, from 0 for Sunday (6)
  %W    is replaced by the Monday week of the year, from 00 (47)
  %x    is replaced by the locale's date (Dec  2 1979)
  %X    is replaced by the locale's time (06:55:15)
  %y    is replaced by the year of the century, from 00 (79)
  %Y    is replaced by the year (1979)
  %Z    is replaced by the time zone name, if any (EST)
  %%    is replaced by the percent character %

If a conversion specifier is not one of the above, the behavior is undefined.

Returns

If the total number of resulting characters including the terminating null character is not more than maxsize, the strftime function returns the number of characters placed into the array pointed to by s not including the terminating null character. Otherwise, zero is returned and the contents of the array are indeterminate.