zerocode icon indicating copy to clipboard operation
zerocode copied to clipboard

Generating Current Timestamp with microsecond with "SSSSSS" - Works but with padding three zeroes

Open authorjapps opened this issue 10 months ago ā€¢ 7 comments

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 šŸ’”

authorjapps avatar Apr 08 '24 15:04 authorjapps

Hi,

I would like to contribute to your project, to gain in experience. How can i take this issue :) ?

Ayoub

abhija77 avatar May 02 '24 09:05 abhija77

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.

nirmalchandra avatar May 02 '24 15:05 nirmalchandra

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.

  1. 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?

  2. 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?

brian6484 avatar May 22 '24 14:05 brian6484

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 šŸ’”

luke-zhou avatar May 31 '24 08:05 luke-zhou

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)

nirmalchandra avatar Jun 10 '24 16:06 nirmalchandra

But then why to use 123 , we can use any random number , generated using (int) (Math.random() * 999) + 1

anmol797 avatar Jun 19 '24 21:06 anmol797

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 šŸ˜„

nirmalchandra avatar Jun 22 '24 18:06 nirmalchandra

Looks like not an issue anymore

authorjapps avatar Jul 08 '24 12:07 authorjapps

Add documentation with some examples

authorjapps avatar Jul 08 '24 12:07 authorjapps