Output a future Date and time

busarider29

Lifetime Supporting Member
Join Date
Aug 2013
Location
Midland, MI
Posts
398
Greetings experts,

Attempting to calculate a future date and time based on number of days, hours, and minutes "from now". As just an example, what is the date and time 22 days, 13 hours, and 17 minutes from now? I'm using TwinCAT (Codesys) and also have the OSCAT library, which has a handful of time and date related functions. I've gotten started on some coding for it already, but it's beginning to look like it's going to get more complicated than I first thought, unless there's an "easy" way and I'm just doing it the 'hard' way again. So before I perhaps start making it more difficult and messy than what it should be, I thought I'd ask has anyone done something similar, perhaps already have similar function they could share?

It's really late, so perhaps after some shut eye, it will come to me better.

Thanks.
 
Looking here

https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/2529415819.html&id=

It looks like DT type is seconds since midnight, 01/01/1970.

So, can you try just adding "seconds" to it? I.e for each day, 24 hrs x 60 min x 60 sec = add 86400, etc.

Might need to do a type conversion from DT to DWORD first, do the math, then convert back to get it in DT format.

I'm no TwinCAT expert though!


I'm not a TwinCAT expert either, but something like that is what I was thinking when I read your post. It's convenient that there's already a DT type that's seconds since a datum point do you don't have to make up your own. Can you grab the DT of your current time, convert your time to be added into seconds, and add them together in another DT type? Does TwinCAT have those functions built in?
 
look up FILETIME64_TO_SYSTEMTIME on beckoff info. Has sample of using getsystemtime function, which is a T_FILETIME64 (ULINT), and converts it to a time structure, with seperate words for Year, Month, DOW, Day, Hour, Minute, Second, Millisecond. Then you can add directly to those words instead of having to convert everything to seconds and back.
 
Here's a quick and dirty method that I tested:
1. Get current system time with FB_LocalSystemTime. Output is in TIMESTRUCT format.
2. Convert TIMESTRUCT to DT (seconds since 1/1/1970).
3. Add some number of seconds, in my example I did a week. Requires DT value to be converted to UDINT first and then back to DT.

As JBooth suggested you can probably create your own time structure with the offset date/time and add that to the DT, rather than converting everything to seconds.

beckhofftime.PNG
 
Last edited:
I'm not a TwinCAT expert either, but something like that is what I was thinking when I read your post. It's convenient that there's already a DT type that's seconds since a datum point do you don't have to make up your own. Can you grab the DT of your current time, convert your time to be added into seconds, and add them together in another DT type? Does TwinCAT have those functions built in?

Found this for the ADD operator on the Beckhoff Infosys site:

"Possible combinations for TIME data types: TIME+TIME = TIME, TOD+TIME = TOD, DT+TIME = DT"

Will need to account for different number of days in a particular month, and also leap years, which was mentioned. The OSCAT library has functions for both of those, so no problem there. However, I was hoping to avoid having to do all of that manually in code (call functions for determining number of days in the month, and if this year is a leap year), but perhaps there's no other way.
 
Here's a quick and dirty method that I tested:
1. Get current system time with FB_LocalSystemTime. Output is in TIMESTRUCT format.
2. Convert TIMESTRUCT to DT (seconds since 1/1/1970).
3. Add some number of seconds, in my example I did a week. Requires DT value to be converted to UDINT first and then back to DT.

As JBooth suggested you can probably create your own time structure with the offset date/time and add that to the DT, rather than converting everything to seconds.

Quick and dirty works fine for me. It works for my application! I knew there had to be an easier way than where I was taking it. Thanks!

I'm experimenting with JBooth's suggestion now. Yes, I can add to each individual word of the TIMESTRUCT but it appears that I still am required to calculate out a future date from that. In short, I'm thinking your method might still be the easier, more straight forward solution.
 

Similar Topics

Hey all, I am starting a new project for a customer and I am adding in solenoid valves. Once added, I noticed there was a lack of an Output...
Replies
0
Views
67
Hi, The hardware is: Click Plc model # CO-O1DD1-O HMI model # S3ML-R magnetic-inductive flow meter model # FMM100-1001. I will set the flow meter...
Replies
4
Views
133
Hi, I am using AB 5069-L306ERS2 CPU. My system should achieve SIL-2. I have safety door switches connected to AB 5069-IB8S module, and I want to...
Replies
1
Views
108
Hello! Hope you are great. I need to make a change in a PLC with ladder logic. I will mount a analog valve and I need to control it in ramp up...
Replies
7
Views
318
I have a 170AAO92100 card that I am interested in using as a 10 volt output. Is there setup that I have to do in order to change output or simply...
Replies
0
Views
86
Back
Top Bottom