Skip to content

Instantly share code, notes, and snippets.

@catichenor
Last active August 8, 2018 17:24
Show Gist options
  • Select an option

  • Save catichenor/910acdc3d8e86bb73f3a4b126e20af38 to your computer and use it in GitHub Desktop.

Select an option

Save catichenor/910acdc3d8e86bb73f3a4b126e20af38 to your computer and use it in GitHub Desktop.
Command to parse a human date description and output a date.
#!/usr/bin/python
from __future__ import print_function # Need to grab the print function from Python 3.
import parsedatetime # Requires "parsedatetime" Python module
from datetime import datetime
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument("input", nargs='?', help="text to convert into a date.")
argparser.add_argument("-d", "--dash", action="store_true", help="use dashes instead of slashes as a seperator.")
argparser.add_argument("-g", "--github", action="store_true", help="output in GitHub dashed format, i.e. \"yyyy-mm-dd\"")
args = argparser.parse_args()
if args.dash or args.github:
dateSeparator = "-"
else:
dateSeparator = "/"
if not args.input: # If there's nothing on the command line, read from the clipboard.
try:
from Tkinter import Tk # Might need to get tkinter from your package manager on Linux.
except:
from tkinter import Tk # Fix for Python 3, which changed the case of Tkinter.
r = Tk()
r.withdraw()
result = r.selection_get(selection = "CLIPBOARD")
else:
result = args.input
cal = parsedatetime.Calendar()
time_struct, parse_status = cal.parse(result)
parsedDate = datetime(*time_struct[:6])
if not args.github:
printDate = str(parsedDate.month) + dateSeparator + str(parsedDate.day) + dateSeparator + str(parsedDate.year) # Using the admittedly horrible American date format.
else:
printDate = str(parsedDate.year) + dateSeparator + str(parsedDate.month).zfill(2) + dateSeparator + str(parsedDate.day).zfill(2)
print(printDate, end="")
# r.clipboard_clear()
# r.clipboard_append(printDate) # Code to send to clipboard, broken in Python 3, worked around by printing output and piping to clip.exe/xsel -ib
if not args.input:
r.destroy()
@catichenor
Copy link
Copy Markdown
Author

catichenor commented Aug 8, 2018

I used this recently to get GitHub commits within a time period.

Example:

git log --all --after="`human-date-parser.py -g 'two weeks ago'` 00:00" --before="`human-date-parser.py -g 'today'` 23:59"

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