LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How does subtracting a double from a time work?

Solved!
Go to solution

Hello,

I'm a little confused by how doubles interact with time. I was just playing around and exploring and made the attached VI. I'm getting the current time then subtracting 0.5 as a double from it. I was expecting the result to be be the current time - 0.5 seconds or something similar, but it makes no sense to me. Can someone explain this?

0 Kudos
Message 1 of 7
(3,064 Views)

Hi Quark,

 

as I couldn't open your VI (stuck with LV2014 right now) I just made a small example of my own:

check.png

Timestamp minus 1.5 seconds…

 

Things might change once you set a unit for the DBL value: LabVIEW supports also time units (like s, min, h, d) and will calculate timestamps correctly, too!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 7
(3,056 Views)
Solution
Accepted by topic author TheStrangeQuark

@TheStrangeQuark wrote:

Hello,

I'm a little confused by how doubles interact with time. I was just playing around and exploring and made the attached VI. I'm getting the current time then subtracting 0.5 as a double from it. I was expecting the result to be be the current time - 0.5 seconds or something similar, but it makes no sense to me. Can someone explain this?


Actually, yes.  Your 0.5 double has a unit of years.  You can either change the units to seconds or just drop the unit part entirely.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 3 of 7
(3,049 Views)

@GerdW wrote:

Things might change once you set a unit for the DBL value: LabVIEW supports also time units (like s, min, h, d) and will calculate timestamps correctly, too!


That is what I just found in the VI.  The constant had a unit of "a", which apparently is year.  So 0.5 years (15.778938e6 seconds) was being subtracted.  I recommend just deleting the constant, create a new one from the palettes, and wire that up to the subtraction.  That new constant will not have units and make things work.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 7
(3,029 Views)

Good catch.

 

I could see something wasn't working right, but there was no indication that there were units hiding in there.

0 Kudos
Message 5 of 7
(3,023 Views)

Another fun feature of time math, you can set the double (with no units) to be displayed in different formats.  My favorite is the HH:MM:SS format.  This makes the code more readable as here I am subtracting 1 hour 2 minutes and 3 seconds.  If that was a normal double I would have to calculate the number of seconds in that time, but that isn't as easily documented as it is in this example.

 

Subtracting times.png

 

This format change can be done by right clicking the constant and choosing Display Format >> Relative Time.  This can also be done on double controls allowing for the user to enter time in this more intuitive format.

Message 6 of 7
(2,961 Views)

I wish I could say that I was observant enough to notice that the two times were six months different, but I actually found out what was wrong by messing with the code and getting broken wires where there weren't supposed to be any.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 7 of 7
(2,949 Views)