Code:
#include <stdio.h>
#include <time.h>
static char *now();
static int csstrptime(char *, char *, struct tm * );
main()
{
char *format1="YYYYMMDDHHMMSS";
char *format2="YYYYMMDD";
struct tm tm1, tm2;
time_t secs1, secs2;
char *d1= now();
char *d2="20030118";
memset (&tm1, 0, sizeof tm1);
memset (&tm2, 0, sizeof tm2);
if (csstrptime(d1, format1, &tm1) != 0)
{
printf ("csstrptime failed. Date = %s format = %s", d1, format1 );
return (1);
}
if (csstrptime(d2, format2, &tm2) != 0)
{
printf ("csstrptime failed. Date = %s format = %s", d1, format2 );
return (1);
}
secs1 = mktime(&tm1);
secs2 = mktime(&tm2);
if (secs2 > secs1 )
printf ("Secs = %ld\n", secs2-secs1);
else
printf ("Secs = %ld\n", secs1-secs2);
}
/*****************************************************************************
**
** Name csstrptime()
**
** Purpose: This function converts the character string pointed to by buf to
** values, which are stored in the tm structure pointed to by tm,
** using the format specified by format.
**
** There is a system call strptime(), which works this emulates but
** there are known bugs in that function. For example, when parsing
** the format'YYYYMMDDHHMMSS' on Solaris it does not work correctly.
** The format must be 'YYYY MM DD HH MM SS'.
**
** In addition, strptime() does not appear to be avialable on NT.
** Syntax:
** static int csstrptime(const char *buf, const char *format, struct tm *tm);
**
** Parameters:
** buf IN Date to converted
**
** format IN Format of DATE (YYYYMMDDHHMMSS or YYYYMMDD)
**
** tm OUT time structure
** int tm_sec; seconds after the minute - [0, 61]
** int tm_min; minutes after the hour - [0, 59]
** int tm_hour; hour since midnight - [0, 23]
** int tm_mday; day of the month - [1, 31]
** int tm_mon; months since January - [0, 11]
** int tm_year; years since 1900
** int tm_wday; days since Sunday - [0, 6]
** int tm_yday; days since January 1 - [0, 365]
** int tm_isdst; daylight savings time flag.
**
** Returns: Returns zero for success one if there is an error.
**
*****************************************************************************/
static int
csstrptime(char *buf, char *format, struct tm *tm )
{
int year=0, month=0, day=0, hour=0, minute=0, second=0;
tm->tm_isdst = 0;
if (!strcmp(format, "YYYYMMDDHHMMSS") &&
(sscanf(buf, "%4d%2d%2d%2d%2d%2d",
&year, &month, &day, &hour, &minute, &second) == 6))
{
tm->tm_year = year - 1900;
tm->tm_mon = month - 1;
tm->tm_mday = day;
tm->tm_hour = hour;
tm->tm_min = minute;
tm->tm_sec = second;
}
else if (!strcmp(format, "YYYYMMDD") &&
(sscanf(buf, "%4d%2d%2d", &year, &month, &day) == 3))
{
tm->tm_year = year - 1900;
tm->tm_mon = month - 1;
tm->tm_mday = day;
/* Set the time to midnight since not time was given */
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
}
else
return (1);
return(0);
}
/*****************************************************************************
**
** Name now()
**
** Syntax: static char *now();
**
** Parameters: NONE
** Returns: Returns the the current date and time in YYYYMMDDHHMMSS format.
**
*****************************************************************************/
/*
** This function returns the current date and time in YYYYMMDDHHMMSS format
*/
static char *
now()
{
static char current_time[20];
time_t clock;
clock = time((time_t)0);
(void)strftime(current_time, sizeof(current_time), "%Y%m%d%H%M%S", localtime(&clock));
return (current_time);
}