C function `sysdate'


Returns the system date as a null terminated character string. Within the calling program use the following preprocessor define, function prototype, and character string declaration
#define NCHAR_SYSDATE_BUFFER 256

void sysdate( char sysdate_buffer[], size_t nchar_sysdate_buffer );

size_t nchar_sysdate_buffer = NCHAR_SYSDATE_BUFFER;
char sysdate_buffer[NCHAR_SYSDATE_BUFFER];
and invoke as
sysdate( sysdate_buffer, nchar_sysdate_buffer );




#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <time.h>

void sysdate( char sysdate_buffer[], size_t nchar_sysdate_buffer )
{
  /*    DESCRIPTION: Returns the system date as a null terminated character string. */
  /*           FILE: sysdate.c                                                      */
  /*         AUTHOR: David Stepaniak, NCAR/CGD/CAS                                  */
  /* DATE INITIATED: July 2002                                                      */
  /*  LAST MODIFIED: July 2002                                                      */

  /* Commented declaratons follow Chapter 17, "Date and Time" of
    "GNU C library manual", especially 17.2.3, "Broken-down Time". */

  time_t curtime;
    /* type defintion time_t is equivalent to long int */

  struct tm *loctime;
    /* Data Type: struct tm
       This is the data type used to represent a broken-down time.
       The structure contains at least the following members, which
       can appear in any order:
       int tm_sec
              This is the number of seconds after the minute,
              normally in the range 0 through 59. (The actual
              upper limit is 60, to allow for leap seconds if
              leap second support is available.)
       int tm_min
              This is the number of minutes after the hour, in
              the range 0 through 59.
       int tm_hour
              This is the number of hours past midnight, in the
              range 0 through 23.
       int tm_mday
              This is the day of the month, in the range 1 through 31.
       int tm_mon
              This is the number of months since January, in the
              range 0 through 11.
       int tm_year
              This is the number of years since 1900.
       int tm_wday
              This is the number of days since Sunday, in the range
              0 through 6.
       int tm_yday
              This is the number of days since January 1, in the
              range 0 through 365.
       int tm_isdst
              This is a flag that indicates whether Daylight Saving Time
              is (or was, or will be) in effect at the time described. The
              value is positive if Daylight Saving Time is in effect, zero
              if it is not, and negative if the information is not available.
       long int tm_gmtoff
              This field describes the time zone that was used to compute
              this broken-down time value, including any adjustment for
              daylight saving; it is the number of seconds that you must
              add to UTC to get local time. You can also think of this as
              the number of seconds east of UTC. For example, for U.S. Eastern
              Standard Time, the value is -5*60*60. The tm_gmtoff field is
              derived from BSD and is a GNU library extension; it is not
              visible in a strict ISO C environment.
       const char *tm_zone
              This field is the name for the time zone that was used to compute
              this broken-down time value. Like tm_gmtoff, this field is a BSD
              and GNU extension, and is not visible in a strict ISO C environment. */

  size_t status;
    /* Integer to store return value of strftime function. */

  /* Get the current time. */
  curtime = time(NULL);
  /* Function: time_t time (time_t *result)
       The time function returns the current time as a value of type time_t.
       If the argument result is not a null pointer, the time value is also stored
       in *result. If the calendar time is not available, the value (time_t)(-1)
       is returned. */

  /* Convert curtime to local time representation. */
  loctime = localtime(&curtime);
  /* Function: struct tm * localtime (const time_t *time)
       The localtime function converts the calendar time pointed to by time to
       broken-down time representation, expressed relative to the user's specified
       time zone.
       The return value is a pointer to a static broken-down time structure, which
       might be overwritten by subsequent calls to ctime, gmtime, or localtime.
       (But no other library function overwrites the contents of this object.)
       Calling localtime has one other effect: it sets the variable tzname with
       information about the current time zone. (See section 17.2.6 Functions and
       Variables for Time Zones.) */
   if (loctime == NULL)
   {
    printf("localtime has returned a NULL pointer in sysdate()\n");
    exit(EXIT_FAILURE);
   }

  /* Write formatted members of loctime into the sysdate_buffer. */
  status = strftime( sysdate_buffer, nchar_sysdate_buffer, "%a %b %d %H:%M:%S %Z %Y", loctime );
  /* Function: size_t strftime (char *s, size_t size, const char *template,
       const struct tm *brokentime)
       This function is similar to the sprintf function (see section 7.12 Formatted
       Input), but the conversion specifications that can appear in the format template
       template are specialized for printing components of the date and time brokentime
       according to the locale currently specified for time conversion (see section 19.
       Locales and Internationalization).
       Ordinary characters appearing in the template are copied to the output string s;
       this can include multibyte character sequences. Conversion specifiers are
       introduced by a `%' character, followed by an optional flag which can be one of
       the following. These flags, which are GNU extensions, affect only the output of
       numbers:
       _ The number is padded with spaces.
       - The number is not padded at all.
       0 The number is padded with zeros even if the format specifies padding with spaces.
       ^ The output uses uppercase characters, but only if this is possible (see section
         4.2 Case Conversion).
       The default action is to pad the number with zeros to keep it a constant width.
       Numbers that do not have a range indicated below are never padded, since there is
       no natural width for them.
       Following the flag an optional specification of the width is possible. This is
       specified in decimal notation. If the natural size of the output is of the field
       has less than the specified number of characters, the result is written right
       adjusted and space padded to the given size.
       An optional modifier can follow the optional flag and width specification. The
       modifiers, which are POSIX.2 extensions, are:
       E Use the locale's alternate representation for date and time. This modifier
         applies to the %c, %C, %x, %X, %y and %Y format specifiers. In a Japanese
         locale, for example, %Ex might yield a date format based on the Japanese
         Emperors' reigns.
       O Use the locale's alternate numeric symbols for numbers. This modifier applies
         only to numeric format specifiers.
       If the format supports the modifier but no alternate representation is available,
       it is ignored.
       The conversion specifier ends with a format specifier taken from the following
       (abbreviated) list. The whole `%' sequence is replaced in the output string as
       follows:
       %a The abbreviated weekday name according to the current locale.
       %b The abbreviated month name according to the current locale.
       %d The day of the month as a decimal number (range 01 through 31).
       %H The hour as a decimal number, using a 24-hour clock (range 00 through 23).
       %M The minute as a decimal number (range 00 through 59).
       %S The second as a decimal number (range 00 through 60).
       %Y The year as a decimal number, using the Gregorian calendar. Years before
          the year 1 are numbered 0, -1, and so on.
       %Z The time zone abbreviation (empty if the time zone can't be determined).
       The size parameter can be used to specify the maximum number of characters to
       be stored in the array s, including the terminating null character. If the
       formatted time requires more than size characters, the excess characters are
       discarded. The return value from strftime is the number of characters placed
       in the array s, not including the terminating null character. If the value
       equals size, it means that the array s was too small; you should repeat the
       call, providing a bigger array.
       If s is a null pointer, strftime does not actually write anything, but instead
       returns the number of characters it would have written.
       According to POSIX.1 every call to strftime implies a call to tzset. So the
       contents of the environment variable TZ is examined before any output is produced.
       For an example of strftime, see section 17.2.7 Time Functions Example. */

}