Page 1 of 1

Date Add Complete Functionality

Posted: Mon Oct 20, 2008 9:33 am
by Nisusmage
Hello All, I copied this, viewtopic.php?p=304267#304267, to try and get community input to create a solid full featured DateAdd routine. Please assist.

I tried to get the Add years functionality but I'm failing dismally due to my shocking Basic knowledge. But I'm pressing on. Can anyone help here?

Code: Select all

*** CURRENTLY THE ROUTINE ONLY HANDLES TIMESTAMP OF FORMAT YYYY/MM/DD HH:MM:SS.sss

      InTimeStamp = trim(Arg1)
      NumToAdd = Abs(trim(Arg2))
      Type = UPCASE(trim(Arg3))

*-----------------------------------------------------------
*INITIALIZE VARIABLES
*-----------------------------------------------------------     

      Date = ''
      InDate = ''
      NewDate = ''
      Result = ''
      Ans = ''


*-----------------------------------------------------------
*Substring different literals of timestamp.
*-----------------------------------------------------------

      MilliSecPart = InTimeStamp[21,3]

      Date = InTimeStamp[1,10]
      Time = InTimeStamp[12,8]
      InDate = ICONV(Date,"D/YMD[4,2,2]")
      InTime = ICONV(Time,"MTS")


*-----------------------------------------------------------
*Determine type of manipulation
*-----------------------------------------------------------


      Begin Case

         Case Type='MSEC'

            GOTO AddMilliSec:

         Case Type='SS'

            ChgNum = NumToAdd
            GOTO TimeCalc:

         Case Type='MM'

            ChgNum = NumToAdd*60
            GOTO TimeCalc:

         Case Type='HH'

            ChgNum = NumToAdd*60*60
            GOTO TimeCalc:

         Case Type='DD'

            GOTO AddDay:

         Case 1
            Ans = "Error: Not a valid option for Arg3"
            Return(Ans)
      End Case


*-----------------------------------------------------------
* This Function Manipulates MilliSeconds.
*-----------------------------------------------------------

AddMilliSec:

      MillisecChk = Field((MilliSecPart + NumToAdd)/1000,".",1)
      AddCasMillSec = Field((MilliSecPart + NumToAdd)/1000,".",2)
      FmtMillSec = FMT(AddCasMillSec,"3'0'L")



*-----------------------------------------------------------
* Add Cascasde of Milliseconds
*-----------------------------------------------------------


      IF (MillisecChk <> 0)
      THEN

         NewTime = InTime + MillisecChk

         If (NewTime < 86399)
         Then
            NewDate = InDate
         End
         Else
            NewDate = InDate + 1
         End

         Ans = OCONV(NewDate,"D/YMD[4,2,2]"):' ':OCONV(NewTime,"MTS"):".":FmtMillSec

         RETURN(Ans)
      END

*-----------------------------------------------------------
* Simple Add of MilliSeconds
*-----------------------------------------------------------

      Else
         NewDate = InDate
         NewTime = InTime
         Ans = OCONV(NewDate,"D/YMD[4,2,2]"):' ':OCONV(NewTime,"MTS"):".":FmtMillSec
         Return(Ans)
      END

*-----------------------------------------------------------
* This Function Manipulates Hours, Minutes and Seconds
*-----------------------------------------------------------

TimeCalc:

      NewTime = InTime + ChgNum

      If (NewTime < 86399)
      Then
         NewDate = InDate
      End
      Else
         NewDate = InDate + 1
      End

      Ans = OCONV(NewDate,"D/YMD[4,2,2]"):' ':OCONV(NewTime,"MTS"):".":MilliSecPart

      Return(Ans)


*-----------------------------------------------------------
* This Function Adds Days.
*-----------------------------------------------------------

AddDay:

      NewDate = InDate + NumToAdd
      Ans = OCONV(NewDate,"D/YMD[4,2,2]"):' ':OCONV(InTime,"MTS"):".":MilliSecPart
      Return(Ans) 

Posted: Mon Oct 20, 2008 2:53 pm
by ray.wurlod
See also these routines for other possibilities.

Posted: Mon Oct 20, 2008 10:30 pm
by Nisusmage
Perfect, thank you kindly.