      OPTIONS /EXTEND_SOURCE

!<title> calculate MJD using VMS time (up to 31-DEC-9999) </title>
! VMS quadtime has the algorithm for MJD aleready built-in:
! just divide it by 100ns units of a day.
! and even more easy: RTL LIB$DAY does it in one call.
!  command-line arguments:
!  1 string in VMS|ISO|EU|US format  (dd-MON-yyyy yyyy-mm-dd dd.mm.yyyy mm/dd/yy)
!  max date is 1-Dec-9999 !

          program mjdv

          integer*8 d
          integer*8 qt
          integer jd
          character*64 vmstime,isotime
          integer status
          INTEGER COMMAND_ARGUMENT_COUNT
          EXTERNAL COMMAND_ARGUMENT_COUNT
          INTEGER ISO_DATE_TO_VMS,EU_DATE_TO_VMS,US_DATE_TO_VMS
          EXTERNAL ISO_DATE_TO_VMS,EU_DATE_TO_VMS,US_DATE_TO_VMS

          call sys$gettim(qt)
          if (COMMAND_ARGUMENT_COUNT().gt.1) then
           call  GET_COMMAND_ARGUMENT(1,vmstime,LENGTH,STATUS)
           isotime=vmstime(1:LENGTH)
           call  GET_COMMAND_ARGUMENT(2,vmstime,LENGTH,STATUS)
           isotime=TRIM(isotime)//"-"//vmstime(1:LENGTH)
           call  GET_COMMAND_ARGUMENT(3,vmstime,LENGTH,STATUS)
           isotime=TRIM(isotime)//"-"//vmstime(1:LENGTH)
           call ISO_DATE_TO_VMS(isotime,vmstime,qt)
D          print *,' ISO date: ',TRIM(isotime),' VMS date:', TRIM(vmstime)
          else
           call  GET_COMMAND_ARGUMENT(1,vmstime,LENGTH,STATUS)
           if (status.eq.0) then
            isotime=vmstime(1:LENGTH)
! convert string to vmstime: the first success wins
! string should be parsed to differ ISO/EU/US ...
            status=lib$convert_date_string(vmstime(1:LENGTH),qt,,127);
D           print *,' vmstime after conversion1:',vmstime
            if (.not.status) status=ISO_DATE_TO_VMS(isotime,vmstime,qt) ! ISO format?
D           print *,' vmstime after conversion2:',vmstime
            if (.not.status) status=EU_DATE_TO_VMS(isotime,vmstime,qt) ! EU format?
D           print *,' vmstime after conversion3:',vmstime
            if (.not.status) status=US_DATE_TO_VMS(isotime,vmstime,qt) ! US format?
D           print *,' vmstime after conversion4:',vmstime
            if (.not.status) call exit(status)
           endif
          endif

!          d=10*1000000
!          d=d*60*60*24
!          d=864000000000
!          jd=INT(qt/d)

! VMS RTL has it:
!         The Day Number Returned as a Longword Integer routine returns the
!         number of days since the system zero date of November 17, 1858,
!         or the number of days from November 17, 1858, to a user-supplied
!         date.

          call lib$day(jd,qt)

          write(vmstime,*) jd
          call lib$set_symbol("MJDN",TRIM(ADJUSTL(vmstime)))
          print *,TRIM(ADJUSTL(vmstime))
          end
