Is it currently possible to output date time with millisecond resolution?
E.g. a date time object containing: 2017-05-29 11:08:34.768
Having looked at the times module, seconds seems to be the most fine grained for full dates so far.
As part of a workflow, I process 10Hz GPS logs, and for various reasons full date-timestamps with ms precision are needed for each logged point (the data exists in the logs, it just needs some processing).
Have I missed something in the documentation? Or is it not yet implemented, and if so, does anyone know if it will be?
Strange that I can't find a Nim implementation of strftime, which has %f for microseconds. The times module floats definitely have enough precision, but support for sub-second intervals in time string formatting functions seems lacking...
Probably not the prettiest way to do this, but here's a thought:
import times, strutils
let
startEpochFloat = epochTime()
startEpochStr = startEpochFloat.formatFloat(precision = 32)
echo "Time float: " & startEpochStr
let
startSubSecStr = startEpochStr.split(".")[^1]
echo "Sub-second: " & startSubSecStr
let
startTimeInfo = getLocalTime(fromSeconds(startEpochFloat))
startTimeStr = startTimeInfo.format("yyyy-MM-dd HH:mm:ss")
startTimeWithSec = startTimeStr & "." & startSubSecStr[0..2]
echo "Str format: " & startTimeWithSec
I get something like:
Time float: 1506307515.2517170906066894531250
Sub-second: 2517170906066894531250
Str format: 2017-09-24 22:45:15.251
Strange that I can't find a Nim implementation of strftime, which has %f for microseconds
Yes, this is exactly what I was looking for and what I successfully used in Python (I've got something similar working in Rust as well). For the milliseconds() proc in the times docs it is specifically mentioned that "Note: not all time functions have millisecond resolution". If this is true for the string representations (it seems to be) it's unfortunately bit of a showstopper at the moment but...
Probably not the prettiest way to do this, but here's a thought: [CODE]
...I can probably compartmentalise some variant of your code well enough as a stopgap measure until date time objects and their string representations include sub-second values, if such functionality is indeed in the pipeline.
Thanks for your help!
I found this thread looking for how to handle milliseconds in importing time from strings and could not find such in the times library. I too have done this in Smalltalk and in Python and am surprised nothing in Nim. This is critical for me as I am processing gigabytes of csv files with datetimes with milliseconds per row. :(
I must be able to do something similar to:
var dt = parse("2018-04-29 17:00:04.507000000", "yyyy-MM-dd hh:mm:ss.???")
I am sorry but this is complete deal breaker. It is an absolute requirement. I do not have the ability to implement it must be there.
Thanks for the otherwise wonderful language.
@alehander42 I don't know. I tried parse and adding the milliseconds as a TimeInterval and echo() shows no milliseconds. now() shows no milliseconds. And I don't see any way to access millisecond values in the DateTime object documentation. I have to be able sort on DateTime with millisecond values as multiple events can occur during a second and order is important.
@GULPF This is wonderful news. I have devel cloned on my machine but I have not compiled it. I will do so and give a try.
Thanks.
I think for my app, the hack I will currently employee is simply storing and using the datetime string. It stores and sorts fine. No information is lost. Any operations that I need to do on a DateTime object do not involve milliseconds. I do not need TimeIntervals in milliseconds. I will just write procs that convert to DateTime and do what I need to do and convert back to a string as needed.
For my app, I think this is a perfectly reasonable solution. I just wanted to put that out just in case this type of workaround is beneficial to someone else. At least until what is in devel is released.
Thanks.