## Time Conversion

### Time Conversion

Anyone know how I can write a program to receive a time value such as
2309942 and convert that to a normal time value.  I know it has to be
calculated from January 1, 1970 but, I'm not sure how to do this.

I'm pulling the value from snmp for the system uptime.  So I need to be
able to convert this number to a normal time value.

Any help would be appreciated.

Thank you.

### Time Conversion

Some clarification would be nice regarding what a "normal" time value
is.

I assume that this is the number of seconds since 1. Jan, 1970.

You should be able to just take num % 60 for the number of seconds, num
/ 60 % 60 for the number of minutes, etc, for all the components of
"normal" time.

-R

> Anyone know how I can write a program to receive a time value such as
> 2309942 and convert that to a normal time value.  I know it has to be
> calculated from January 1, 1970 but, I'm not sure how to do this.

> I'm pulling the value from snmp for the system uptime.  So I need to be
> able to convert this number to a normal time value.

> Any help would be appreciated.

> Thank you.

--
-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^
Ryan Hankins
Information Services
Silicon Graphics Inc., Eagan, Minnesota
-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^

### Time Conversion

Hi,
There is a function called ctime you can use to get an ASCII string. Here
the commands you need:

include <time.h>
time_t time(time_t *tloc);
char *ctime(const time_t *clock);

If tloc is not NULL the time function stores the time in *tloc. The C
library function ctime converts a time in the time_t format to an
ASCII string.

I've actually never tried it out but according to one of my books it
works.

Joachim

> Anyone know how I can write a program to receive a time value such as
> 2309942 and convert that to a normal time value.  I know it has to be
> calculated from January 1, 1970 but, I'm not sure how to do this.

> I'm pulling the value from snmp for the system uptime.  So I need to be
> able to convert this number to a normal time value.

---------------------------------------------------------------------------

---------------------------------------------------------------------------

### Time Conversion

Try:
Assuming 2309942 is GMT time and you want it printed in LOCAL
timezone:

time_t my_time = 2309942;
printf("%s", asctime(localtime(&my_time)));

> > Anyone know how I can write a program to receive a time value such as
> > 2309942 and convert that to a normal time value.  I know it has to be
> > calculated from January 1, 1970 but, I'm not sure how to do this.

> > I'm pulling the value from snmp for the system uptime.  So I need to be
> > able to convert this number to a normal time value.

> > Any help would be appreciated.

> > Thank you.

> --
> -^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^
> Ryan Hankins
> Information Services
> Silicon Graphics Inc., Eagan, Minnesota
> -^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^

### Time Conversion

The following is an HPUX command-line routine to convert seconds into a time
string; it contains an
internal function which just might be what you're looking for...

-RAM

/*****************************************************************************/

/* BEGIN FILE  timeC.c                                                       */

/*****************************************************************************/

/* PURPOSE                                                                   */

/*    This is a utility program which converts a 9-digit number (seconds)    */

/*    into a time-date-group string.                                         */

/*                                                                           */

/*****************************************************************************/

/* DESIGN NOTES                                                              */

/*                                                                           */

/* The UNIX system will most likely use a standard base time.               */
/* For example, the SUN uses 01-01-1970~00:00:00 - but other systems may
vary.    */
/*                                                                           */

/* This program is set up as two separate procedures so that they will be  */
/* available for other routines to use the specific functionality.           */

/* The 'main' is the normal command line interface.                          */

/*                                                                           */

/* Differences of less than 1 minute (60 seconds) will be truncated; ie,     */

/*       2678400 through 2678459 ==>
02-01-1980~00:00:00                             */
/*****************************************************************************/

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

#define TIME struct tm

static char  TZ[11] = "TZ=GMT0GST\0";
static char ETZ[11] = "TZ=EDT5EST\0";

char *CHAR_time(seconds)
long seconds;
{
time_t base_secs = 0; /* Adjust if base time is NOT 01-01-1970~00:00:00 */
static char   out_buffer[24]; /* Adjust if you change the strftime call
Format string */
TIME   time;
long   secs;
secs = seconds + base_secs;  /* Adjust 0 time to UNIX base time */
time = *localtime(&secs);      /* We want LOCALTIME */
strftime(out_buffer, 21, "%m-%d-%y ~%02H:%M:%S  ", &time);  /* Format the
24-hour Time  */
return(out_buffer);

Quote:}

main(argc, argv)
int argc;
char *argv[];
{
long   seconds;
char  *cp;
extern long timezone;
extern char *tzname[2];
if(argc < 2)
{
printf("\nUsage: %s seconds\n", argv[0]);
exit(1);
}
/* Before we convert the input argument to seconds,  */
/* make sure it is at least numeric                                     */
cp = argv[1];
while(*cp != '\0')
{
if(!isdigit(*cp))
{
printf("%s: Input argument (%s) must be numeric\n",argv[0], argv[1]);
exit(1);
}
cp++;
}
seconds = atol(argv[1]);
tzset();
if (seconds < timezone)
{
printf
("%s: Input argument (%d) must exceed GMT offset(%d=%dhrs) of %s\n",
argv[0], seconds, timezone, timezone/3600, tzname[1]);
exit(1);
}
printf("%s\n",CHAR_time(seconds));  /* Call the Function and Print the
result */
Quote:}

/*****************************************************************************/

/* END FILE  timeC.c                                                         */

/*****************************************************************************/

> Try:
>         Assuming 2309942 is GMT time and you want it printed in LOCAL
>         timezone:

>         time_t my_time = 2309942;
>         printf("%s", asctime(localtime(&my_time)));

> > > Anyone know how I can write a program to receive a time value such as
> > > 2309942 and convert that to a normal time value.  I know it has to be
> > > calculated from January 1, 1970 but, I'm not sure how to do this.

> > > I'm pulling the value from snmp for the system uptime.  So I need to be
> > > able to convert this number to a normal time value.

> > > Any help would be appreciated.

> > > Thank you.

> > --
> > -^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^
> > Ryan Hankins
> > Information Services
> > Silicon Graphics Inc., Eagan, Minnesota
> > -^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^

--
+----------------------------------------------------------------------+
| Robert A. Matern -- Software Engineer -- URL: http://www.CSA-USA.com |
| MAIL: Control Systems Analysis, 3848 Main Road, Tiverton, RI 02878 - |
| E-MAIL: -- at CSA's Office --OR-- at HOME via Compuserve. -- :E-MAIL |

| ------- VOICE#: (401) 624-3300 -------- FAX#: (401) 624-2700 ------- |
+----------------------------------------------------------------------+

I'm writing a bash script and I need to convert to and from unix time.
Does anyone know of utils that will work under linux that will do this?

tia

Andrew

Sent via Deja.com http://www.deja.com/