How to convert local time to milliseconds? 
Author Message
 How to convert local time to milliseconds?

Hi,

I have a requirement to send the current local time to a server
in an 4 byte integer, expressed in milliseconds.....

I wrote the following function (which will run on SUNOS, HP-UX and AIX)
and am wondering if there is a more efficient way.  Any feedback would
be appreciated.

TIA,

Neil

/**************************************************************************
* Function:     milliseconds()
* Arguments:    <time_t> time - the current date and time calculated in
*                               seconds since 01/01/1970 - usually fetched
*                               via gettimeofday() or time().
*
* Returns:      Long integer representing the current local time
*               in milliseconds.
*
***************************************************************************/

long milliseconds( long time )
{
char    hour_format[10];
char    minute_format[10];
char    second_format[10];
char    hours[10];
char    minutes[10];
char    seconds[10];

/*
Construct strftime formats so SCCS doesn't accidentally interpret them.
*/

   sprintf(hour_format, "%s", "%H");
   sprintf(minute_format, "%s", "%M");
   sprintf(second_format, "%s", "%S");

   strftime(hours, sizeof(hours) - 1, hour_format, localtime(&time));
   strftime(minutes, sizeof(minutes) - 1, minute_format, localtime(&time));
   strftime(seconds, sizeof(seconds) - 1, second_format, localtime(&time));

   return(((atol(hours) * 3600) + (atol(minutes) * 60)
                    + atol(seconds)) * 1000);

Quote:
}



Tue, 26 Dec 2000 03:00:00 GMT  
 How to convert local time to milliseconds?
Hi,

I have a requirement to send the current local time to a server
in an 4 byte integer, expressed in milliseconds.....

I wrote the following function (which will run on SUNOS, HP-UX and AIX)
and am wondering if there is a more efficient way.  Any feedback would
be appreciated.

TIA,

Neil

/**************************************************************************
* Function:     milliseconds()
* Arguments:    <time_t> time - the current date and time calculated in
*                               seconds since 01/01/1970 - usually fetched
*                               via gettimeofday() or time().
*
* Returns:      Long integer representing the current local time
*               in milliseconds.
*
***************************************************************************/

long milliseconds( long time )
{
char    hour_format[10];
char    minute_format[10];
char    second_format[10];
char    hours[10];
char    minutes[10];
char    seconds[10];

/*
Construct strftime formats so SCCS doesn't accidentally interpret them.
*/

   sprintf(hour_format, "%s", "%H");
   sprintf(minute_format, "%s", "%M");
   sprintf(second_format, "%s", "%S");

   strftime(hours, sizeof(hours) - 1, hour_format, localtime(&time));
   strftime(minutes, sizeof(minutes) - 1, minute_format, localtime(&time));
   strftime(seconds, sizeof(seconds) - 1, second_format, localtime(&time));

   return(((atol(hours) * 3600) + (atol(minutes) * 60)
                    + atol(seconds)) * 1000);

Quote:
}



Tue, 26 Dec 2000 03:00:00 GMT  
 How to convert local time to milliseconds?

Quote:

> I have a requirement to send the current local time to a server
> in an 4 byte integer, expressed in milliseconds.....

> I wrote the following function (which will run on SUNOS, HP-UX and AIX)
> and am wondering if there is a more efficient way.

Much:

long milliseconds( long time )
{
   struct tm *p = localtime(&time);
   return ((p->tm_hour * 60 + p->tm_min) * 60L + tm_sec) * 1000L;

Quote:
}

-Larry Jones

I think grown-ups just ACT like they know what they're doing. -- Calvin



Wed, 27 Dec 2000 03:00:00 GMT  
 How to convert local time to milliseconds?


Quote:
>Hi,

>I have a requirement to send the current local time to a server
>in an 4 byte integer, expressed in milliseconds.....

>I wrote the following function (which will run on SUNOS, HP-UX and AIX)
>and am wondering if there is a more efficient way.  Any feedback would
>be appreciated.

    struct tm *tmptr = localtime(&timeval);

    return (tmptr->tm_hour*3600L + tmptr->tm_min*60 + tmptr->tm_sec) * 1000;

Noting that there is no guarantee that long is a 4 byte integer (but it
will be at least 32 bit wide).

time is the name of a standard library function. While using it as the name
of a variable at a restricted scope isn't strictly wrong, I wouldn't
recommend it.

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


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



Wed, 27 Dec 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to convert local time to gmt using a local variable time zone per process/thread

2. How to convert local time with specific time zone to UTC time in Win32 API

3. Converting UTC time to local time

4. Convert local time to gmt time without using TimeZone Env variable

5. How to convert GMT time to local time?

6. converting from Local Time to UTC time...

7. Q: How to convert GMT to local time?

8. converting UTC to local time

9. Converting GMT to local time

10. What is purpose of abstract TimeZone class and how do I convert a local time to another local time?

11. Time: UCT, local time, daylight - any standards?

12. Time: UCT, local time, daylight - any standards?

 

 
Powered by phpBB® Forum Software