Welcome to Synthiam!

Program robots using technologies created by industry experts. ARC is our free-to-use robot programming software that makes features like vision recognition, navigation, and artificial intelligence easy.

Get Started
Asked — Edited

Date And Time Question

Can anyone point me in the direction to find a way to tell how much time there is between two dates.

For example Date1 = 10/29/2016 10:30:54 PM and Date2 = 10/28/2016 7:00:01 AM

How many days, hours, minutes, and seconds before date 1.

AI Support Bot
Related Content
Synthiam
PRO
USA
#6  
@DJ,

That's a major refactoring!
I tried that before but does not work as the OP wants. does not handle days, months, years.

Code:


$fromDate = "11/17/1974 10:00:00 PM"
$toDate = "10/27/2016 4:29:00 PM"
$diff = CDateTime($toDate) - CDateTime($fromDate)
print($diff)


Check the output:
User-inserted image
PRO
Synthiam
#7  
Your code is very fun and impressive! You could save a bit of code by parsing the output of this...

Code:


$startDate = "10/29/2016 1:00:00 PM"
$endDate = "5/28/2014 2:02:02 PM"

$diff = CDateTime($startDate) - CDateTime($endDate)


The $diff in this case is 884.22:57:58. That's
- 884 days
- 22 hours
- 57 minutes
- 58 seconds

I just added a fmttimespan() to EZ-Script for the next release. It will allow formatting of a timespan (which is what a datetime subtraction/addition creates)
PRO
USA
#8  
@DJ,

yes it was more an exercise i can't do that in my day job:)

I like to test concepts:
Goto / Return (like a function call)
Working with an array to minimize variable usage
Reusing a (function call)

everything went well.

yes i could parse the big number (days), but i would need to calculate the leap years, to extract years, then how many leap years in between, so i decided to do it from scratch.
PRO
Synthiam
#9  
It's awesome:D

I have been fiddling with the idea of making user defined functions in ez-script. It would be a control where you can define the function and parameters.

My first thought was to make the function be ez-script. But lately i've been leaning toward the idea of having the function be c# to give additional functionality. That means you could create the c# code for the internal function and be
a) extra fast
b) have control over the ARC architecture just like a plugin
#10  
@DJ... Great idea.... User defined functions would be awesome....
PRO
USA
#11  
@DJ,

I'm glad you raised that topic.

Plugin:
the concept is neat, but as you know does not work on Mobile and Windows Universal Apps

user functions:
I would do it first in EZ-Script, as you know C# CodeDom/Generation is not allowed on IOS Apps.

that way you can use user functions everywhere.
PRO
Synthiam
#12  
I was thinking of the user function being similar to a plugin's version management and shareable in an online library.

if the function is a mere EZ-Script and defined on a per project basis, there would be no version control and outdated/bugger functions would be difficult to manage across projects.

I'll think a bit on what an EZ-Script user function with online version management simlar to a plugin would look like. hmmm...
#13  
While we're on the topic... (and assuming I got the gist of this function idea).... A function or library for serial would be great... I am just ok at serial stuff, but streamlining serial receive would be welcome...
PRO
USA
#14  
@Richard R,

Can you elaborate.
#15  
@PTP.... I would like to just read serial into a variable in a function call... avoiding GetByteAt, UARTAvailable, UARTRead, etc....

And maybe even a simple parser... $string=(left($string),10,5)
Something like this would set $string as the 10th character in and read for 5 characters after.... This way you can pick the data you want out of the incoming serial data
PRO
Synthiam
#16  
How would you know if any data had been transmitted, how to tell if a packet had been received without UartAvailable()?

Why are you ever GetByteAt() anyway? That's not a serial specific function - it's a function to get a byte(character) within a string
#17  
@DJ I was using GetByteAt when I was using my Roomba ...example code snippet...

Code:


$RX_DATA=0
uartWrite(0,0,142,22) # Volatge check
sleep(10)
uartWrite(0,0,142,21) # charge state
sleep(10)
uartWrite(0,0,142,34) # charge state
sleep(50)
$rx = UartAvailable(0, 0)


