How to convert an arbitrary time to UTC?

How to convert an arbitrary time to UTC?

Post by Guenther Gra » Sat, 04 Apr 1998 04:00:00



Hi,

I have an arbitrary date/time within an arbitrary timezone
and I want to convert this to UTC (or localtime). How do I do this?
I know about getdate(3C), but this doesn't work, if
the TZ variable is not the same as the one in the date/time
I provide. This is on Solaris 2.5.1/2.6.

Quote:> echo $TZ
MET
> cat test.c

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

int main(void) {
  struct tm *timePointer;
  struct tm time1, time2;

  putenv("DATEMSK=/tmp/getdatemask");

  timePointer = getdate("24 9 1968 12:34 CET");
  if (NULL == timePointer) {
    printf("Oops, getdate test error <%d>\n", getdate_err);
    return 1;
  }
  return 0;

Quote:}
> cat /tmp/getdatemask
%d %m %Y %H:%M %Z
> cc time.c ; ./a.out

Oops, getdate test error <7>
Exit 1

Change the timezone CET in the program to MET and the program
works just fine.

I know that I could change TZ before the invocation of getdate(),
but I need this in a multithreaded program, and thus I'd have to
synchronize access to all time/date-processing functions, which is
a lot of work.

  Guenther

P.S.: I'd be happy, if I just could find out, what the time difference
between any given time in a timezone and UTC was.

 
 
 

How to convert an arbitrary time to UTC?

Post by John Bens » Tue, 07 Apr 1998 04:00:00



>P.S.: I'd be happy, if I just could find out, what the time difference
>between any given time in a timezone and UTC was.

    Use mktime(), and ask it for the time in seconds for Janyary 2nd,
1970 at 0:00 local.  Than subtract 86400.  You can't use January 1st,
because east of Grenwich, it's an error (time_t being unsigned).

    If you're worried about screwing your TZ environment, fork()
and change it in the child.



 
 
 

1. HDI: call an arbitrary function with an arbitrary number of arguments (and possibly arbitrary types)

If only this were lisp where the world is simple...  I'm not too happy
about http://www.eskimo.com/~scs/C-faq/q15.12.html , is there a
solution for Linux/GCC?  How about C++?

I figured out how to call an arbitrary function (here "ioctl") with
known arguments;

      int fd = open("/dev/cdrom", O_RDONLY);
      void *handle = dlopen("/lib/libc.so", RTLD_LAZY);
      int (*function)() = dlsym(handle, "ioctl");
      (*function)(fd, CDROMCLOSETRAY);  // whee
      dlclose(handle);

For a pseudo-arbitrary number of arguments, I could have a switch
statement that handles the cases for 0, 1, 2, 3, x arguments but how
lame would that be?

/me suddenly obsessed with partially implementing WinNT's
    "rundll32.exe" in Linux.

-N.
--
http://www.cs.mcgill.ca/~navindra/editors/

2. To all linux user......

3. convert UT time in local time / local time in UT time

4. Help on XFree386

5. date reset as UTC from time to time RH6.0

6. this URL crashes XF86

7. Default UTC time vs local time in kernel

8. Need immediate help with Awk! Thanks.

9. timezone programming question: converting UTC to localtime

10. access.log SQUID PROXY UTC seconds converting

11. UTC/local time question

12. rdate gives me UTC, I want local time

13. UTC Time