Subtracting Date in a translation

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Subtracting Date in a translation

  • Creator
    Topic
  • #50924
    Lina Patel
    Participant

      What is the correct syntax for subtracting date to a previous date and changing time in a translation?

      For example:

      20090601240014

      will become

      20090531235914

      Thank You,

      Lina Patel

    Viewing 5 reply threads
    • Author
      Replies
      • #68022
        Jim Kosloskey
        Participant

          Linda,

          You will need Tcl to do Date Math.

          Check the clock Tcl command.

          email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

        • #68023
          Lina Patel
          Participant

            Can I call the tcl proc below from the translation date field to be formatted?

            $xlateInVals dt

            set dt [clock format [clock scan $dt] -format “%Y%m%d%H%M%S”]

            set xlateOutVals

              How can I subtract to the previous day’s date?

              Thanks,

              Lina

          • #68024
            Bob Richardson
            Participant

              Greetings,

              Just a quick note:  using “clock scan yesterday” gets you a date/time string of exactly the moment from now that would be yesterday.

              Then adjust the time in seconds as an addition or subtraction.  Beware of borderline cases like midnight.

              For example on “yesterday” (for me now here in the Midwest US):

              tcl>fmtclock [clock scan yesterday]

              Sun May 31 14:39:22 CDT 2009

              Enjoy.

              BobR

            • #68025
              Robert Milfajt
              Participant

                Linda,

                Just a few comments.

                When getting the date, you probably want something like (assuming the date is in the first argument of the Xlate

                Code:

                set dt [lindex $xlateInVals 0]

                Second, if the date is already formatted like 20090501140001, you probably want to do something like this (in pseudo code):

              • – Use clock scan to convert date1 to integer

                – Use clock scan to convert date2 to integer

                – Subtract the two integers

                – Convert that difference to what you need for your application, as the result value will be the difference in seconds, and 60 secs/min, 60 mins/hour, etc.  You might use clock format or math functions here.

              • Hope this helps,

                Robert Milfajt
                Northwestern Medicine
                Chicago, IL

          • #68026
            Charlie Bursell
            Participant

              The clock scan command expects a list of one or two elements.  The first element is the date and the second element is the time, if there. So an HL7 timestamp like 20090601164230  will not scan.  You have to make it scanable.

              Ther are a couple of different methods

              set sdate “[string range $dt 0 7] [string range $dt 8 end]”

              or

              regsub — {(d{9})(.*)} $dt {1 2} sdate

              Then:  clock scan $sdate

              As for math you can do all sorts of things like:

              clock scan “2 days ago”. “last week” “-2 days”, etc.

              For example using the above date:

              clock format [clock scan $sdate]

                  => Mon Jun 01 4:42:30 PM Central Daylight Time 2009

              clock format [clock scan “2 days ago” -base [clock scan $sdate]]

                  => Sat May 30 4:42:30 PM Central Daylight Time 2009

              Of course you can then use the -format flag to format it any way you like.

              Experiment!  You will find the clock command is very powerful.

              experiment

              You can use a basedate

            • #68027
              Lina Patel
              Participant

                Thank You all for the suggestions. I will make the modifications and update on progress.

                Thanks again,

                Lina

              • Viewing 5 reply threads
                • The forum ‘Cloverleaf’ is closed to new topics and replies.