Zettelkasten Forum

Script to normalize date-time strings from the clipboard

#ruby #script #timestamp

I wanted to copy some metadata of a Tweet the other day to cite a person in a Zettel. My computer language is set to English, so I get am/pm times. Of course all I care about are ISO-formatted date and time strings!

So I wondered how bad it'd be to use a natural language parser for the dates in a script. The popular "chronic" Ruby gem didn't recognize the format. With the "timeliness" Ruby gem, I was able to specify the format, but where's the fun in that?

require "timeliness"
Timeliness.parse("1:51 PM - 12 Apr 2019", format: "h:nn_ampm - dd mmm yyyy")
# => 2019-04-12 13:51:00 +0200

Turns out nickel is very good at parsing U.S. Twitter dates:

require "nickel"
Nickel.parse("1:51 PM - 12 Apr 2019").occurrences[0].start_time.to_time
#  => 2019-06-21 13:51:00 +0200

It's a bit clunky to use as a oneliner, but wrapped in a Keyboard Maestro or Alfred action or Quicksilver action or Automator workflow, I think this is pretty useful. You should be able to change the output format, but I like the default.

How do you automate stuff like that?

Author at Zettelkasten.de • https://christiantietze.de/


  • I use the Python module parsedatetime in most of my Keyboard Maestro macros that require interpreting/converting dates. It works pretty well for my purposes and even understands dates such as "2w 1d" (in 2 weeks and 1 day) and "next mon" (next Monday).

    If you like The Archive's "PrettyFunctional (Basic)" theme, consider upgrading to the "PrettyFunctional (Regular)" theme.

  • That package works great, too! Thanks @Basil

    This would be the script I end up with (accepting pasteboard as input, aka stdin):

    import parsedatetime, time, sys
    input = sys.stdin.read()
    parsedTuple = parsedatetime.Calendar().parse(input)
    parsedTime = parsedTuple[0]
    result = time.strftime("%Y-%m-%d %H:%M", parsedTime)

    Author at Zettelkasten.de • https://christiantietze.de/

Sign In or Register to comment.