From: mb301@hotmail.com Sent: Friday, April 12, 2002 11:05 AM To: Info-VAX@Mvb.Saic.Com Subject: Re: VMS boottime - how to calculate # of days running Compaq ref: http://askq.compaq.com/askopenvms/askpage.asp?ask=How+to+compute+differences+between+two+dates&qsource=0&origin=80&site_name=cmpq Or you can try this DCL procedure. $! *** start here *** $ first_date=f$getsyi("boottime") $ second_date=f$time() $! $ON_OUR_WAY: $! $! Convert string to dd-mmm-yyyy hh:mm:ss.cc $! $ date_one = F$CVTIME (first_date, "ABSOLUTE") $ hold_date = date_one $ date_two = F$CVTIME (second_date, "ABSOLUTE") $! $! Convert dates to form for comparison (yyyy-mmm-dd hh:mm:ss.cc) $! $ comp_date_one = F$CVTIME (date_one, "COMPARISON") $ comp_date_two = F$CVTIME (date_two, "COMPARISON") $! $! Get the larger date in DATE_TWO and COMP_DATE_TWO $! $ IF (comp_date_one .LES. comp_date_two) THEN GOTO NO_SWAP $ TEMP = date_one $ date_one = date_two $ date_two = TEMP $ hold_date = date_one $! $! Convert dates to form for comparison (yyyy-mmm-dd hh:mm:ss.cc) $! $ comp_date_one = F$CVTIME (date_one, "COMPARISON") $ comp_date_two = F$CVTIME (date_two, "COMPARISON") $! $! fall through to continue $! $NO_SWAP: $! $! check to see if second date is more than 10,000 days from the $! first date: $ largest = date_one + "+9999-23:59:59.99" $ test = f$cvtime(largest,"COMPARISON") $ if (test .GTS. comp_date_two) THEN GOTO GET_ON_WITH_IT $! $! the second date is greater than 10,000 days greater than the $! first date - put out an error message: $! $ WRITE SYS$OUTPUT - " The second date is more than 10,000 days greater than the first date" $ test = F$CVTIME(LARGEST,"ABSOLUTE") $ WRITE SYS$OUTPUT - " 10,000 days greater than ",date_one," is ",test $ GOTO ENDING_JOB $! $! $GET_ON_WITH_IT: $ flow = 0 $! $! Calculate answer to nearest day $! $ L = 10000 $ form = "!AS- 0:0:0.0" $ GOTO ZERO_IN $R1: $! $! set DAYS to the number of days difference $! $ days = t $! $! Calculate answer to nearest hour $! $ L = 24 $ form = "0- !AS:0:0.0" $ GOTO ZERO_IN $! $R2: $! $! set HOURS to the hours difference $! $ hours = t $! $! Calculate to nearest minute $! $ L = 60 $ form = "0- 0:!AS:0.0" $ GOTO ZERO_IN $! $R3: $! $! set MINUTES to the minutes of difference $! $ minutes = t $! $! Calculate to nearest second $! $ L = 60 $ form = "0- 0:0:!AS.0" $ GOTO ZERO_IN $! $R4: $! $! set SECONDS to the seconds of difference $! $ seconds = t $! $! Calculate to nearest hundredths of seconds $! $ L = 100 $ form = "0- 0:0:0.!AS" $ GOTO ZERO_IN $! $R5: $! $! set HUNDREDTHS to hundredths of seconds of difference $! $ hundredths = t $! $! get the day of the week for each date $! $ day_one = F$CVTIME (hold_date,,"WEEKDAY") $ day_two = F$CVTIME (date_two,,"WEEKDAY") $! $! Output the answer. $! $ WRITE SYS$OUTPUT " " $ WRITE SYS$OUTPUT " " $ WRITE SYS$OUTPUT " The difference between ", day_one,", ",hold_date $ WRITE SYS$OUTPUT " and ", day_two,", ", date_two $ WRITE SYS$OUTPUT F$FAO ( - " Is !ZL day!%S, !2ZL:!2ZL:!2ZL.!2ZL", - days, hours, minutes,seconds, hundredths) $ WRITE SYS$OUTPUT F$FAO ( - " (!ZL day!%S, !2ZL hours !2ZL minutes !2ZL.!2ZL seconds)", - days, hours, minutes, seconds, hundredths) $ WRITE SYS$OUTPUT " " $ WRITE SYS$OUTPUT " " $! $! END OF EXECUTION STREAM $! $ENDING_JOB: $ EXIT $! $! zero in procedure, the variable L contains the largest delta $! value allowed for this field. This routine does a binary chop $! from minimum value to the maximum value for each time variable $! to isolate the correct delta value. $! $ZERO_IN: $! $! Set initial lowest value parameter to minus 1 $! $ S = -1 $! $! compute temporary variable value that is halfway between $! the least value we know is greater than the value we want $! (initially this is the maximum value L) $! and the greatest value we know is less than the value we want $! (initially this is -1 - stored in S) $! $ new_t = (S + L) / 2 $! $! initial value new_t= (-1 + maximum value)/2 = half of maximum $! $! Top of loop $! $TRY_VALUE: $! $! set t equal to new_t $! $ t = new_t $! $! Get character string representation of T in T_STR variable $! $ t_str = "''t'" $! $! if this is a single digit preface it with a 0 to make it at $! least a 2 digit number (this is for accuracy in the 'hundredths $! of seconds' computation, i.e. 2 is not the same as 02 following $! a decimal point). $! $ if f$length(t_str) .eq. 1 then t_str="0''t_str'" $! $! Use the FORM specified in the main flow to put the value $! of T_STR into the appropriate field of the ADD_TO_DATE $! variable (FORM for days is "!AS- 0:0:0.0", for hours is $! "0- !AS:0:0.0", etc.) $! $ add_to_date = F$FAO (form, F$STRING (T_str)) $! $! Compute TEST_DATE to be the smaller date (DATE_ONE) plus $! the ADD_TO_DATE created above $! $ added_date = "date_one + ""+''add_to_date'""" $ test_date = F$CVTIME ('added_date, "COMPARISON") $! $ IF (test_date .LES. comp_date_two) THEN GOTO TRY_LARGER_VALUE $! $! fall through with computed test_date less than the larger date $! entered. $! $! if the test date is less than the second date, then we $! need a smaller delta value - so we want to lower new_t (and t) $! to a value half way between its current value and the largest number $! that we know to be smaller (initially this is the -1) $! which is stored in the variable S. $! $ new_t = (T + S) / 2 $! $! if the new value is equal to the delta value of this loop, we are $! done with this loop. $! $ IF new_t .EQ. t THEN GOTO VALUE_DONE $! $! recompute the smallest value that we know is larger than our $! date (variable L) to be our T value: $! $ L = T $! $! go to loop again: $! $ GOTO TRY_VALUE $! $TRY_LARGER_VALUE: $! $! if the computed date is less than the second date, then we $! need a greater delta value - so we want to raise new_t (and t) $! to a value half way between its current value and a least number $! that we know to be larger (initially this is the maximum delta $! value) which is stored in the variable L. $! $ new_t = (T + L) / 2 $! $! if the new_t value equals the t value, we have found our delta $! value $! $ IF new_t .EQ. t THEN GOTO VALUE_DONE $! $! reset the least number know to be larger than our desired value $! to the current T value: $! $ S = T $! $! go to top of loop: $! $ GOTO TRY_VALUE $! $VALUE_DONE: $! $! we have the delta value for our current testing loop - add it $! on to our lower date for comparison $! $ date_one = F$CVTIME (date_one + "+''F$FAO (Form,F$STRING (T))'", "ABSOLUTE") $! $ flow = flow + 1 $! $! return to main flow $! $ GOTO R'Flow $! *** end *** example: $ @uptime $ @a The difference between Tuesday, 5-MAR-2002 19:24:13.00 and Friday, 12-APR-2002 16:02:00.81 Is 37 days, 20:37:47.81 (37 days, 20 hours 37 minutes 47.81 seconds)