|
2 | 2 | # encoding: utf-8 |
3 | 3 |
|
4 | 4 |
|
| 5 | +import re |
| 6 | + |
5 | 7 | import arrow |
6 | | -from dateutil import tz |
7 | | -from workflow import ICON_CLOCK, ICON_NOTE |
| 8 | +from workflow import ICON_CLOCK, ICON_NOTE, ICON_ERROR |
8 | 9 |
|
9 | 10 | FORMAT_LIST = ( |
10 | 11 | (ICON_NOTE, 'X', 'UTC Timestamp (s)'), |
|
24 | 25 | # FORMAT_RFC3339 |
25 | 26 | ) |
26 | 27 |
|
| 28 | +RE_TIMEZONE = '^[+-][0-9]{2}$' |
| 29 | +RE_SHIFT = '^[+-][0-9]+[smhdmy]$' |
| 30 | + |
| 31 | + |
| 32 | +class Time(object): |
| 33 | + wf = None |
| 34 | + _query = None |
| 35 | + |
| 36 | + time = None |
| 37 | + now = False |
| 38 | + |
| 39 | + zone = None |
| 40 | + shift = None |
| 41 | + |
| 42 | + def __init__(self, wf): |
| 43 | + self.wf = wf |
| 44 | + |
| 45 | + @property |
| 46 | + def query(self): |
| 47 | + return self._query |
| 48 | + |
| 49 | + @query.setter |
| 50 | + def query(self, value): |
| 51 | + self._query = value.strip(' ') |
| 52 | + |
| 53 | + def do_parser(self): |
| 54 | + self.wf.logger.debug('query string:{} {}'.format( |
| 55 | + type(self.wf.args[0]), self.wf.args[0]) |
| 56 | + ) |
| 57 | + |
| 58 | + try: |
| 59 | + self.query = self.wf.args[0].encode('utf8') |
| 60 | + except IndexError: |
| 61 | + self.wf.logger.debug('parser workflow args failed.') |
| 62 | + return False |
27 | 63 |
|
28 | | -def parser_query(wf): |
29 | | - """parser datetime, timezone, shift""" |
30 | | - try: |
31 | | - query = wf.args[0].encode('utf8').strip(' ').rstrip(' ') |
| 64 | + while True: |
| 65 | + if not self._parser_extend_info(): |
| 66 | + break |
32 | 67 |
|
33 | | - if query.isdigit(): |
34 | | - query = int(query) |
| 68 | + return self._parser_datetime() |
35 | 69 |
|
36 | | - wf.logger.debug('query string:{} {}'.format(type(query), query)) |
37 | | - return arrow.get(query), False |
| 70 | + def _parser_extend_info(self): |
| 71 | + """parser timezone, shift""" |
| 72 | + index = self.query.rfind(' ') |
| 73 | + if index == -1: |
| 74 | + query = '' |
| 75 | + info = self.query |
| 76 | + else: |
| 77 | + query = self.query[:index] |
| 78 | + info = self.query[index + 1:].strip(' ') |
38 | 79 |
|
39 | | - except (IndexError, arrow.ParserError): |
40 | | - wf.logger.debug('args:{}'.format(wf.args)) |
41 | | - return arrow.get(), True |
| 80 | + # time zone |
| 81 | + if info.upper() == 'UTC' or info == '+00' or info == '-00': |
| 82 | + self.query = query |
| 83 | + self.zone = 'UTC' |
| 84 | + self.wf.logger.debug('found zone info:'.format(self.zone)) |
| 85 | + return True |
42 | 86 |
|
| 87 | + r = re.match(RE_TIMEZONE, info) |
| 88 | + if r: |
| 89 | + self.query = query |
| 90 | + self.zone = info |
| 91 | + self.wf.logger.debug('found zone info:'.format(self.zone)) |
| 92 | + return True |
43 | 93 |
|
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) |
| 94 | + # time shift TODO |
49 | 95 |
|
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 | | - }) |
| 96 | + return False |
57 | 97 |
|
58 | | - return f |
| 98 | + def _parser_datetime(self): |
| 99 | + """parser datetime""" |
| 100 | + try: |
| 101 | + if self.query.isdigit(): |
| 102 | + self.time = arrow.get(int(self.query)) |
| 103 | + else: |
| 104 | + self.time = arrow.get(self.query) |
| 105 | + |
| 106 | + return True |
| 107 | + |
| 108 | + except arrow.ParserError: |
| 109 | + self.wf.logger.debug( |
| 110 | + 'parser datetime error,query string:{}'.format(self.query) |
| 111 | + ) |
| 112 | + |
| 113 | + if self.query == 'now' or self.query == '': |
| 114 | + self.now = True |
| 115 | + self.time = arrow.now() |
| 116 | + return True |
| 117 | + |
| 118 | + return False |
| 119 | + |
| 120 | + def get_feedback(self): |
| 121 | + if self.time is None: |
| 122 | + return [{ |
| 123 | + 'title': 'Please enter timestamp, datetime string, "now", or space', |
| 124 | + 'subtitle': 'Examples: 1607609661, 2020-12-10 22:14:33, now +08', |
| 125 | + 'valid': False, |
| 126 | + 'icon': ICON_ERROR, |
| 127 | + }] |
| 128 | + |
| 129 | + f = list() |
| 130 | + for icon, fmt, desc in FORMAT_LIST: |
| 131 | + if self.now: |
| 132 | + subtitle = 'Now, {}'.format(desc) |
| 133 | + else: |
| 134 | + subtitle = desc |
| 135 | + |
| 136 | + if self.zone: |
| 137 | + self.time = self.time.to(self.zone[:3]) |
| 138 | + subtitle = '[{}]{}'.format(self.zone, subtitle) |
| 139 | + else: |
| 140 | + self.time = self.time.to('local') |
| 141 | + |
| 142 | + value = self.time.format(fmt) |
| 143 | + f.append({ |
| 144 | + 'title': value, |
| 145 | + 'subtitle': subtitle, |
| 146 | + 'valid': True, |
| 147 | + 'arg': value, |
| 148 | + 'icon': icon, |
| 149 | + }) |
| 150 | + |
| 151 | + return f |
59 | 152 |
|
60 | 153 |
|
61 | 154 | def do_convert(wf): |
62 | | - time, is_now = parser_query(wf) |
63 | | - return create_feedback(time, is_now) |
| 155 | + time = Time(wf) |
| 156 | + time.do_parser() |
| 157 | + |
| 158 | + return time.get_feedback() |
0 commit comments