The Question is:
I need to write a C routine that takes a date and converts it to CET and
returns the result (without changing the callers Time Zone as a result).
The Answer is :
Though time-pedants and specific applications requiring accuracy
of 0.9 seconds or better will correctly indicate that the values do
differ, this particular discussion will assume that UTC and GMT can
be used interchangeably. The term Zulu will here be used to reference
the particular time value at the Zero Meridian, or Paris Mean Time (PMT)
diminished by 9 minutes 21 seconds.
Using Compaq C functions, generate a binary time (time_t) in Zulu that
corresponds to the target time. Tools such as strptime() and mktime()
can be used here, but the specific routine(s) required depend in the
format of the input time.
Now generate a tm structure for Zulu using a call:
Set the TZ environment variable to the target timezone:
Generate the output time string for the target timezone using:
strftime(struct tm *InputZulu);
Remove the TZ environment variable:
$ type timezone.c
time_t t = time(NULL);
$ cc/decc/prefix=all timezone
$ link timezone
$ run timezone
Mon May 7 19:29:04 2001
Tue May 8 01:29:04 2001
Mon May 7 19:29:04 2001
Here is an introduction to some of the DECdts-provided utc_* calls:
char time1, time2;
t = time((time_t)0);
printf("The current time is %s\n",asctime(localtime(&t)));
utc_asclocaltime(time1, 50, &utc);
utc_ascgmtime(time2, 50, &utc);
printf("The local time is %s,\n or %s Zulu\n", time1, time2);
The above example uses DECnet-Plus DECdts components, or OpenVMS V7.3
and later (which incorporates the DECdts components), both of which
provide the utc routines. The example assumes the timezone will be
set by logical name or environment variable.
The following example shows how to convert between OpenVMS-format
binary timestamps (quadwords) and UTC-format (opaque) binary timestamps
using some of the DECdts calls, while specifying the Timezone Differential
Factor (TDF) for each. (The TDF value determines the offset from Zulu and
the local time.) The addition of utc_getusertime and/or utc_gettime and/or
utc_localzone calls will be of interest here.
struct utc ZuluTime;
// Get the current OpenVMS quadword time.
RetStat = sys$gettim(vmsTime);
// convert the OpenVMS local time to a Zulu, applying a TDF of
// -300 minutes, assuming the timezone is -5 hours from Zulu.
// convert Zulu back to an OpenVMS local time.
// A TDF of -300 is applied to the Zulu, since ZuluTime was
// constructed with that same value.
// This effectively gives us the same OpenVMS time value
// we started with.
You will also want to look at the ZIC and (to a lesser extent) the
LOCALE utilities provided by OpenVMS, and you will likely want to
use the epoch or (better) the documented text and opaque binary UTC
formats as your time formats. If you decide to directly load or
alter the tm struct, make certain you set tm_isdst field correctly.