Showing results for
Did you mean:

# subtracting a constant from a timestamp channel

Hey guys,

I need to subtract a constant from a timestamp channel with many datapoints in the format mm/dd/yyyy hh:nn:ss.ffff.  I'm not sure how DIADEM deals with absolute time if it is similar to excel or not.  The constant I need to subtract from the channel will also be an absolute time so i'll be converting the absolute timestamp into a relative time channel.  I am trying to do this using the calculator.  Thanks for any help.

Message 1 of 6 (1,426 Views)

## Re: subtracting a constant from a timestamp channel

Hello atlasengineer,

First a little information on absolute time data in DIAdem:

The starting point for time data in DIAdem is 01.01.0000 at 00:00:00. This time/date is the value '0'. The DIAdem time channels will be accurate until at least 12/31/2078, starting at 10/15/1582. This is due to changes made to the Gregorian calendar back in 1582 concerning leap years. If anyone has test data from before 1582 they will have to manually correct that before importing it into DIAdem.

The date (04/18/2002) at 1:00:00 PM (13:00:00 military time) would be the value 6.31862676E+10. This number represents the number of seconds that have passed since 01.01.0000 at 00:00:00.

To your specific question:

I personally am not a huge fan of the DIAdem calculator, it's not very fast and a little difficult to debug. The code below whould give you what you are looking for though:

```L1 = CNo("Time")
R1 = TTR("01/01/1900 00:00:00.0000", "mm/dd/yyyy hh:nn:ss.ffff")
Call ChnLinScale(L1, "/Time2", 1, -R1)```

The code above assumes you have data in a channel called "Time" in the format "mm/dd/yyyy hh:nn:ss.ffff" and want to subtract 1900 years from that channel.

You can do this in the calculator as well, using the TTR and RTT functions, but it's a bit of a pain to code ...

I hope this helps,

Otmar

Otmar D. Foehner
Message 2 of 6 (1,345 Views)

## Re: subtracting a constant from a timestamp channel

Hi Otmar,

Given your explanation, why is it that the following code subtracts a day rather than one second from all the time values in channel "Time"? If you subtract 1 from each time, aren't you subtracting 1 second?

<Create a time channel called "Time2", that is the same length as "Time", then run following code

i = 1

Do While i <= Data.Root.ActiveChannelGroup.Channels("Time").Size

ChV(i,"[]/Time2") = ChV(i,"[]/Time") - 1

i = i + 1

Loop

I tried something like this because I wanted to selectively offset individual time values based on other conditions. I found that the time arithmetic does not work like this. Can you explain why not? Is there a guide on time arithmetic somewhere? I couldn't find any helpful documentation, so had to use the approach of creating multiple temporary channels and using the ChnLinScale() function as you have described.

Cheers,

David.

Message 3 of 6 (1,054 Views)

## Re: subtracting a constant from a timestamp channel

Hi PorridgeMan,

I believe this has to do with the format of how the ChV function returns the value of the channel and the conversion that takes place when performing the mathmatical operation on the data.  I was able to have some luck with retrieving the the data from the channel in a different way and then performing the operation.  Try this bit of code out, the line I have commented out demonstrates the same functionality except it uses the TTR function to convert from a date/time format.

Dim i
i = 1

Do While i <= Data.Root.ChannelGroups("EXAMPLE").Channels("Time").Size

ChV(i,"[1]/Time2") = Data.Root.ChannelGroups("EXAMPLE").Channels("Time").Values(i) - 1
'ChV(i,"[1]/Time2") = Data.Root.ChannelGroups("EXAMPLE").Channels("Time").Values(i) - TTR("01/01/0000 00:00:01.0000", "mm/dd/yyyy hh:nn:ss.ffff")

i = i + 1
Loop

Justin D
Applications Engineer
National Instruments
http://www.ni.com/support/
Message 4 of 6 (1,027 Views)

## Re: subtracting a constant from a timestamp channel

Hi JD_war_eagle,

Strange, your code did not work for me - it still reduced the times by 1 day instead of 1 second. Here is the exact code that I used:

```Option Explicit  'Forces the explicit declaration of all the variables in a script.

Dim i
Dim OldTimeChn
Dim NewTimeChn1, NewTimeChn2

Set OldTimeChn = Data.Root.ActiveChannelGroup.Channels("Time")
Set NewTimeChn1 = Data.Root.ActiveChannelGroup.Channels.Add("Time2",DataTypeDate)
Call ChnPropSet(NewTimeChn1,"length",Data.Root.ActiveChannelGroup.Channels("Time").Size)
Set NewTimeChn2 = Data.Root.ActiveChannelGroup.Channels.Add("Time3",DataTypeDate)
Call ChnPropSet(NewTimeChn2,"length",Data.Root.ActiveChannelGroup.Channels("Time").Size)

i = 1

Do While i <= OldTimeChn.Size
ChV(i,"[]/Time2") = OldTimeChn.Values(i) - 1
ChV(i,"[]/Time3") = OldTimeChn.Values(i) - TTR("01/01/0000 00:00:01.0000", "mm/dd/yyyy hh:nn:ss.ffff")
i = i + 1
Loop```

If it works for you and you are using the same version of DIAdem as me (11.1.0f3806), then ChV() must be affected by some system setting. No matter, I've used ChnLinScale() function as previously suggested and it works.

Thanks,

PorridgeMan.

Message 5 of 6 (1,021 Views)

## Re: subtracting a constant from a timestamp channel

Hi PorridgeMan,

So I have been able to dig into this a bit more and have discovered the reason for this strange behavior we have been observing.  In your original code you were using the ChV function to return the values from the Time channels.  This function returns to values of the channels as variants rather than doubles.  The variant is treated differently in the mathematical operation and this is why we are indeed seeing a difference of 1 day rather than the expected difference of 1 second.  In order to access the data from the channels as doubles you should be using the CHD function instead.  By inserting this function into your original code you should see the behavior you expect.

```Dim i

i = 1
Do While i <= Data.Root.ActiveChannelGroup.Channels("Time").Size
CHD(i,"[]/Time2") = CHD(i,"[]/Time") - 1
i = i + 1
Loop```

Justin D
Applications Engineer
National Instruments
http://www.ni.com/support/
Message 6 of 6 (1,005 Views)