diff --git a/download/DatetimeFormatConverter.alfredworkflow b/download/DatetimeFormatConverter.alfredworkflow index 2713125..de71b3b 100644 Binary files a/download/DatetimeFormatConverter.alfredworkflow and b/download/DatetimeFormatConverter.alfredworkflow differ diff --git a/workflow/process.py b/workflow/process.py index 88dcf72..5591124 100644 --- a/workflow/process.py +++ b/workflow/process.py @@ -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 @@ -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: @@ -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