if ($rx=4)
$RX_DATA = UARTRead(0, 0, $rx)
$MSB=GetByteAt($RX_DATA,0)
$LSB=GetByteAt($RX_DATA,1)
$Charge=GetByteAt($RX_DATA,2)
$Charge_source=GetByteAt($RX_DATA,3)
$voltage=Round(($LSB+(256*$MSB))/1000,2)
#18  
@PTP, @DJ... So my idea was a function call something like this... Not knowing C# limits my understanding of what can be done here so apologies...

Code:


$data=GetSerialData()

What ever is in the buffer (if anything) would be read into $data... The complexity is removed... And It would be up to the user to pick out and/or convert the data he wants using maybe simple parser....
PRO
Synthiam
#19  
So what happens when you call "GetSerialData()" and you only get 1 byte, but your example code looks like it isexpecting 4 bytes.

This is the reason you have..

Code:


$rx = UartAvailable(0, 0)


if ($rx=4)
....


You can't trust that you ha
PRO
Synthiam
#20  
ve received all the data with a single command.

(see what i did there?)
#21  
@DJ... Ha, Ha... Touché..... Still function() commands to return data is a good idea...:)
PRO
Synthiam
#22  
No, it's not a good idea, as the example was presented:)

Additionally, using UARTRead() with binary/byte data is not recommended. UARTRead(), as the documentation dictates, is for ASCII String data.

Use UARTReadBinary(), which puts the data into an array.

To read all contents in one line, use this...

Code:


UARTReadBinary(0, UARTAvailable(0, 0), $array)


or for ASCII Read all, it's

Code:


$data = UARTReadAvailable( 0, 0 )


Here's the info from the EZ-Script manual in ARC:

[feature]
UARTRead( boardIndex, port, numBytes )
Receive ASCII bytes from the Peripheral UART Receive Buffer of the EZ-B v4. The UART receive buffers on the EZ-B v4 are 5,000 bytes.
To know how many bytes are available, use the UARTAvailable() function.
The Board Index is the EZ-B index starting at 0.
The port can be 0, 1 or 2.
Look near at the UART Port section lower in this document for the EZ-B Pin’s associated with each UART Port.
Example: UARTRead(0, 0, 10)
Example: UARTRead(0, 0, UARTAvailable(0, 1))

UARTReadBinary( boardIndex, port, numBytes, variable )
Receive binary bytes from the Peripheral UART Receive Buffer of the EZ-B v4 into the variable as an array. The UART receive buffers on the EZ-B v4 are 5,000 bytes.
To know how many bytes are available, use the UARTAvailable() function.
The Board Index is the EZ-B index starting at 0.
The port can be 0, 1 or 2.
Look near at the UART Port section lower in this document for the EZ-B Pin’s associated with each UART Port.
Example: UARTReadBinary(0, 0, 10, $variable)
Example: UARTReadBinary(0, 0, UARTAvailable(0, 1), $variable)

UARTReadAvailable( boardIndex, port )
Receive all available ASCII bytes from the Peripheral UART Receive Buffer of the EZ-B v4. The UART receive buffers on the EZ-B v4 are 5,000 bytes.
The Board Index is the EZ-B index starting at 0.
The port can be 0, 1 or 2.
Look near at the UART Port section lower in this document for the EZ-B Pin’s associated with each UART Port.
Example: UARTReadAvailable(0, 0)
Example: UARTReadAvailable(0, 0)
[/feature]
#23  
@DJ.... Got you. I do understand how it works, I just have to experiment further with UART... Learning to better parse the incoming information for the relevant data that I need is the next step....:) Thanks
PRO
Synthiam
#24  
Did you also notice that the command you were asking for exists? I pasted it in the above message from the script manual. It's uartreadavailable(). It's just that I wouldn't recommend using it for your application.

Actually, I wouldn't use it for any application other than debugging. Because you do need to know how much data is in the buffer. And you should only read as much data as your expected packet size is and leave the other packets in the buffer to process on the next loop.
#25  
@DJ... Yes, last night I went over and re-read all the UART documentation... I didn't pay much attention to what uartreadavailable() really does as I only used what I needed when I was messing with the UART and roomba.... I am going to go back and do some more experimenting... Thanks as always