Possible Solution
date --set="2011-12-07 01:20:15.962" && date --rfc-3339=ns
Original Question
I can get the date with milliseconds like so:
date +"%S.%N"
date --rfc-3339=ns
According to man date
I would expect to be able to set it like so:
date --set=%s.%N +`date +"%S.%N"`
date --set="%s.%N" "+1323217126.085882000"
According to my googling I would expect to be able to set the date like so (the inverse of the above):
date +%s.%N -s `date +"%S.%N"`
date +"%s.%N" -s "1323217126.085882000"
Neither work. Can someone clue me in on the issue?
P.S. No, I don't need nanosecond resolution. Yes, I know bash execution takes milliseconds. What I really need is sub-second resolution, 10ths of a second would be good enough.
Here is a solution (Linux, NOT Unix):
Note the delay:
As you'll notice, whole milliseconds of delay are introduced. This is due to the time it takes to initialize memory for and load the
date
binary. This is true for all shells and theexec
of insert-higher-level-language-hereHowever, if you just need sub-second resolution in the range of 10ths of a second, this will good enough in many cases.
If your goal is to set time in with sub-second intervals, then date seems like the wrong command if I am reading the source correctly.
Assuming you are using the coreutils version of date, then it appears to me that
when.tv_nsec = 0
line is setting nanosecond portion of thewhen
variable which is a timespec structure to zero. Even if you could convince date to accept a more accurate value, it looks to me like it would be pointless.There is a better method that doesn't require converting the timestamp to a human-readable date format as in the accepted answer. You were extremely close with your original suggestion:
When giving timestamps, you need to prefix with an '@' like so and not quote them:
This will now correctly set the current Linux time in seconds with millisecond accuracy.