convert time to UTC format 
Author Message
 convert time to UTC format

Hi,

Does anyone know of C time-functions that can be used to convert
time into it's UTC format.

The output, for example, should look like this :
1998-04-16-15:29:07.736

I've tried using a combination of the available standard time functions, but
have not been able to achieve the desired result. Please help!

Thanks in advance.

SR



Sun, 14 Jan 2001 03:00:00 GMT  
 convert time to UTC format
From what you've written, I'm assuming you have a time_t value retrieved
using time(), and you want to format it. For that you have to convert it
into a usable form with gmtime(), and use strftime() to format it.
Unfortunately, the type struct tm doesn't have a milliseconds member.

Here's some code you can try:

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

#define FTIME_BUFFER 256

int main(void)
{
     char acBuffer[FTIME_BUFFER];
     time_t ttCurrent;
     struct tm *ptm;

     time(&ttCurrent);
     ptm = gmtime(&ttCurrent);

     if (ptm != NULL)
     {
          strftime(acBuffer, FTIME_BUFFER, "%Y-%m-%d-%H:%M:%S", ptm);
          printf("UTC: %s\n", acBuffer);
     }

     return 0;

Quote:
}

Russell


Savitha Rajiv wrote ...

Quote:
>Does anyone know of C time-functions that can be used to convert
>time into it's UTC format.

>The output, for example, should look like this :
>1998-04-16-15:29:07.736

...


Sun, 14 Jan 2001 03:00:00 GMT  
 convert time to UTC format
I would imagine that you are interested in getting the current time. For this,
you could use gettimeofday(). There is also ftime() which is obsolete.
Quote:

> Hi,

> Does anyone know of C time-functions that can be used to convert
> time into it's UTC format.

> The output, for example, should look like this :
> 1998-04-16-15:29:07.736

> I've tried using a combination of the available standard time functions, but
> have not been able to achieve the desired result. Please help!

> Thanks in advance.

> SR



Sun, 14 Jan 2001 03:00:00 GMT  
 convert time to UTC format

Quote:

> The output, for example, should look like this :
> 1998-04-16-15:29:07.736

I don't know a portable way to get milliseconds, but the rest is easy:

$ cat>demo.c
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
    time_t time_t=time(NULL);
    struct tm *ptm=localtime(&time_t);
    char buf[20];
    (void) strftime(buf, sizeof(buf), "%Y-%m-%d-%T", ptm);
    (void) puts(buf);

Quote:
}

^D
$ cc demo.c
$ a.out
1998-07-29-16:06:28
$

--
Christopher Westbury, Midtown Associates, 15 Fallon Place, Cambridge, MA 02138



Sun, 14 Jan 2001 03:00:00 GMT  
 convert time to UTC format


Quote:
>I would imagine that you are interested in getting the current time. For this,
>you could use gettimeofday(). There is also ftime() which is obsolete.

Note that neither gettimeofday() not ftime() are defined by the C language.

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


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



Mon, 15 Jan 2001 03:00:00 GMT  
 convert time to UTC format


Quote:


>> The output, for example, should look like this :
>> 1998-04-16-15:29:07.736

>I don't know a portable way to get milliseconds, but the rest is easy:

>$ cat>demo.c
>#include <stdio.h>
>#include <time.h>
>int main(int argc, char *argv[])
>{
>    time_t time_t=time(NULL);
>    struct tm *ptm=localtime(&time_t);

The requirement was UTC so use gmtime() instead of localtime() here.
Also remember that, unlike localtime(), gmtime() is allowed to fail in which
case it returns a null pointer (i.e. when the implementation doesn't know
how to conver to UTC).

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


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



Mon, 15 Jan 2001 03:00:00 GMT  
 convert time to UTC format

Quote:



> > Hi,

> > Does anyone know of C time-functions that can be used to convert
> > time into it's UTC format.

> > The output, for example, should look like this :
> > 1998-04-16-15:29:07.736

> The milliseconds are difficult in standard C although several C
> implementations provide them (in one form or another).

Most implementation attempt to supply subsecond accuracy by way of the
standard C function "clock()". The returned value can be converted
to seconds by using the "CLOCKS_PER_SEC" macro. But this macro is
deceptive, because it often suggests a higher accuracy than really
is the case. The real accuracy of "clock()" is implementation defined
and might be considerably less than what "CLOCKS_PER_SEC" offers.

For instance with Microsoft Visual-C you get a CLOCKS_PER_SEC value
of 1000, but the timer used is updated inly 18.2 timer per
second. Borland-C (3.0) is more honest: it has a CLOCKS_PER_SEC
value of 18.2

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 16 Jan 2001 03:00:00 GMT  
 convert time to UTC format
On Wed, 29 Jul 1998 11:05:08 -0400, "Savitha Rajiv"

Quote:

>Does anyone know of C time-functions that can be used to convert
>time into it's UTC format.

>The output, for example, should look like this :
>1998-04-16-15:29:07.736

I think that should be 1998-04-16T15:29:07.736 ie, it should be a
T separating the time from the date.
--
Roger


Wed, 17 Jan 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

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

2. Converting UTC time to local time

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

4. converting UTC to local time

5. UTC time from time server

6. function to convert time from ascii to long format

7. Converting time from string format

8. Converting to a Readable time format

9. CTime: +1 hour at utc-format!

10. Converting a struct tm in UTC to a time_t

11. Program to Convert UTC to PST or Other

12. Converting a struct tm in UTC to a time_t

 

 
Powered by phpBB® Forum Software