Converting DateTime to string
-
Hi Everyone,
I can't seem to find a way to convert a DateTime variable to a string. I am trying to create a file at the start of my gcode that will include the current date and time (using state.time) in its title; however, it seems that the name of a file must be of type string.
Any thoughts? Thanks!
Current Firmware: RepRapFirmware for Duet 3 MB6HC 3.4.6
-
@jonathanbrent use the ^ operator to concatenate it with a string literal, even an empty string literal will do.
-
@dc42 Thanks! Now it turns out my issue is really that I can't include the ":" character in a file name. Is there a way for me to modify the string that I have retrieved from state.time (e.g. 2024-03-12T12:54:43)? Or is there some other way to get the current time without colons?
Thanks again
-
@jonathanbrent I'm sorry, I can't think of a workaround at present.
-
@jonathanbrent can you convert it to an int instead of a string. They the files will be sequential, with the right number of seconds between names. Just not easily human readable without converting the int back to a datetime.
Being able to use datetime in a filename is a feature I would like as well. There is a feature request tracking something similar here:
https://github.com/Duet3D/RepRapFirmware/issues/787 -
maybe a generic split function for strings would suffice?
-
@oliof string split to an array would be even more useful for sure.
-
@T3P3Tony or maybe a replace-in-string function e.g.
replace(state.time, ':', '-')
-
@dc42 yes that would work as well for the particular usecase of naming a file with a datetime that's a valid filename. Its less generalisable than being able to use just the date or just the time - but that's not necessarily an urgent requirement.
It depends on the overhead to implement various types of string handling.