Time conversion

I ran into an issue with conversion and time/DT calculation.

The conversion TIME_TO_DT and DT_TO_TIME is not functionall. Also the DT operation are not good.

TOD and TIME operation and conversion look good.

I try runtime 6.6.2, 6.7.6, 6.8.1. In HW (X20CP0484-1) and in simulator is the problem. The same result in C50.

VAR
	Test_Time : ARRAY[0..3] OF TIME;
	Test_DT : ARRAY[0..3] OF DATE_AND_TIME;
	Test_TOD : ARRAY[0..3] OF TIME_OF_DAY;
END_VAR


    Test_Time[0] := Test_Time[1] - Test_Time[2];
	Test_Time[1] := DT_TO_TIME(Test_DT[1]);
	Test_Time[2] := DT_TO_TIME(Test_DT[2]);
	Test_Time[3] := INT_TO_TIME(((TIME_TO_INT(Test_Time[1]) / 1000) - (TIME_TO_INT(Test_Time[2]) / 1000)) * 1000);
	
	//Test_DT[1] := TIME_TO_DT(Test_Time[1]);
	//Test_DT[2] := TIME_TO_DT(Test_Time[2]);
	//Test_DT[1] := DINT_TO_DT(TIME_TO_DINT(Test_Time[1]));
	//Test_DT[2] := DINT_TO_DT(TIME_TO_DINT(Test_Time[2]));
	Test_DT[0] := TIME_TO_DT(Test_DT[1] - Test_DT[2]);
	Test_DT[3] := TIME_TO_DT(Test_Time[3]);
	
	Test_TOD[1] := TIME_TO_TOD(Test_Time[1]);
	Test_TOD[2] := TIME_TO_TOD(Test_Time[2]);
	Test_TOD[0] := TIME_TO_TOD(Test_TOD[1] - Test_TOD[2]);
	Test_TOD[3] := TIME_TO_TOD(Test_Time[3]);

The run of DT_TO_TIME - it’s not multiply by 1000

Run od TIME_TO_DT - it’s not divide by 1000.

Hi,

I’m not completely sure what are the start conditions of your code.
Are you setting the values “Test_DT[1]” and “Test_DT[2]” by watch windows (this is what I assume)? Because I don’t see any condition how values are set away from zero.

Anyway, I think the problem is a missing cast in the substraction line of Test_DT[1] - Test_DT[2]:

I haven’t tried more in that direction, but at least if using the right explicit casting, it looks like expected (at least I think so).

But to be honest even doing so makes not much sense to me, as a timespan (which is the content of TIME) has a relative meaning (milliseconds since start), while DATE_AND_TIME has an absolute one (seconds since 1.1.1970).

So casting between absolute and relative time Interpretations will anyhow lead to some problems depending on the distance from DT to 1.1.1970.
To calculate the timespan between two DT, use the function DiffDT() from AsTime instead.

Best regards!

Hey Jan,

I had encounter that problem in the past and it is pretty anoying. Problem is indeed based in the fact TIME_TO_DT and DT_TO_TIME is not fully doing its job. Base unit of the TIME is the millisecond but the base unit for the DT is the second. So when you do per exemple 5s in Time to DT, the conversion is reading 5000ms and putting 5000s in the DT. So you just need to always divide your TIME variables by 1000 before the conversion.

For the opposite interaction, you just have to multiply by 1000 to convert your seconds in millisecond before the conversion. But there is a catch, for some reason, the DT_TO_TIME only accept pure DT value and will refuse all operations. So you have to multiply after the conversion.

So for exemple:

Test_Time[0] := T#5s ;
Test_Time[1] := T#10s ;

Test_DT[0] := TIME_TO_DT((Test_Time[0] + Test_Time[0])/1000) ; // Output is 1970-01-01-00:00:15

And the opposite:

Test_DT[0] := DT#1970-01-01-00:00:15
Test_DT[1] := DT#1970-01-01-00:00:10

Test_Time[0] := (DT_TO_TIME(Test_DT[0]) - DT_TO_TIME(Test_DT[1]))*1000; // Output is T#5s

Hope this help.

I use this only to demonstrate the problem, because I can use here some another test variables. And the variables I set from watch to test it.

The main problem is, that TON use TIME and the TIME is not settable from mappView. So the sulution is use the DT (it have own widget with format HH:MM:SS and good settable visualization) and in timer use conversion to TIME. It’s maybe the question for developers to add it. Maybe someone with WDTC can do it for community?

@Christophe_C yes, it’s very annoying, especially when it used sometimes. Dt to dt structure and back do the job right, so why not DT_TO_TIME and back?

Ok, the diffDT is ok to use, but why I am possible to do it with - operator? I try use somwhere + operator and the compiller don’t let to do it? In math is just only sub. But it need to convert to int based var, do the math and convert it back :sleepy_face:

Hi,

Okay thanks, now I understood a bit better (but pretty sure still not completely).

Honestly spoken, I’m not sure what you mean exactly? Why - is possible but + not? I just know that it should be possible to use Tdiff := DT2 - DT1;with result value Tdiff directly is a TIME type, while the result of DiffDT(DT2,DT1) function is an UDINT.
I haven’t tried it, but I also would expect that something like DT2 := DT1 + Tdiff; should work also , as a Date + a timespan results in a new date… but I’m not sure what “DATE + DATE” should deliver as result, also a timespan? So or so, this is not possible as you said. I’m not sure but as I remember other programming languages handle it in the same way that a addition to a date needs a timespan as 2nd operator.

