When you subtract two T format values (or 2 D format values), what you end up with is simply a number (not in T or D format).
When subtracting two T format values, since time is defined as number of tenths of a second since the “point of origin”, you can take this resulting number and divide by 10, taking the remainder as number of 10ths of a second, then divide by 60, taking the remainder as number of seconds, divide by 60 again, taking the remainder as number of minutes, divide by 24, taking the remainder as number of hours, etc…
This will give you a way to come up with the 1 day, 1 hour difference you are looking for.
Can`t disagree LESS with Brian!
Indeed, T2 - T1 gives us exactly 900000 (in tens of second) = 90000 SECONDS; and divided by 3600 = 25 hours: 1 day + 1 hour; so, the difference is 1 DAY and 1 HOUR.
However, I would not recommend to interpret the result as DATE (?)
T format: TIME1 - TIME2 = number of tenths of a second between them
D format: DATE1 - DATE2 = number of days between them.
In a weird sort of way, Fernando’s result even with the T format actually does indicate 1 day, 1 hour as well because the point of origin is 0000-01-02 00:00:00.
Date 0000-01-03 01:00:00 is exactly 1 day, 1 hour after that, but like you said, Nikolay, it is not recommended to use D or T formats to interpret the subtracted result. I would use a numeric variable to assign the difference to, then use arithmetic on the numeric difference to determine that instead.
The DISPLAY, which is from your code is correct (I added a day when entering the “to time”).
In the first WRITE statement, I have computed (long way) #TIMER which is the number of tenths of seconds for the interval. It matches the REDEFINE of #HOURS into #TENTHS which is binary. HOWEVER, on the second line of the first WRITE, note the 04 for DD.
I double checked what was going on by way of the MOVE EDITED to #ALPHA, which shows 04, not 02.
Am I missing something obvious? Do I need more caffeine? (probably)
Might it have something to do with the fact that the “date” inside #HOURS is out of range? seems a possibility. But, I would have expected an error message, not a strange value.
If your question is, why is DD showing 04 instead of two, when there is obviously a two day difference between your times, it is because a time variable without the date component initialized is 01/02/0000. The 04 you are seeing is because the date part of the T variable is 01/04/0000, or, two days more than the beginning of time as far as the T variable is concerned. Does this clear up your confusion or am I not understanding what has you confused?
I actually stumbled across the ability to extract the date portion from a T variable by using a D variable while working with a time capture system. A T variable was being stored with the date component when only the time component should have been.
One needs to be careful and understand that once you “extract” the date value, the T variable still contains the date portion. To truly separate the two, after extracting the date to a D variable, you can then subtract the D variable from the time variable to leave only the time component in the T variable. If you use the date and time variables in calculations without doing this, you will get unexpected results.
Typically, I redefine the T variables to P13 and do math that way. T variables do not work as expected when performing multiplication and division on them. I found that out the hard way when trying to calculate a total time when the values available were per piece times and quantities.
Despite being available for a decade (?), I know few programmers who are aware that D and T format fields can be used for durations.
When you add an integer to a date value or subtract from one, you get a new date, but when you subtract one date from another the result is a duration - a number of days. You need to know in your logic whether the D variable is a date or a duration. Using a date edit mask on a duration makes no sense, as in
MOVE EDITED #HOURS (EM=DD) TO #ALPHA
Subtracting one time value from another has a similar result - a number of days, hours, minutes, and seconds. The T portion of the result result will always be less than 24:00:00 because 24 hours is a day, and the number of days in the duration is placed in the date portion of the T variable. You need to know in your logic whether the T variable is a time or a duration.
I don’t use P13 redefinitions of T variables. I can add hours, minutes and seconds to a T variable via a literal or another T variable.
Note that Natural lets me WRITE ‘=’ #hours (em=yyyymmddhhiiss); and it makes a mess of it (again, because of what it considers a null date?). The difference between the two dates is clearly less than 7 months; yet Natural tells me 7 is the month (null month is 1) and 30 is the days. Totally useless information. Okay, I could subtract one from the 7 to get 6; but is even that correct?
In a “normal” date, YYYY MM DD are all meaningful. Clearly for a duration, this is not true. While “elapsed days” would be meaningful (although I might have to supply more than two D’s), MM is clearly not going to be meaningful. It would at best be based on the given dates, and not be applicable anywhere else. The same, of course, is true for YYYY (leap year or not?).
If I have a Duration, I should NOT be allowed to use Y’s or M’s in an edit mask. This should be a runtime error.
How about D’s? Perhaps I would be required to use DDD (or more D’s) so Natural knows I want what I would call a true days duration, not a day within month, which is what it does with “normal” dates.
Now how would Natural know the difference between a “true time” and a “duration time”? Perhaps when subtracting times, Natural could make the lower order character different. Say instead of a C (true time) an E (duration). Combine the different character with a requirement that DDD be used for durations, and I think this would work quite nicely.
Perhaps a change enhancement is in order. This would be for both output and MOVE EDITED.
I agree on there not being a need to use a P13 redefinition for addition or subtraction but performing multiplication or division on T variables has caused me problems in the past. You can end up with results that are not very accurate. Using the P13 redefinition in the calculation yields more accurate results.
I am curious to know the use case for multiplying or dividing a time field by a number. I guess you are treating the time field as a duration, 5 minutes and 30 seconds, in which case your example makes some sense. But to Natural, T"00:05:30.0" is not 5 minutes and 30 seconds, but 12:05AM on Jan 2, 0000. Half of 5 minutes and 30 seconds is 2 minutes and 15 seconds, but I have no idea what half of 12:05AM Jan 2, 0000 is, and I wouldn’t expect Natural to know either.
So while your math with the P13 field works for small time values, it breaks down when the result is more than one day.
I would reiterate, using a (D) or (T) field to represent a duration is mixing apples and cabbage. You should be careful, know what you are doing, and comment the code so whoever comes along behind you knows what you did.
Yes, it is using a T variable to hold a duration. This was utilized in a time tracking system I helped a company develop. An employee starts working on a job, process 1000 pieces, then finishes the job 2 hours later, you can calculate how much time was required per piece.
While the durations in this system are less than 1 day, I don’t see where there is a breakdown when the result is greater than 1 day. I can subtract 20130101 00:00:00 from 20140101 00:00:00, end up with a value that when represented by an edit mask looks like 00010101 00:00:00 but really contains the value 315360000. This is exactly 365 days, represented in tenths of a second. If I divide that by two, I end up with 00000702 12:00:00, or 157680000, which is exactly 182.5 days, represented in tenths of a second. Sure, I can’t use EM=YYYYMMDD to come up with anything that makes sense, but I can determine that the value of 157680000 is 182 days and 12 hours, should I need to. When talking about durations, the date edit masks don’t make sense, and I think that is what Steve was frustrated about but I don’t see any problems with using a T variable to store a duration.
I agree that you should be careful and know what you are doing. Someone did not understand what they were doing in this system and stored a current date value as part of a T variable when only the time portion was appropriate. You very quickly blow up a Natural program when you have 1000 pieces that had a duration record of more than 2014 years.
What approach would you take to capturing a start and end time, then determining a per piece cost from it?