How to find the difference between two timestamps

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
girishoak
Participant
Posts: 65
Joined: Wed Oct 29, 2003 3:54 am

How to find the difference between two timestamps

Post by girishoak »

Hi,

I have two timestamps in the format yyyy-mm-dd hh24:mi:ss. I want to find the exact difference (sec level). Is there any in-buit routine that can I give me the difference. I know how to get that using Basics.

Thanks,
Girish Oak
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

This is the code from my own set of tools for KBATimestampDeltaSeconds, which takes two timestamps as arguments and returns to you the difference in seconds:

Code: Select all

      StartDate = LEFT(Arg1,10)
      StartTime = RIGHT(Arg1,8)
      FinishDate = Left(Arg2,10)
      FinishTime = RIGHT(Arg2,8)

      InternalStartDate = ICONV(StartDate,"D-YMD[4,2,2]")
      InternalStartTime = ICONV(StartTime,"MTS")
      InternalFinishDate = ICONV(FinishDate,"D-YMD[4,2,2]")
      InternalFinishTime = ICONV(FinishTime,"MTS")

      NetDays = InternalFinishDate - InternalStartDate

      NetTime = 0
      Begin Case
         Case InternalFinishTime >= InternalStartTime
            NetTime = InternalFinishTime - InternalStartTime
         Case InternalFinishTime < InternalStartTime
            NetTime = ((24 * 60 * 60) - InternalStartTime) + InternalFinishTime
            NetDays -= 1                 
      End Case

* Number of days difference * 24 hours * 60 minutes * 60 seconds + number of seconds elapsed

      TotalElapsedSeconds = (NetDays * 24 * 60 * 60) + NetTime

      If TotalElapsedSeconds < 0 or NetDays < 0 Then TotalElapsedSeconds = 0

      Ans = TotalElapsedSeconds
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Here's a version KBATimestampDeltaDays that gives you the answer rounded to days:

Code: Select all

      StartDate = LEFT(Arg1,10)
      StartTime = RIGHT(Arg1,8)
      FinishDate = Left(Arg2,10)
      FinishTime = RIGHT(Arg2,8)

      InternalStartDate = ICONV(StartDate,"D-YMD[4,2,2]")
      InternalStartTime = ICONV(StartTime,"MTS")
      InternalFinishDate = ICONV(FinishDate,"D-YMD[4,2,2]")
      InternalFinishTime = ICONV(FinishTime,"MTS")

      NetDays = InternalFinishDate - InternalStartDate

      NetTime = 0
      Begin Case
         Case InternalFinishTime >= InternalStartTime
            NetTime = InternalFinishTime - InternalStartTime
         Case InternalFinishTime < InternalStartTime
            NetTime = ((24 * 60 * 60) - InternalStartTime) + InternalFinishTime
            NetDays -= 1                 
      End Case

* Number of days difference * 24 hours * 60 minutes * 60 seconds + number of seconds elapsed

      TotalElapsedSeconds = (NetDays * 24 * 60 * 60) + NetTime

      If TotalElapsedSeconds < 0 or NetDays < 0 Then TotalElapsedSeconds = 0

      Ans = OCONV(TotalElapsedSeconds / (60 * 60 * 24), "MD0")
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
girishoak
Participant
Posts: 65
Joined: Wed Oct 29, 2003 3:54 am

Post by girishoak »

Thanks for quick reply

Girish Oak
Nisusmage
Premium Member
Premium Member
Posts: 103
Joined: Mon May 07, 2007 1:57 am

Post by Nisusmage »

Why not use these?

MeasureTime YearsToSeconds
MeasureTime WeeksToSeconds

Look in the help file under "Measurement Transforms - Time"
~The simpliest solutions are always the best~
~Trick is to understand the complexity to implement simplicity~
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

You sure they existed five years ago? :wink:
-craig

"You can never have too many knives" -- Logan Nine Fingers
Nisusmage
Premium Member
Premium Member
Posts: 103
Joined: Mon May 07, 2007 1:57 am

Post by Nisusmage »

Haha.. point taken.

I never looked at the date.
But, hopefully it'll update the thread in case someone else come along.
~The simpliest solutions are always the best~
~Trick is to understand the complexity to implement simplicity~
Post Reply