HP OpenVMS Systems

ask the wizard
Content starts here

Programming with dates and times?

» close window

The Question is:

 
I need the inverse function for LIB$DAY(). I have been trying various
combinations of LIB$CVT_TO_INTERNAL_TIME (and FROM) and trying to add a
delta time. I seem to be able to store DELTA_DAYS in internal format but
when I use LIB$ADD_TIMES to add the delt
a time to today's date to go back or forth a few days, internal day number
doesn't change.
 
If I had the inverse of LIB$DAY my problem would be trivial.
 
Thanks,
DGH
 
 


The Answer is :

 
  The terms date and time will here be used largely interchangeably.
 
  The LIB$CVT_FROM_INTERNAL_TIME call is designed to pick apart the
  binary format time, while LIB$CVT_TO_INTERNAL_TIME is designed
  to create the binary value.  LIB$DAY returns the number of days since
  the base date, or since the specified quadword-format time.
 
  The particular question -- the particular manipulation of time you
  wish to conduct -- is not entirely clear to the OpenVMS Wizard.
  Though you wish the inverse of lib$day -- to convert the days since
  base date into a quadword -- you then discuss moving a couple of
  days forward or back from the current date.
 
  The binary equivalent of an absolute date is always a positive value,
  while a binary delta time is always stored as a negative value.  (The
  various system service and RTL calls that manipulate the quadword date
  format regularly utilize this fact.)
 
  As a start, please see the chapter on time in the Programming Concepts
  Manual, and please see the attached example:
 
 
#include <descrip.h>
#include <lib$routines.h>
#include <ssdef.h>
#include <starlet.h>
#include <stdio.h>
#include <stsdef.h>
#define MAXASCTIMLEN 23
#define MAXVECTIMLEN 7
main()
    {
    int RetStat;
    $DESCRIPTOR( TimeTodayAscii, "TODAY");
    $DESCRIPTOR( TimeSourceAscii, "01-JAN-1995 00:00:01.00");
    $DESCRIPTOR( DeltaTimeSourceAscii, "1 00:00:00");
    char TimeSourceBinary[] =
      { 0x080, 0x0F5, 0x020, 0x000, 0x000, 0x000, 0x000, 0x000};
    struct dsc$descriptor FormattedTimeDesc;
    char FormattedTimeBuffer[MAXASCTIMLEN];
    unsigned char *TimeBinaryPtr;
    int TimeBinaryQW[2];
    int TodayBinaryQW[2];
    int DeltaTimeBinaryQW[2];
    int NewTimeBinaryQW[2];
    int DaysSinceBaseDate;
    unsigned short int VecTim[MAXVECTIMLEN];
    int i;
 
    /*
    //	example 1:
    //	Convert time from binary to ASCII.
    */
    FormattedTimeDesc.dsc$w_length = MAXASCTIMLEN;
    FormattedTimeDesc.dsc$b_dtype = DSC$K_DTYPE_T;
    FormattedTimeDesc.dsc$b_class = DSC$K_CLASS_S;
    FormattedTimeDesc.dsc$a_pointer = FormattedTimeBuffer;
 
    RetStat = sys$asctim( 0, &FormattedTimeDesc, TimeSourceBinary, 0 );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = lib$put_output( &FormattedTimeDesc );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
 
    /*
    //	example 2:
    //	Convert time from ASCII to binary and back to ASCII.
    */
    RetStat = sys$bintim( &TimeSourceAscii, TimeBinaryQW );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = sys$asctim( 0, &FormattedTimeDesc, TimeBinaryQW, 0 );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    TimeBinaryPtr = (unsigned char *) TimeBinaryQW;
    printf("0x0%2.2x 0x0%2.2x 0x0%2.2x 0x0%2.2x 0x0%2.2x 0x0%2.2x 0x0%2.2x 0x0%2.2x\n",
	TimeBinaryPtr[0], TimeBinaryPtr[1],
	TimeBinaryPtr[2], TimeBinaryPtr[3],
	TimeBinaryPtr[4], TimeBinaryPtr[5],
	TimeBinaryPtr[6], TimeBinaryPtr[7] );
    RetStat = lib$put_output( &FormattedTimeDesc );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
 
    /*
    //  Example 3:
    //  Display the number of days between the base date and the
    //  specified binary time...
    */
    RetStat = lib$day( &DaysSinceBaseDate, TimeBinaryQW, NULL );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    printf("Days Since Base Date for %*.*s: %d\n",
      TimeSourceAscii.dsc$w_length, TimeSourceAscii.dsc$w_length,
      TimeSourceAscii.dsc$a_pointer, DaysSinceBaseDate );
 
    /*
    //  Example 4:
    //  Display the number of days since the base date for today...
    //  Add some times together and then display the number of days...
    //  Subtract some times and then display the number of days...
    */
    RetStat = sys$gettim( TodayBinaryQW );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = lib$day( &DaysSinceBaseDate, TodayBinaryQW, NULL );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    printf("Days Since Base Date Today: %d\n", DaysSinceBaseDate );
    RetStat = sys$bintim( &DeltaTimeSourceAscii, DeltaTimeBinaryQW );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = lib$add_times( TodayBinaryQW, DeltaTimeBinaryQW,
      NewTimeBinaryQW );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = lib$day( &DaysSinceBaseDate, NewTimeBinaryQW, NULL );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    printf("Days Since Base Date Tomorrow: %d\n", DaysSinceBaseDate );
    RetStat = lib$sub_times( TodayBinaryQW, DeltaTimeBinaryQW,
      NewTimeBinaryQW );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = lib$day( &DaysSinceBaseDate, NewTimeBinaryQW, NULL );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    printf("Days Since Base Date Yesterday: %d\n", DaysSinceBaseDate );
 
    /*
    //  Example 5:
    //  Convert from a quadword into a time vector...
    //  Then reconstitute the quadword from the vector...
    */
    RetStat = sys$numtim( VecTim, TimeBinaryQW );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    for (i = 0; i < MAXVECTIMLEN; i++ )
      printf("VecTim[%d] = %d\n", i, VecTim[i] );
    RetStat = lib$cvt_vectim( VecTim, NewTimeBinaryQW );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = sys$asctim( 0, &FormattedTimeDesc, NewTimeBinaryQW, 0 );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
    RetStat = lib$put_output( &FormattedTimeDesc );
    if (!$VMS_STATUS_SUCCESS( RetStat ))
      return RetStat;
 
    return SS$_NORMAL;
    }

answer written or last revised on ( 7-SEP-1999 )

» close window