[svlug] Time - yy mm dd hh mm ss

Seth David Schoen schoen at loyalty.org
Thu May 25 21:56:25 PDT 2000


Edward M. Goldberg writes:

> Time,
> 
> I needs a routine that converts time in "time = yyyy mm dd hh mm ss" format
> to UNIX standard time so that I can test for (timeA - TimeB) >  6Hr with no
> Y2K problems.
> 
> The old code that I have does not perform any corrections for Y2K leap seconds
> or Leap years ...
> 
> If some one on the list could point me to the correct routine it would help me.

What language do you want?

You can do it from the shell with

date -d "$YEAR-$MONTH-$DAY $HOUR:$MINUTE:$SECOND" +%s

if you can get YEAR, MONTH, DAY, HOUR, MINUTE, and SECOND into shell
variables.

You can do it in C with

#include <time.h>

time_t find_unix_time(char *t){

struct tm *foo;
time_t temp;

foo = (struct tm*)malloc(sizeof(struct tm));

sscanf(t, "%i %i %i %i %i %i", &(foo->tm_year), &(foo->tm_mon), &(foo->tm_mday), &(foo->tm_hour), &(foo->tm_min), &(foo->tm_sec));

/* Adjust for peculiarities of mktime(3) */

foo->tm_year -= 1900;
foo->tm_mon -= 1;

temp = mktime(foo);
free(foo);
return temp;

}

You'll have to consider Daylight Saving Time also, if you want to be
correct; see mktime(3).

I can also tell you how to do it in Python, which is easier than C.
But I bet one of the above versions will help.

-- 
Seth David Schoen <schoen at loyalty.org>  | And do not say, I will study when I
Temp.  http://www.loyalty.org/~schoen/  | have leisure; for perhaps you will
down:  http://www.loyalty.org/   (CAF)  | not have leisure.  -- Pirke Avot 2:5





More information about the svlug mailing list