Skip to content

Commit

Permalink
Add timezone control and Alfred4 supporting
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhe1 committed Feb 1, 2022
1 parent 02ffc84 commit f3d7212
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
alfred-datetime-format-converter
================================

Alfred 2 workflow for converting between timestamps and formatted datetime strings with ease.
Alfred workflow for converting between timestamps and formatted datetime strings with ease.

Simply type "df" followed by: "now", a UTC unix timestamp, or a formatted datetime string.

Expand All @@ -14,3 +14,7 @@ This will present you with the parsed date in various formats ready to copy to y
![Screenshot](https://raw.github.com/mwaterfall/alfred-datetime-format-converter/master/download/screenshot_1.png)
![Screenshot](https://raw.github.com/mwaterfall/alfred-datetime-format-converter/master/download/screenshot_2.png)
![Screenshot](https://raw.github.com/mwaterfall/alfred-datetime-format-converter/master/download/screenshot_3.png)

You can define your timezone variable in Alfred workflow variables (Name "timezone", Value following pytz convertions like "US/Eastern") to control displaying datetime. If variable missing, UTC is used by default.

![Screenshot](https://raw.github.com/mwaterfall/alfred-datetime-format-converter/master/download/screenshot_4.png)
Binary file added download/screenshot_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions workflow/alfred.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def xml(self):
def args(characters=None):
return tuple(unescape(decode(arg), characters) for arg in sys.argv[1:])

def env_arg(name):
return os.getenv(name)

def config():
return _create('config')

Expand All @@ -71,10 +74,10 @@ def unescape(query, characters=None):

def work(volatile):
path = {
True: '~/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data',
False: '~/Library/Application Support/Alfred 2/Workflow Data'
true: env_arg('alfred_workflow_cache'), # using alfred env variable instead of fixed string
false: env_arg('alfred_workflow_data')
}[bool(volatile)]
return _create(os.path.join(os.path.expanduser(path), bundleid))
return _create(os.path.expanduser(path))

def write(text):
sys.stdout.write(text)
Expand Down
6 changes: 3 additions & 3 deletions workflow/delorean/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
utc = timezone("utc")


def parse(s, dayfirst=True, yearfirst=True):
def parse(s, tz=UTC, dayfirst=True, yearfirst=True):
"""
Parses a datetime string in it and returns a `Delorean` object.
Expand All @@ -25,8 +25,8 @@ def parse(s, dayfirst=True, yearfirst=True):
# raise a parsing error.
raise ValueError("Unknown string format")
if dt.tzinfo is None:
# assuming datetime object passed in is UTC
do = Delorean(datetime=dt, timezone=UTC)
# assuming datetime object passed in is tz given
do = Delorean(datetime=dt, timezone=tz)
else:
dt = utc.normalize(dt)
# makeing dt naive so we can pass it to Delorean
Expand Down
24 changes: 17 additions & 7 deletions workflow/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import calendar
from delorean import utcnow, parse, epoch

def get_timezone():
tz = alfred.env_arg('timezone')
if not tz:
tz = 'UTC'
return tz

def process(query_str):
""" Entry point """
value = parse_query_value(query_str)
Expand All @@ -23,7 +29,7 @@ def parse_query_value(query_str):
try:
d = epoch(float(query_str))
except ValueError:
d = parse(str(query_str))
d = parse(str(query_str), get_timezone())
except (TypeError, ValueError):
d = None
return d
Expand Down Expand Up @@ -51,20 +57,24 @@ def alfred_items_for_value(value):
index += 1

# Various formats
tz = get_timezone()
formats = [
# 1937-01-01 12:00:27
("%Y-%m-%d %H:%M:%S", ''),
("%Y-%m-%d %H:%M:%S", tz),
# 19 May 2002 15:21:36
("%d %b %Y %H:%M:%S", ''),
("%d %b %Y %H:%M:%S", tz),
# Sun, 19 May 2002 15:21:36
("%a, %d %b %Y %H:%M:%S", ''),
("%a, %d %b %Y %H:%M:%S", tz),
# 1937-01-01T12:00:27
("%Y-%m-%dT%H:%M:%S", ''),
("%Y-%m-%dT%H:%M:%S", tz),
# 1996-12-19T16:39:57-0800
("%Y-%m-%dT%H:%M:%S%z", ''),
("%Y-%m-%dT%H:%M:%S%z", tz),
]
for format, description in formats:
item_value = value.datetime.strftime(format)
tz_value = value
if description:
tz_value = value.shift(description)
item_value = tz_value.datetime.strftime(format)
results.append(alfred.Item(
title=str(item_value),
subtitle=description,
Expand Down

0 comments on commit f3d7212

Please sign in to comment.