forked from Delgan/loguru
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Pendulum dependencie with custom datetime subclass
- Loading branch information
Showing
14 changed files
with
353 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from datetime import datetime as datetime_, timezone, timedelta | ||
from calendar import day_name, day_abbr, month_name, month_abbr | ||
from time import time, localtime | ||
import re | ||
|
||
|
||
tokens = r"H{1,2}|h{1,2}|m{1,2}|s{1,2}|S{1,6}|YYYY|YY|M{1,4}|D{1,4}|Z{1,2}|zz|A|X|x|E|Q|dddd|ddd|d" | ||
|
||
pattern = re.compile(r"(?:{0})|\[(?:{0})\]".format(tokens)) | ||
|
||
|
||
def now(): | ||
t = time() | ||
local = localtime(t) | ||
microsecond = int(abs(t) * 1e6 % 1e6) | ||
tzinfo = timezone(timedelta(seconds=local.tm_gmtoff), local.tm_zone) | ||
year, month, day, hour, minute, second, _, _, _ = local | ||
return datetime(year, month, day, hour, minute, second, microsecond, tzinfo) | ||
|
||
|
||
class datetime(datetime_): | ||
|
||
def __format__(self, spec): | ||
if not spec: | ||
spec = "%Y-%m-%dT%H:%M:%S.%f%z" | ||
|
||
if '%' in spec: | ||
return super().__format__(spec) | ||
|
||
year, month, day, hour, minute, second, weekday, yearday, _ = self.timetuple() | ||
microsecond = self.microsecond | ||
timestamp = self.timestamp() | ||
tzinfo = self.tzinfo or timezone(timedelta(seconds=0)) | ||
offset = tzinfo.utcoffset(self).total_seconds() | ||
sign = ('-', '+')[offset >= 0] | ||
h, m = divmod(abs(offset // 60), 60) | ||
|
||
rep = { | ||
'YYYY': '%04d' % year, | ||
'YY': '%02d' % (year % 100), | ||
'Q': '%d' % ((month - 1) // 3 + 1), | ||
'MMMM': month_name[month - 1], | ||
'MMM': month_abbr[month - 1], | ||
'MM': '%02d' % month, | ||
'M': '%d' % month, | ||
'DDDD': '%03d' % yearday, | ||
'DDD': '%d' % yearday, | ||
'DD': '%02d' % day, | ||
'D': '%d' % day, | ||
'dddd': day_name[weekday], | ||
'ddd': day_abbr[weekday], | ||
'd': '%d' % weekday, | ||
'E': '%d' % (weekday + 1), | ||
'HH': '%02d' % hour, | ||
'H': '%d' % hour, | ||
'hh': '%02d' % ((hour - 1) % 12 + 1), | ||
'h': '%d' % ((hour - 1) % 12 + 1), | ||
'mm': '%02d' % minute, | ||
'm': '%d' % minute, | ||
'ss': '%02d' % second, | ||
's': '%d' % second, | ||
'S': '%d' % (microsecond // 100000), | ||
'SS': '%02d' % (microsecond // 10000), | ||
'SSS': '%03d' % (microsecond // 1000), | ||
'SSSS': '%04d' % (microsecond // 100), | ||
'SSSSS': '%05d' % (microsecond // 10), | ||
'SSSSSS': '%06d' % microsecond, | ||
'A': ('AM', 'PM')[hour // 12], | ||
'Z': '%s%02d:%02d' % (sign, h, m), | ||
'ZZ': '%s%02d%02d' % (sign, h, m), | ||
'zz': tzinfo.tzname(self) or '', | ||
'X': '%d' % timestamp, | ||
'x': '%d' % (int(timestamp) * 1000000 + microsecond), | ||
} | ||
|
||
def get(m): | ||
try: | ||
return rep[m[0]] | ||
except KeyError: | ||
return m[0][1:-1] | ||
|
||
return pattern.sub(get, spec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.