Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The date command doesn't offer such thing, which is kind of sad since RFC-3339 is the modern, widespread, sane format used everywhere (except in email which is neither modern nor sane).

My timezone offset is currently -08:00 so the simplest form of this command should print the current time as 2013-09-05T14:58:33.102-08:00.

share|improve this question
    
Which standard (s) are you interested in? –  Stéphane Chazelas Mar 19 at 21:54
    
I personally am fine with GNU but in the grand scheme of things it really should be broader than that. It should also come with a reasonable default (short option, millisecond precision) and have a way to specify the subsecond precision (number of digits other than 0 or 9). –  Martin Jambon Mar 19 at 22:37
1  
It's not a sane format: the total absence of whitespace makes it unnecessarily hard for humans to read. You should use the slight variation 2013-09-05 14:58:33.102 -0800 except when there is some reason why spaces must not be used at all. –  Zack Mar 20 at 1:26
    
I was comparing it to the mail date format (RFC 822, 2822) which is hard to read for both humans and machines. –  Martin Jambon Mar 20 at 1:31

3 Answers 3

With GNU date (5.90 or above):

$ TZ=America/Anchorage date '+%FT%T.%N%:z'
2014-03-19T14:29:31.041119357-08:00

Replace %N with %3N for milliseconds, %6N for micro-seconds...

AFAIK, none of the POSIX, Unix or LSB specifications specify any command that can display times with sub-second granularity, but the fractional part is optional in RFC 3339.

POSIX/Unix/LSB strftime supports %z to display the TZ offset as -0800, so the most portable you're probably going to get is:

 $ TZ=America/Anchorage perl -MPOSIX -le '$t = strftime "%Y-%m-%dT%T%z",
   localtime; $t =~ s/..$/:$&/; print $t'
 2014-03-19T14:30:23-08:00
share|improve this answer

It seems like you can do several formats using the switch to the GNU implementation of date (version 5.90 or above), --rfc3339=.

Examples

$ date --rfc-3339=date
2014-03-19

$ date --rfc-3339=seconds
2014-03-19 18:00:05-04:00

$ date --rfc-3339=ns
2014-03-19 18:00:08.179780629-04:00

If you want the T to be added, as a hack:

$ date --rfc-3339=seconds | sed 's/ /T/'
2014-03-19T18:35:03-04:00

If you want it in milliseconds:

$ date --rfc-3339=ns | sed 's/ /T/; s/\(\....\).*-/\1-/g'
2014-03-19T18:42:52.362-04:00

References

share|improve this answer

GNU date has the ISO-8601 format built in - isn't that quite close or even idential to RFC-3339?

1065 % date --iso-8601=seconds
2014-03-19T16:51:16-0600
share|improve this answer
    
ISO-8601 allows different formattings of dates and times, but I think the OP is asking specifically for W3C Date format "Complete date plus hours, minutes, seconds and a decimal fraction of a second YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)" - W3C Date and Time Formats. –  hakre Oct 13 at 10:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.