Skip to content

Instantly share code, notes, and snippets.

@pfuntner
Last active April 3, 2018 15:46
Show Gist options
  • Save pfuntner/ed3a7f9591c214e05ce320bf7a9df59d to your computer and use it in GitHub Desktop.
Save pfuntner/ed3a7f9591c214e05ce320bf7a9df59d to your computer and use it in GitHub Desktop.
Unix-style date script for Cygwin/Git bash
#! /usr/bin/python
import sys
import subprocess
cmd = sys.argv[1:]
if not any(word.startswith("+") for word in cmd):
cmd.insert(0, "+%a %b %e %H:%M:%S %Z %Y")
cmd.insert(0, "date")
exit(subprocess.Popen(cmd).wait())
@pfuntner
Copy link
Author

pfuntner commented Apr 3, 2018

This script is a companion to the GNU date on Windoze blog entry I wrote.

I've made changes to this script to recognize that other options can introduce a different format - the test was a little too naive. This is still the basic idea but a better version is in my Git repo.

@pfuntner
Copy link
Author

pfuntner commented Apr 3, 2018

Even the new changes are not perfect:

$ date -d +30\ days                    # this doesn't behave as expected, the Unix format is not specified
Thu, May  3, 2018 11:36:28 AM

$ date -d+30\ days                     # this behaves as expected, the Unix format is specified
Thu May  3 11:36:32 EDT 2018

$

When a space separated the -d option and its argument, the script incorrectly assumed that +30 days was a format specification. To solve this:

  • the script could intelligently parse out every option and argument so it doesn't confuse arguments for options - not as simple as it may sound because date -ud +1\ day is valid - add one day and report UTC information. The script would have to know this is means the same as date -u -d +1\ day
  • do not leave a space between the option and the argument

How dumb is it for date to have the format specified this way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment