HP OpenVMS Systems

ask the wizard
Content starts here

Time and Timezone Conversions?

» close window

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:
 
    gmtime(time_t *InputBinaryTime);
 
  Set the TZ environment variable to the target timezone:
 
    putenv("TZ=CET");
 
  Generate the output time string for the target timezone using:
 
    strftime(struct tm *InputZulu);
 
  Remove the TZ environment variable:
 
    unsetenv("TZ");
 
  For example:
 
$ type timezone.c
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
main()
  {
  time_t t = time(NULL);
  printf("%s",ctime(&t));
  putenv("TZ=CET");
  printf("%s",ctime(&t));
  unsetenv("TZ");
  printf("%s",ctime(&t));
  return 1;
  }
$ 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:
 
#include <stdio.h>
#include <time.h>
#include <utc.h>
main ()
  {
  time_t t;
  utc_t utc;
  char time1[50], time2[50];
 
  t = time((time_t)0);
  utc_getusertime(&utc);
  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.
 
 
#include <starlet.h>
#include <stdlib.h>
#include <utc.h>
 
main()
  {
  struct utc ZuluTime;
  int vmsTime[2];
  int RetStat;
 
  //
  //  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.
  //
  if (utc_mkvmsanytime(&ZuluTime,vmsTime,-300))
    exit(1);
 
  //
  //  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.
  //
  if (utc_vmsanytime(vmsTime,&ZuluTime))
    exit(2);
 
  return 1;
  }
 
  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.
 
 

answer written or last revised on ( 8-MAY-2001 )

» close window