When calculating a timespan between 2 dates with a result of type TIME, we should always be aware that TIME is in milliseconds → that means every timespan that is greater then 2^32 ms will lead to a datatype overflow and therefore invalid values.
I think that’s the 2nd reason for DiffDT(), as it delivers a UDINT result in seconds, and DATE_AND_TIME also is interpreted as seconds since 1.1.1970, 00:00:00.

Coming back to the use case how I understood it:

you have a DATE_AND_TIME value, but just use HH:MM:SS in mappView as TIME is not natively supported, and you want to have a timespan represented by the range of 00:00:00 and 23:59:59 that can be used directly as a TON().PT input?

If yes, maybe this could be a possibility?
You could use DATE_AND_TIME in mappView, cast this DT to TOD and then cast it to TIME → DT_TO_TOD delivers just the time-portion of DT, independent of the date portion, and TOD_TO_TIME then casts to a timespan in TIME format, so directly usable with PT of TON for example.

Th" double cast" is neccessary because of the strict separation of IEC date and timespan data types, where TOD is a own datatype related to DT, and not to a timespan.

Maybe I got the use-case still wrong, in that case sorry about it.
But maybe the information in future is still helpful for someone else, so I worte it down here.

Best regards!

So DT - DT return TIME and need explicit conversion :exploding_head: And DT + TIME return TIME without conversion need :exploding_head:

The + operator is problem with TOD (Data type mismatch in parameter ‘2’ of ‘+’: Cannot convert TOD to TOD.) - it’s from the mistake in program and givven as example, that something can be disabled.

I found some magic words in help : “The bit pattern is adopted 1:1 for conversion to TIME.”. The TOD work in differ way, so my main question is WHY? :sweat_smile: It can be little bit confusing and in help I expected better documentation (some warning). So the “TOD_TO_TIME(DT_TO_TOD(some DT))” works and “TOD_TO_DT(TIME_TO_TOD(some time))” does not work.

obrazek

obrazek

And the main reason is, that DATE_AND_TIME have widget, which can be look something like this:

So there is for operator much better representation. TIME have only NumericInput/Output, but it’s just number in ms always. When I use DINT, I can add unit to it in millisec and in visu change the unit to sec, hours, days,… So there is not some support in mappView to set TIME variable with some good representation or I something overlook? :sweat_smile:

No :wink:

  • DT - DT = TIME, right → but doesn’t need explicit conversion
  • DT + TIME = DT → doesn’t need explicit conversion

Here’s a (incomplete?) example of things that are possible without conversion → the “logic” behind is interpretable as “thinking compared to mathematics operations with compatible units and/or unitless values”:

So, a DATE_AND_TIME (and things derived from DT like TOD = time of day) represents a concrete “real-time anchor” on a time-based axis. TIME represents a span of time without the concrete reference to a real-time anchor… so in my thinking, a DT is a “unit-based” value, and TIME is a “unitless” value… mathematically, I can operate with unit-based and unitless values, the result will then always be the unit-based value… and I can operate with 2 values of the same / compatible units and get as result a unitless / unit simplified value…
… I know, that still doesn’t explain that DT + DT isn’t allowed, and also some other things - but for me it’s a sufficiently good mental model to keep the coherence in mind :wink:

You’re right, there’s nothing as far as I know.
I’m unfortunately not a mappView knowledge holder, so I have no idea if anyone with expertise of WDTC could deliver some kind of such a widget.
But as the value range of TIME is something >24 days, I think such a widget should be (optionally?) limited to “HH:MM:SS” (23:59:59), which is the most common use-case I think?

Maybe someone out there has something in that direction, unfortunately I don’t know.

I haven’t read the help there to be honest :sweat_smile: The first thing works, yes, the 2nd doesn’t work I believe, right (as the “parent” of TOD is still a DT)… But at this point of the discussion I’ll check it, because it’s really interesting for me as my real programming experience is somehow outdated → I come from a time, where time, date, timespans and anything else were not existing, and I anyhow always needed own implementations / classes for such “special value interpretations” (yes, I’m that old :smiley: ).

Best regards!

OK, thanks. I will hope, that something will be added to improve user experience. And I hope, that this topic help also somebody, because it’s little bit hard to find this “bugs”.

I tried to request for licence of WDTC, but it’s little too much for this “small” things.

Hi @NovotnyJ
I can take a look to make this kind of widget, just to be sure you basically want a widget “TimeInput” where you can enter time like the “DataTimeInput” with the wheels selections?

Does you need like 3 wheels for HH, MM and SS or you need a 4th one with milliseconds?

Regards,
Florent

Hi @florent.boissadier , I soved it by DateTimeInput, but if you can create it for TIME type, that would be fantastic! I think the community would appreciate it, and at the very least, it will come in handy for my future projects.

Just look samething like DateTimeInput, but pair with TIME variable and add support for ms (maybe settable for show and step).

I must change the solution, because @florent.boissadier do a working widget with TIME! And it’s fantastic! :heart_eyes::heart_eyes:
Topic of widget: TimeInput widget for the TIME datatype

I was not there couple of days..and I see again how great our Community is. Thanks @NovotnyJ and @florent.boissadier you made my day :slight_smile: