I have the following function, which was made with the help of some people here:
module DateTimeFormatter =
let mutable year = -1
let mutable month = -1
let mutable day = -1
let mutable hour = -1
let mutable minute = -1
let mutable second = -1
let dateTimeArray = "xxxx-xx-xx xx:xx:xx.xxx".ToCharArray()
let timeArray = "xx:xx:xx.xxx".ToCharArray()
let zeroChar = int '0'
// format the datetime into a date and a time with milliseconds
let format (dateTime: DateTime) =
if dateTime.Year <> year then
year <- dateTime.Year
dateTimeArray.[0] <- char (zeroChar + year / 1000)
dateTimeArray.[1] <- char (zeroChar + (year % 1000) / 100)
dateTimeArray.[2] <- char (zeroChar + (year % 100) / 10)
dateTimeArray.[3] <- char (zeroChar + (year % 10))
if dateTime.Month <> month then
month <- dateTime.Month
dateTimeArray.[5] <- char (zeroChar + month / 10)
dateTimeArray.[6] <- char (zeroChar + month % 10)
if dateTime.Day <> day then
day <- dateTime.Day
dateTimeArray.[8] <- char (zeroChar + day / 10)
dateTimeArray.[9] <- char (zeroChar + day % 10)
if dateTime.Hour <> hour then
hour <- dateTime.Hour
dateTimeArray.[11] <- char (zeroChar + hour / 10)
dateTimeArray.[12] <- char (zeroChar + hour % 10)
if dateTime.Minute <> minute then
minute <- dateTime.Minute
dateTimeArray.[14] <- char (zeroChar + minute / 10)
dateTimeArray.[15] <- char (zeroChar + minute % 10)
if dateTime.Second <> second then
second <- dateTime.Second
dateTimeArray.[17] <- char (zeroChar + second / 10)
dateTimeArray.[18] <- char (zeroChar + second % 10)
let ms = dateTime.Millisecond
dateTimeArray.[20] <- char (zeroChar + ms / 100)
dateTimeArray.[21] <- char (zeroChar + (ms % 100) / 10)
dateTimeArray.[22] <- char (zeroChar + ms % 10)
new string(dateTimeArray)
The idea is to build date time + millisecond timestamp as fast as possible.
But sometimes, and not always, I get this output:
2021-06-01 xx:xx:53.648
And the failure is always on the hours / minutes, but sometimes it works fine and sometimes it doesn't. and I don't see anything wrong with the tests when building the string, but it looks like hour and minutes fail even the first test since there is the placeholder 'x' in place.
It may be one of these super simple things that require another set of eyes to see it :)