1

I have read the following question: Convert from Unix time at the command line and Convert Any Format In Unix. I have tried a few different ways to convert my time 2017-10-12 00:34:26 which is in the date "+%F %T" format to the date -R format or Thu, 12 Oct 2017 00:34:26 -0400.

Since I need to convert a list of dates like the following, I'm using $etime as a variable for just one line of the file (until I got it working).

file1:

2017-10-12 00:22:26
2017-10-12 00:25:26
2017-10-12 00:28:26
2017-10-12 00:31:26
2017-10-12 00:34:26

1st attempt:

etime=$(echo "2017-10-12 00:34:26"); date -Rd @$etime

2nd attempt:

etime=$(echo "2017-10-12 00:34:26"); | gawk '{print strftime("%c", $0)}'

Although these two didn't work, I was hoping to get them to work and then just loop the command for each line in file1 so the result would be:

Thu, 12 Oct 2017 00:22:26 -0400
Thu, 12 Oct 2017 00:25:26 -0400
Thu, 12 Oct 2017 00:28:26 -0400
Thu, 12 Oct 2017 00:31:26 -0400
Thu, 12 Oct 2017 00:34:26 -0400

Does anyone know an efficient way to convert these date formats in a list? Your help and support for the question is much appreciated.

DomainsFeatured
  • 1,426
  • 1
  • 21
  • 39

2 Answers2

1

GNU awk solution:

awk '{ gsub(/[-:]/," "); print strftime("%c %z",mktime($0)) }' file
  • %c - The locale’s “appropriate” date and time representation. (This is ‘%A %B %d %T %Y’ in the "C" locale.)

  • %z - The time zone offset in a ‘+HHMM’ format (e.g., the format necessary to produce RFC 822/RFC 1036 date headers)

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • So close! Do you know how to get rid of the AM/PM in the output? I just need it on the 24 hour clock. – DomainsFeatured Oct 11 '17 at 20:11
  • I'm getting an output like this (first line) `Thu, Oct 12, 2017 12:22:26 AM -0400` but I thought RFC would be like this: `Thu, Oct 12, 2017 00:22:26 -0400`. Do you know how to do that? Thanks so much for the help. – DomainsFeatured Oct 11 '17 at 20:15
  • Getting closer...now I got this `Thu Oct 12 00:22:26 2017 -0400`. (The year is out of place and the commas are gone). – DomainsFeatured Oct 11 '17 at 20:20
  • any ideas on how to remove the `AM/PM`? – DomainsFeatured Oct 11 '17 at 20:38
  • Nope :-/ It gave: `Thu, Oct 12, 2017 12:22:26 AM -0400` – DomainsFeatured Oct 11 '17 at 20:44
  • Tried removing that and it didn't work. However, I don't think simply removing the AM/PM will work because times that are at 12:01 AM will not be converted to 00:01. I think it's just generating that automatically. It has something to do the with the %z format – DomainsFeatured Oct 11 '17 at 20:57
1

This requires GNU date for the -f option:

date -R -f file1

resulting in

Thu, 12 Oct 2017 00:22:26 -0400
Thu, 12 Oct 2017 00:25:26 -0400
Thu, 12 Oct 2017 00:28:26 -0400
Thu, 12 Oct 2017 00:31:26 -0400
Thu, 12 Oct 2017 00:34:26 -0400

From the date man page:

-f, --file=DATEFILE
like --date; once for each line of DATEFILE

and

-d, --date=STRING
display time described by STRING, not 'now'

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116