Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified download/DatetimeFormatConverter.alfredworkflow
Binary file not shown.
37 changes: 35 additions & 2 deletions workflow/process.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# -*- coding: utf-8 -*-

import re
import operator
import alfred
import calendar
from datetime import datetime
from delorean import utcnow, parse, epoch

def process(query_str):
""" Entry point """
value = parse_query_value(query_str)

if value is not None:
results = alfred_items_for_value(value)
xml = alfred.xml(results) # compiles the XML answer
Expand All @@ -16,8 +20,13 @@ def parse_query_value(query_str):
""" Return value for the query string """
try:
query_str = str(query_str).strip('"\' ')
if query_str == 'now':
d = utcnow()
match = re.match('(\+|\-)(\d+)([smhdMy])|now', query_str)

if match is not None:
if match.group(0) == 'now':
d = utcnow()
else:
d = shift_time(match.group(1), match.group(2), match.group(3))
else:
# Parse datetime string or timestamp
try:
Expand All @@ -28,6 +37,30 @@ def parse_query_value(query_str):
d = None
return d

def shift_time(op, value, measure):
# Create operator map, to avoid some if/else's
op_map = {'+' : operator.add, '-' : operator.sub}
multiplier = 1

if measure == 'm':
multiplier = 60
elif measure == 'h':
multiplier = 60*60
elif measure == 'd':
multiplier = (60*60)*24
elif measure == 'w':
multiplier = ((60*60)*24)*7
elif measure == 'M':
multiplier = ((60*60)*24)*30 # egh..
elif measure == 'y':
multiplier = ((60*60)*24)*365

# Convert our value + measure to seconds
seconds = multiplier * int(value)
current_ts = calendar.timegm(datetime.now().timetuple())

return epoch(op_map[op](current_ts, seconds))

def alfred_items_for_value(value):
"""
Given a delorean datetime object, return a list of
Expand Down