DGrigsbyII Posted May 11, 2023 Posted May 11, 2023 (edited) I have a condition that has a duration property from child capsules. This property initially returns as a date time string from 1/1/1970 00:00. I want to be able to convert it to purely hh:mm:ss and be able to be larger than 24 hours if needed. What is the best approach to this? I saw some posts on doing a string replace function on the returned property but I couldn't get that to work This is what I'm using as an example property to test and play with. Edited May 11, 2023 by DGrigsbyII
Thorsten Vogt Posted May 12, 2023 Posted May 12, 2023 (edited) Hello, you could do this by creating a transform and calculating the values inside it. In my example I am using the duration() method to get the duration of the capsule. You might need to change this to get the value from your property. $condition.removeLongerThan(1mo).transform($capsule -> { //Calculate total hours $totalHours = $capsule.duration().convertUnits('h').setUnits('') $hoursFraction = $totalHours - $totalHours.floor() $hours = ($totalHours - $hoursFraction).floor() //Calculate minutes $minutes = $hoursFraction * 60 $minutesFraction = $minutes - $minutes.floor() //Calculate seconds $seconds = ($minutesFraction * 60).round(0) //Set property by buildung a string $capsule.setProperty('Hours', $hours.toString() + ":" + $minutes.floor().toString() + ":" + $seconds.toString()) }) Regards, Thorsten Edited May 12, 2023 by Thorsten Vogt
Synjen Marrocco Posted May 12, 2023 Posted May 12, 2023 This is probably uglier that Thorsten's approach, but you could also convert the duration property to a signal and do some regex replacing. Here is an example I used recently. First convert the property to a signal: $condition.toSignal('Duration Property Name') Then use the replace function to adjust the date as needed (you may need to move some thing around depending on the exact format): $timestamp_signal.replace('/(?<year>....)/(?<month>..)/(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' , '${hour}:${minute}:${sec}') //This example normalizes the duration if the original date always 1 day -- may need to dived here too $day = ($s.replace('/(?<year>....)/(?<month>..)/(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' , '${day}').toNumber() - 1).toString() $day + $s.replace('/(?<year>....)/(?<month>..)/(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' , ':${hour}:${minute}:${sec}') You would then set this new signal back as a property: //may need to adjust key for sample $newformat_signal.setProperty('New Duration', $duration_signal, $StartValue()) Again, this is a little ugly, and Thorsten's approach may work better depending on need, but wanted to give an example where you might get away without a transform.
DGrigsbyII Posted May 18, 2023 Author Posted May 18, 2023 On 5/12/2023 at 2:23 AM, Thorsten Vogt said: Hello, you could do this by creating a transform and calculating the values inside it. In my example I am using the duration() method to get the duration of the capsule. You might need to change this to get the value from your property. $condition.removeLongerThan(1mo).transform($capsule -> { //Calculate total hours $totalHours = $capsule.duration().convertUnits('h').setUnits('') $hoursFraction = $totalHours - $totalHours.floor() $hours = ($totalHours - $hoursFraction).floor() //Calculate minutes $minutes = $hoursFraction * 60 $minutesFraction = $minutes - $minutes.floor() //Calculate seconds $seconds = ($minutesFraction * 60).round(0) //Set property by buildung a string $capsule.setProperty('Hours', $hours.toString() + ":" + $minutes.floor().toString() + ":" + $seconds.toString()) }) Regards, Thorsten This works well and I like the approach except for dealing with minutes and seconds values less than 10. Do you have an approach for that?
Thorsten Vogt Posted May 19, 2023 Posted May 19, 2023 Hello, Seeq does not have a direct way to format strings. However you can use a regular expression to prefix values less then 10 with a leading "0". Just change the last line in the formula to this: $capsule.setProperty('Hours', $hours.toString().replace("/^(\\d{1})$/", "0$1") + ":" + $minutes.floor().toString().replace("/^(\\d{1})$/", "0$1") + ":" + $seconds.toString().replace("/^(\\d{1})$/", "0$1")) The RegEx in the replace function will search for one digit numbers and replace them with the original value prefixed with a leading 0. If the number contains more then one digit it will keep the original value: Regards, Thorsten
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now