zerocode
zerocode copied to clipboard
Generating Current Timestamp with microsecond with "SSSSSS" - Works but with padding three zeroes
Actual
"timeNow":"${LOCAL.DATETIME.NOW:yyyy-MM-dd'T'HH:mm:ss.SSSSSS}Z"
Generates:
ā2023-12-06T14:17:47.738000ā š“ <ā Always generates with ā000ā padding, which is not exactly correct
"timeNow":"${LOCAL.DATETIME.NOW:yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnn}Zā
ā2023-12-06T14:17:47.738553101ā š¢ <ā works as expected.
Expected
"timeNow":"${LOCAL.DATETIME.NOW:yyyy-MM-dd'T'HH:mm:ss.SSSSSS}Z"
Should generates:
ā2023-12-06T14:17:47.738109ā
Or
ā2023-12-06T14:17:47.738125ā
Or
ā2023-12-06T14:17:47.738775ā
Etc
Potential Fix
- If java8 has no outofthebox date/time function to generate this, then another PH(placeholder)-Token could be implemented to solve this.
- Or suggest any other idea š”
Hi,
I would like to contribute to your project, to gain in experience. How can i take this issue :) ?
Ayoub
Hi,
I would like to contribute to your project, to gain in experience. How can i take this issue :) ?
Ayoub
@abhija77 š , can you have a look at the CONTRIBUTING and BUILDING please. That should give you some start point to crack on.
Hello :) I am new to open source contribution and came across this issue, which I would like to solve. But I just have a few questions.
-
What test do I need to run to see this timeNow field being printed out in the log? I searched for this "timeNow" field within source code but is it just variable name or an actual field name in the log? I saw that it is the request and response time stamps that take the value from this timeNow field variable and is built to make CoreLogger. So is the name of this "timeNow" field accurate or am I missing something?
-
I am thinking of using Java 8's DateTimeFormatter to format to string first and convert it back to localdatetime type. Is this idea viable?
I think this is the issue with Java 8, the LocalDateTime
is actually returning the clock
value, which in Java 8 is based on System.currentTimeMillis()
(in most operating system), and this is the reason it only returns 3 digits.
and also, when I was testing:
"timeNow":"${LOCAL.DATETIME.NOW:yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnn}Zā
ā2024-05-31T17:29:06.979000000ā š¢ <ā This return is different from yours, I dont know if I missed anything here.
I'm also uncertain if this level of accuracy is necessary. Since the value is used for comparison with API returns, the nanoseconds (and even milliseconds) might not align with the actual return values. To me, this seems unnecessary.
This is just my perspective, and I could be mistaken.
Cheers,
Actual
"timeNow":"${LOCAL.DATETIME.NOW:yyyy-MM-dd'T'HH:mm:ss.SSSSSS}Z" Generates: ā2023-12-06T14:17:47.738000ā š“ <ā Always generates with ā000ā padding, which is not exactly correct
"timeNow":"${LOCAL.DATETIME.NOW:yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnn}Zā ā2023-12-06T14:17:47.738553101ā š¢ <ā works as expected.
Expected
"timeNow":"${LOCAL.DATETIME.NOW:yyyy-MM-dd'T'HH:mm:ss.SSSSSS}Z" Should generates: ā2023-12-06T14:17:47.738109ā Or ā2023-12-06T14:17:47.738125ā Or ā2023-12-06T14:17:47.738775ā Etc
Potential Fix
- If java8 has no outofthebox date/time function to generate this, then another PH(placeholder)-Token could be implemented to solve this.
- Or suggest any other idea š”
I think we can close this issue.
As you suggested, we can get almost the same result by this:
Instead of "timeNow":"...'HH:mm:ss.SSSSSS}Z"
, the below can be used to get all the digits populated:
"timeNow":"...'HH:mm:ss.SSS}123Z"
(... SSS}123 or SSS}456 etc... where SSS will resolve to random timelapsed mili-sec always and then, 123 or 456 will be static, but together the whole number will be a random and unique and can be treated like nano-sec)
But then why to use 123 , we can use any random number , generated using (int) (Math.random() * 999) + 1
But then why to use 123 , we can use any random number , generated using (int) (Math.random() * 999) + 1
Good thought. But does this works out of the box, not sure ? Give it a try!
(int) (Math.random() * 999) + 1
Probably not straight away as it has got Math.random()
java language specific expression here.
Then to solve this you can pick the value from an earlier step by calling a static method if that helps.
But I would prefer to go with SSS123
as the SSS
is still random, which results in the entire 6 digits random š
Looks like not an issue anymore
Add documentation with some examples