Purpose

These functions convert time values between time_t, tm structure, and char * forms.

Header File

time.h

Prototypes

char * asctime (const struct tm * Structured);
char * ctime(const time_t * Linear);
struct tm * localtime (const time_t * Linear);
struct tm * gmtime (const time_t * Linear);
time_t mktime (struct tm * Structured);

Arguments

Linear
Seconds since midnight before January 1, 1970 GMT.
Structured
A pointer to a structure containing decoded values:
struct tm {
  int   tm_sec;     /* 0..59 */
  int   tm_min;     /* 0..59 */
  int   tm_hour;    /* 0..23 */
  int   tm_mday;    /* 1..31 */
  int   tm_mon;     /* 0..11 */
  int   tm_year;    /* Year - 1900 */
  int   tm_wday;    /* 0..6 */
  int   tm_yday;    /* 0..365 */
  int   tm_isdst;   /* 0..1 */
};

Return Values

asctime
A character string representation of the tm structure in local time.
ctime
A character string representation of the tm structure in local time.
localtime
A pointer to a tm structure with decoded values.
gmtime
A pointer to a tm structure with decoded values.
mktime
Seconds since January 1, 1970.

Example

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

void Display (struct tm * Time);

int main () {
  time_t LocalNow;
  time_t GMTNow;
  char * LocalNowString;
  char * GMTNowString;
  struct tm * LocalNowStructure;
  struct tm * GMTNowStructure;
  LocalNowString = (char *) malloc (27);
  GMTNowString = (char *) malloc (27);
  /* time function */
  GMTNow = time (NULL);
  LocalNow = time (NULL);
  printf ("The current time (time), in seconds since January 1, 1970, is %i.\n",
          LocalNow);
  /* ctime function */
  strcpy (LocalNowString, ctime (&LocalNow));
  LocalNowString [24] = 0;
  printf ("The current time (ctime) is %s.\n",
       LocalNowString);
  /* localtime function */       
  LocalNowStructure = localtime (&LocalNow);
  printf ("Local Time: \n");
  Display (LocalNowStructure);
  /* mktime function */
  LocalNow = mktime (LocalNowStructure);
  printf ("The current time (mktime) is %i.\n",
       LocalNow);
  /* asctime function */
  strcpy (LocalNowString, asctime (LocalNowStructure));
  LocalNowString [24] = 0;
  printf ("The current ASCII time (asctime) (Local) is %s.\n",
       LocalNowString);
  /* gmtime function */       
  GMTNowStructure = gmtime (&GMTNow);
  printf ("Greenwich Mean Time: \n");
  Display (GMTNowStructure);
  /* mktime function */
  GMTNow = mktime (GMTNowStructure);
  printf ("The current time (mktime) is %i.\n",
       GMTNow);
  /* asctime function */
  strcpy (GMTNowString, asctime (GMTNowStructure));
  GMTNowString [24] = 0;
  printf ("The current ASCII time (asctime) (GMT) is %s.\n",
       GMTNowString);
  return 0;
}

void Display (struct tm * Time) {
 if (Time == NULL) {
   printf ("NULL POINTER\n");
 } else {
   printf ("  The year is %i.\n", Time->tm_year);
   printf ("  The month is %i.\n", Time->tm_mon);
   printf ("  The day of the month is %i.\n", Time->tm_mday);
   printf ("  The day of the year is %i.\n", Time->tm_yday);
   printf ("  The day of the week is %i.\n", Time->tm_wday);
   printf ("  The hour is %i.\n", Time->tm_hour);
   printf ("  The minute is %i.\n", Time->tm_min);
   printf ("  The second is %i.\n", Time->tm_sec);
   printf ("  The daylight savings time flag is %i.\n", 
           Time->tm_isdst);
 }
}

Output

The current time (time), in seconds since January 1, 1970, is 1194675865.
The current time (ctime) is Sat Nov 10 00:24:25 2007.
Local Time: 
  The year is 107.
  The month is 10.
  The day of the month is 10.
  The day of the year is 313.
  The day of the week is 6.
  The hour is 0.
  The minute is 24.
  The second is 25.
  The daylight savings time flag is 0.
The current time (mktime) is 1194675865.
The current ASCII time (asctime) (Local) is Sat Nov 10 00:24:25 2007.
Greenwich Mean Time: 
  The year is 107.
  The month is 10.
  The day of the month is 10.
  The day of the year is 313.
  The day of the week is 6.
  The hour is 6.
  The minute is 24.
  The second is 25.
  The daylight savings time flag is 0.
The current time (mktime) is 1194697465.
The current ASCII time (asctime) (GMT) is Sat Nov 10 06:24:25 2007.

Valid HTML 4.01 Transitional

Valid CSS

Site Icon Copyright © 1999-, jhyoung, revised 00/00/0