0

I want to mimic the format used in log4j for timestamps using the linux date (just some quick scripting)

On log4j the format is defined as %d{dd MMM yyyy HH:mm:ss,SSS} That would translate into lines like:

2016-03-10 07:01:09,778
2016-03-10 07:01:09,784

Can this be accomplished in date? The closer I got was using

date +"%Y-%m-%d %H:%M:%S,%N"

But this still has 9 digits instead of 3, any ideas? I'd prefer not using sed/cut,etc, but if there's no alternative it'd be fine.

aseques
  • 537
  • 4
  • 21
  • 1
    Possible duplicate of [Get Formatted Date From Timestamp With Rounded Milliseconds Bash Shell Script](http://stackoverflow.com/questions/16036763/get-formatted-date-from-timestamp-with-rounded-milliseconds-bash-shell-script) –  Mar 11 '16 at 12:32

1 Answers1

1

That %N is giving you the number of nanoseconds, (one billion per second) hence the nine digits, and GNU date doesn't have a milliseconds option.

Thankfully we can specify the field width like this

date +"%Y-%m-%d %H:%M:%S,%3N"

To turn billionths into thousandths.

M.

Martin Cowie
  • 2,788
  • 7
  • 38
  • 74