2

I am reading through the GNU coreutils manual, and I am experimenting with the date command.  Here is what I found in the text:

  1. The date command has a -f datefile option:

    • Parse each line in datefile as with -d and display the resulting date and time.
    • that means date is looking at each line in datefile and treating it as a date string for use to display a particular date.
  2. In chapter 29 (Date input formats), it is stated:

    The output of the date command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items like ‘IST’.  When using date to generate a date string intended to be parsed later, specify a date format that is independent of language and that does not use time zone items other than ‘UTC’ and ‘Z’.  Here are some ways to do this:

    $ LC_ALL=C TZ=UTC0 date
                 ︙
    
    • this statement (especially the bold part) implies to me that I should be able to put in the datefiledate command such as the one above, and the date command I issue in the terminal should be able to parse that date command in the datefile and output the corresponding date.

It is not working for me.  Either there is something wrong with my date command in the datefile (wrong quotation marks, some special syntax which I am not aware of...), or the -f datefile option is not capable of handling this situation.

What I am asking, in summary, is:

  • can the date -f command be fed a file where the date string on one line is another date command?  e.g., can I put in a file a string "date" on the first line (which, as a command, would return the system time), and then feed that file into a date -f command and get the current system time (generated by the date command in the file)?
2
  • 1
    What does your date command look like, and what does the file look like? In other words, please post your exact input and command(s).
    – anon
    Sep 27, 2016 at 6:36
  • The key bit you're missing re: the bolded bit, is that's when using date to generate a date. So you can use LC_ALL=C TZ=UTC0 date ... to generate the string that you then put in the file you're going to parse with date -f datefile.
    – Wildcard
    Mar 22 at 22:16

3 Answers 3

3

The correct format for a date-time that will be correctly parsed by GNU date is:

$ LC_ALL=C TZ=UTC0 date +"%Y-%m-%dT%H:%M:%S%z"
2016-09-28T04:08:55+0000

And that avoids spaces also, which may generate other problems.

If your file contains one line per date like:

$ cat dates.txt
1992-03-28T14:26:01+0000
2004-02-29T21:02:02+0000
2005-02-28T21:02:03+0000
2015-07-02T11:52:04+0000
2016-03-31T01:36:05+0000

Then this will work:

$ TZ=America/Los_Angeles date -f infile
Sat Mar 28 06:26:01 PST 1992
Sun Feb 29 13:02:02 PST 2004
Mon Feb 28 13:02:03 PST 2005
Thu Jul  2 04:52:04 PDT 2015
Wed Mar 30 18:36:05 PDT 2016


If what you want to do is create a file with "date commands" as in:

$ cat dates.txt
date -d "1992-03-28T14:26:01+0000"
date -d "2004-02-29T21:02:02+0000"
LC_ALL=C TZ=UTC0 date -d "2005-02-28T21:02:03+0000" +"%Y-%m-%d"
TZ=America/Los_Angeles date -d "2005-02-28T21:02:03+0000"
date -d "2015-07-02T11:52:04+0000"
date -d "2016-03-31T01:36:05+0000"

Then you need to execute each line with the shell.
One fast solution is to source the file and all commands will be excuted.

$ . ./dates.txt
Sat Mar 28 14:26:01 GMT 1992
Sun Feb 29 21:02:02 GMT 2004
2005-02-28
Mon Feb 28 13:02:03 PST 2005
Thu Jul  2 11:52:04 GMT 2015
Thu Mar 31 01:36:05 GMT 2016

Does this answer your question?

4
  • Many thanks guys for your interest in my question and for the support you are trying to provide. However, I must say, with all due respect, that you are missing the point of my question: all your posts place a valid date string (generated by a date command) in a file and then use the date -f command. However, that is not what I am asking. What I am asking is: can the date -f command be fed a file where the date string on one line is another date command? Sep 28, 2016 at 12:29
  • e.g. can I put in a file a string "date" on the first line (which as a command would return the system time), and then feed that file in a date -f command and get the current system time (generated by the date command in the file)? Sep 28, 2016 at 12:30
  • @AndreiCostache For your first question the answer is No. The date command could not interpret another date command. You need the shell to execute commands, not date. You can however source a file that has a list of date commands. I'll write that up. Please modify your question to explain this requirement.
    – user232326
    Sep 28, 2016 at 16:56
  • I modified the question with a copy paste of my additional explanations which were much better understood. I would recommend that in your definitive answer, you mention also that the date -d command is in fact capable of this. (see chapter 21.1.6 in the GNU Coreutils manual where there is a specific example for this). This way there is less chance for confusion (I think); Sep 29, 2016 at 6:03
0

With date from coreutils 8.21, TZ=UTC and LC_ALL=C I get

$ date > x; sleep 1; date >> x; sleep 1; date >> x
$ date -f x
Wed Sep 28 03:32:39 UTC 2016
Wed Sep 28 03:32:40 UTC 2016
Wed Sep 28 03:32:41 UTC 2016
$

as expected. I checked the source code, too: The magic happens in the routine batch_convert where essentially there's a loop over parse_datetime()/show_date() just as in the command line case (using -d).

0

No, you can not read LC_ALL=C TZ=UTC0 from a file with date -f.

cat tz
LC_ALL=C TZ=UTC0

date -f tz
date: invalid date ”LC_ALL=C TZ=UTC0”

You can, however, put the entire command in a file, like this:

cat date
#!/bin/bash

LC_ALL=C TZ=UTC0 date

Then run it from the command line, which gives you:

./date 
Tue Sep 27 06:56:29 UTC 2016

As opposed to the timezone on my server which is:

date
tis 27 sep 2016 08:56:52 CEST
2
  • your first example has no chance to work, because you are writing in the file only the locale/timezone modifiers of the date command but not the date command itself; the date -f tz command will try to recognize a date in the string "LC_ALL=C TZ=UTC0", which fails... Sep 27, 2016 at 15:19
  • your second example I can follow, but it does not answer my question whether date is in fact capable of this feature or not. I will keep messing with it and post back if I find anything. Sep 27, 2016 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .