Jump to content

Recommended Posts

Posted (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.

image.png.d60674de1bad66b2f551da70ef6afb64.png

image.png.b83743061337a93aa8786ecfc4552504.png

 

 

Edited by DGrigsbyII
Posted (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())
})

image.png.104fe9435cb7218f2095d95e9cc1064c.png

Regards,

Thorsten

Edited by Thorsten Vogt
Posted

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. 

Posted
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())
})

image.png.104fe9435cb7218f2095d95e9cc1064c.png

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?

Posted

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:

image.png.7de4ce494997a6cf23c8126afe60e7b0.png

Regards,

Thorsten

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...