2

I have a shapefile line layer with an attribute for time in float format.

This is what 23.21 and 55 seconds looks like:

232155.17

This is 03.27 and 01 seconds:

32701.64 

I would like to convert this to a proper time format in Field Calculator.

4
  • What data type for the new field?
    – Bera
    Commented Jun 3, 2021 at 13:03
  • I think Time would be best? Commented Jun 3, 2021 at 13:43
  • Lines Shapefile Commented Jun 3, 2021 at 13:46
  • 2
    As far as I know there is no time data type in shapefiles. Maybe text/string will be best
    – Bera
    Commented Jun 3, 2021 at 13:52

1 Answer 1

6

This should do the trick:

to_time( to_string( "<float_time_field>" ), 'hmmss.z' )

Since Shapefiles do not support time data types (there is date, but that cannot work without a date...), an easy fix for the above would be to write a string representation of your time:

format_date( to_time( to_string( "<float_time_field>" ), 'hmmss.z' ), 'hh:mm:ss:z' )

Remove :z to not include milliseconds.

While you could achieve the same results with plain regexp logic, using the time detour is somewhat more convenient.

5
  • Hmm; not working, I'm trying this: to_time( to_string( "HMMSS" ), 'hmmss.z' ) Commented Jun 3, 2021 at 13:48
  • 3
    @Bacon_Sammich Yeah, as @BERA said, Shapefiles do not support time. See my update. You can also wait for someone to add a regexp based solution; I doubt there is much to gain from avoiding the time detour in favor for regexp in the expression, though.
    – geozelot
    Commented Jun 3, 2021 at 14:04
  • I got really close with this: try (format_date( to_time( to_string( "Time_Float" ), 'hhmmss.z' ), 'hh:mm' ), (format_date( to_time( to_string( "Time_Float" ), 'hmmss.z' ), 'hh:mm' ))) But there are still some values that don't compute, like '1157.85'. Commented Jun 3, 2021 at 15:28
  • 4
    Well, TBH, that's probably the end of it; without any fixed rules to the float representation, you can't make sense of them. I.e. what does 1157.85 mean? 01:01:57? Or 01:15:07? Or 00:11:57 even?
    – geozelot
    Commented Jun 3, 2021 at 15:32
  • When you put it like that... you have an excellent point! Time to go back to the drawing board, thanks for all your help. Commented Jun 3, 2021 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.