-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
283 lines (242 loc) · 9.52 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import enum
import itertools
import re
from datetime import UTC, datetime, timedelta, timezone
from pathlib import Path
import pytz
from albert import ( # pylint: disable=import-error
Action,
PluginInstance,
StandardItem,
TriggerQueryHandler,
setClipboardText,
)
md_iid = '2.3'
md_version = '1.3'
md_name = 'DateTime Steven'
md_description = 'Convert between datetime strings and timestamps'
md_url = 'https://github.com/stevenxxiu/albert_datetime_steven'
md_maintainers = '@stevenxxiu'
md_lib_dependencies = ['pytz']
ICON_URL = f'file:{Path(__file__).parent / "icons/datetime.png"}'
UNITS = ['seconds', 'milliseconds', 'microseconds', 'nanoseconds']
UNITS_ABBREV = ['s', 'ms', 'us', 'ns']
UNIX_EPOCH = datetime.fromtimestamp(0, tz=UTC)
class TimeStr(enum.IntEnum):
DATE = enum.auto()
NTFS_DATE = enum.auto()
UNIX_TIMESTAMP = enum.auto()
NTFS_TIMESTAMP = enum.auto()
def guess_unix_unit(timestamp: int, max_year: int = 9999) -> int:
'''
:param timestamp:
:param max_year: Find the smallest resolution we can so `timestamp` is before this.
:return: `power`
'''
max_dt = datetime(max_year, 12, 31, tzinfo=UTC)
for power in 0, 3, 6, 9:
seconds = timestamp // 10**power
try:
dt = datetime.fromtimestamp(seconds, tz=UTC)
if dt <= max_dt or power == 9:
return power
except (ValueError, OverflowError, OSError):
continue
raise ValueError('datetime value out of range')
def parse_unix_timestamp(timestamp: int, power: int) -> tuple[datetime, int, str]:
seconds = timestamp // 10**power
dt = datetime.fromtimestamp(seconds, tz=UTC)
nanoseconds = 10 ** (9 - power) * (timestamp % (10**power))
unit = UNITS[power // 3]
return dt, nanoseconds, unit
LOCAL_TZINFO = datetime.now().astimezone().tzinfo
def format_unix_timestamp(dt: datetime, nanoseconds: int) -> list[str]:
fmt = f'%Y-%m-%d %H:%M:%S:{nanoseconds:09d} %z'
return [
dt.astimezone(LOCAL_TZINFO).strftime(fmt),
dt.astimezone(UTC).strftime(fmt),
]
def to_unix_timestamp(dt: datetime, nanoseconds: int) -> int:
return int(dt.timestamp()) * 10**9 + nanoseconds
NFTS_EPOCH = datetime(1601, 1, 1, tzinfo=UTC)
def parse_ntfs_timestamp(timestamp: int) -> tuple[datetime, int]:
ticks = timestamp % 10**7
seconds = timestamp // 10**7
dt = NFTS_EPOCH + timedelta(seconds=seconds)
return dt, ticks
def format_ntfs_timestamp(dt: datetime, ticks: int) -> list[str]:
fmt = f'%Y-%m-%d %H:%M:%S:{ticks:07d} %z'
return [
dt.astimezone(LOCAL_TZINFO).strftime(fmt),
dt.astimezone(UTC).strftime(fmt),
]
def to_ntfs_timestamp(dt: datetime, nanoseconds: int) -> int:
return int((dt - NFTS_EPOCH).total_seconds()) * 10**7 + nanoseconds // 100
class Plugin(PluginInstance, TriggerQueryHandler):
def __init__(self):
TriggerQueryHandler.__init__(
self,
id=__name__,
name=md_name,
description=md_description,
synopsis='(NT|NTFS|LDAP) <v>|<v>[unit]|<%Y-%m-%d [%H:%M:%S:[%NS|%NTFS_TICKS]] [%z]>',
defaultTrigger='dt ',
)
PluginInstance.__init__(self)
@staticmethod
def add_items(dt: datetime, nanoseconds: int, input_type: str, types: list[TimeStr], query) -> None:
item_defs = []
for timestamp_type in types:
match timestamp_type:
case TimeStr.DATE:
item_defs.extend(zip(format_unix_timestamp(dt, nanoseconds), itertools.repeat('Date')))
case TimeStr.NTFS_DATE:
item_defs.extend(
zip(format_ntfs_timestamp(dt, nanoseconds // 100), itertools.repeat('NTFS/LDAP date'))
)
case TimeStr.UNIX_TIMESTAMP:
item_defs.append((str(to_unix_timestamp(dt, nanoseconds)), 'Unix timestamp'))
case TimeStr.NTFS_TIMESTAMP:
item_defs.append((str(to_ntfs_timestamp(dt, nanoseconds)), 'NTFS/LDAP timestamp'))
for output_str, output_str_type in item_defs:
query.add(
StandardItem(
id=f'{md_name}/{output_str}',
text=output_str,
subtext=f'{output_str_type} (input as {input_type})',
iconUrls=[ICON_URL],
actions=[Action(md_name, 'Copy', lambda value_=output_str: setClipboardText(value_))],
)
)
# Copy all
headings = [output_str_type for _output_str, output_str_type in item_defs]
max_heading_len = max(len(heading) for heading in headings)
all_output_str = (
f'With input as {input_type}\n'
+ '\n'.join(
[
f'{heading:<{max_heading_len}} {output_str}'
for heading, (output_str, _output_str_type) in zip(headings, item_defs)
]
)
+ '\n'
)
query.add(
StandardItem(
id=f'{md_name}/copy_all',
text='Copy All',
iconUrls=[ICON_URL],
actions=[Action(md_name, 'Copy', lambda: setClipboardText(all_output_str))],
)
)
@classmethod
def parse_epoch(cls, query_str: str, query) -> bool:
try:
matches = re.match(r'(?:NT|NTFS|LDAP)\s+(\d+)$', query_str, re.IGNORECASE)
if matches:
(timestamp_str,) = matches.groups()
timestamp = int(timestamp_str)
dt, ticks = parse_ntfs_timestamp(timestamp)
cls.add_items(
dt,
ticks * 100,
'NTFS time in 100 nanoseconds',
[TimeStr.NTFS_DATE, TimeStr.DATE, TimeStr.UNIX_TIMESTAMP],
query,
)
return True
matches = re.match(r'(\d+)\s*(s|ms|us|ns)?$', query_str)
if matches:
timestamp_str, unit_abbrev = matches.groups()
timestamp = int(timestamp_str)
if unit_abbrev:
power = 3 * UNITS_ABBREV.index(unit_abbrev)
else:
power = guess_unix_unit(timestamp)
dt, nanoseconds, unit = parse_unix_timestamp(timestamp, power)
cls.add_items(
dt,
nanoseconds,
f'Unix time in {unit}',
[TimeStr.DATE, TimeStr.NTFS_DATE, TimeStr.UNIX_TIMESTAMP, TimeStr.NTFS_TIMESTAMP],
query,
)
return True
except (OverflowError, ValueError) as e:
query.add(
StandardItem(
id=f'{md_name}/{e}',
text=str(e),
iconUrls=[ICON_URL],
)
)
return True
return False
RE_DATETIME: re.Pattern = re.compile(
# Date
r'(?P<year>\d{1,4})-(?P<month>\d{2})-(?P<day>\d{2})'
# Time
r'(?:\s+'
r'(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(:(?:(?P<nanosecond>\d{9})|(?P<ntfs_ticks>\d{7})))?'
r')?'
# Timezone
r'(?:\s+(?:'
r'((?P<tz_fixed_sign>[+-])(?P<tz_fixed_hours>\d{2}):?(?P<tz_fixed_minutes>\d{2}))|'
fr'(?P<tz_named>{"|".join(timezone for timezone in pytz.all_timezones)})'
r'))?',
re.IGNORECASE,
)
@classmethod
def parse_datetime(cls, query_str: str, query) -> bool:
matches = cls.RE_DATETIME.match(query_str)
if not matches:
return False
matches_dict = matches.groupdict()
dt = datetime(
int(matches_dict['year']),
int(matches_dict['month']),
int(matches_dict['day']),
)
if matches_dict['hour'] is not None:
dt = dt.replace(
hour=int(matches_dict['hour']), minute=int(matches_dict['minute']), second=int(matches_dict['second'])
)
nanoseconds = 0
if matches_dict['nanosecond'] is not None:
nanoseconds = int(matches_dict['nanosecond'])
elif matches_dict['ntfs_ticks'] is not None:
nanoseconds = int(matches_dict['ntfs_ticks']) * 100
if matches_dict['tz_fixed_sign'] is not None:
input_timezone = timedelta(
hours=int(matches_dict['tz_fixed_hours']), minutes=int(matches_dict['tz_fixed_minutes'])
)
if matches_dict['tz_fixed_sign'] == '-':
input_timezone = -input_timezone
dt = dt.astimezone(timezone(input_timezone))
elif matches_dict['tz_named'] is not None:
dt = pytz.timezone(matches_dict['tz_named']).localize(dt)
else:
dt = dt.replace(tzinfo=UTC)
if matches_dict['ntfs_ticks'] is not None:
cls.add_items(
dt,
nanoseconds,
'date',
[TimeStr.NTFS_TIMESTAMP, TimeStr.UNIX_TIMESTAMP, TimeStr.NTFS_DATE, TimeStr.DATE],
query,
)
else:
cls.add_items(
dt,
nanoseconds,
'date',
[TimeStr.UNIX_TIMESTAMP, TimeStr.NTFS_TIMESTAMP, TimeStr.DATE, TimeStr.NTFS_DATE],
query,
)
return True
def handleTriggerQuery(self, query) -> None:
query_str = query.string.strip()
if self.parse_epoch(query_str, query):
return
if self.parse_datetime(query_str, query):
return