|
| 1 | +#!/usr/bin/python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | + |
| 5 | +import arrow |
| 6 | +from dateutil import tz |
| 7 | +from workflow import ICON_CLOCK, ICON_INFO |
| 8 | + |
| 9 | +FORMAT_LIST = ( |
| 10 | + (ICON_INFO, 'X', 'UTC Timestamp (s)'), |
| 11 | + (ICON_INFO, 'x', 'UTC Timestamp (us)'), |
| 12 | + ( |
| 13 | + ICON_CLOCK, 'YYYY-MM-DD HH:mm:ss', 'Date and Time' |
| 14 | + ), |
| 15 | + ( |
| 16 | + ICON_CLOCK, 'W, DDDD[th day]', |
| 17 | + 'ISO Week date and Day for year' |
| 18 | + ), |
| 19 | + ( # https://www.w3.org/TR/NOTE-datetime |
| 20 | + ICON_CLOCK, 'YYYY-MM-DDTHH:mm:ssZZ', |
| 21 | + 'W3C' |
| 22 | + ), |
| 23 | + (ICON_CLOCK, arrow.FORMAT_RFC850, 'RFC850'), |
| 24 | + # FORMAT_RFC3339 |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def parser_query(wf): |
| 29 | + """parser datetime, timezone, shift""" |
| 30 | + try: |
| 31 | + query = wf.args[0].encode('utf8').strip(' ').rstrip(' ') |
| 32 | + |
| 33 | + if query.isdigit(): |
| 34 | + query = int(query) |
| 35 | + |
| 36 | + wf.logger.debug('query string:{} {}'.format(type(query), query)) |
| 37 | + return arrow.get(query), False |
| 38 | + |
| 39 | + except (IndexError, arrow.ParserError): |
| 40 | + wf.logger.debug('args:{}'.format(wf.args)) |
| 41 | + return arrow.get(), True |
| 42 | + |
| 43 | + |
| 44 | +def create_feedback(time, is_now): |
| 45 | + f = list() |
| 46 | + for icon, fmt, desc in FORMAT_LIST: |
| 47 | + value = time.to(tz.tzlocal()).format(fmt) |
| 48 | + # value = t.format(fmt) |
| 49 | + |
| 50 | + f.append({ |
| 51 | + 'title': value, |
| 52 | + 'subtitle': 'Current {}'.format(desc) if is_now else desc, |
| 53 | + 'valid': True, |
| 54 | + 'arg': value, |
| 55 | + 'icon': icon, |
| 56 | + }) |
| 57 | + |
| 58 | + return f |
| 59 | + |
| 60 | + |
| 61 | +def do_convert(wf): |
| 62 | + time, is_now = parser_query(wf) |
| 63 | + return create_feedback(time, is_now) |
0 commit comments