def d2w(x): # 2018-05-20 if isinstance(x, str): x = s2d(x) y = x.isocalendar() # ISOyear, ISOweeknumber, ISOweekday wochennummer = y[1] wochentag = y[2] - 1 # Wochentag beginnt aus Montag(1), Sontag ist 7 x = x - dl(days=wochentag) # res = x.strftime('%4d-%02d-%02d-w' % (x.year, x.month, x.day)) res += str(wochennummer) return res
def tplus(t1, t2): assert (isinstance(t1, tm)) assert (isinstance(t2, tm)) diff = dl(hours=t2.hour, minutes=t2.minute, seconds=t2.second, microseconds=t2.microsecond) z = dt.combine(dy.today(), t1) z += diff return z.time()
def p2q(x): if x.minute in [0, 15, 30, 45]: return x min = x.minute q = min // 15 r = min % 15 if r < 8: min = q * 15 + 0 else: min = q * 15 + 15 diff = min - x.minute y = x + dl(minutes=diff) return y
def q2d(w, fy=12): assert fy in range(1, 13) if not is_q(w): return w j = int(w[0:4]) q = int(w[-1]) d = calendar.monthrange(j, fy)[1] tag = dy(j, fy, d) # if q == 4: d = 0 elif q == 3: d = 31 * 3 * 1 elif q == 2: d = 31 * 3 * 2 elif q == 1: d = 31 * 3 * 3 tag -= dl(days=d) # d = calendar.monthrange(tag.year, tag.month)[1] tag = dy(tag.year, tag.month, d) return tag
def min2strt(x): y = dt(1,1,1,0,0) + dl(minutes=x) y = str(y)[-8:-3] # print( y ) return y
def utc2jst(zt): # x = zt.replace(tzinfo=dateutil.tz.tzlocal()) x = zt.astimezone(tz.utc) # print( x.tzinfo ) #d x = x.astimezone(tz(dl(hours=+9))) return x
def tzconv(zt, des='JST', aux='UTC'): if aux == 'JST': x = zt.astimezone(tz(dl(hours=+9))) return x
def s2z(x, zeichnis=''): # assert( str, type(x) ) ### DELTA ### d4dl = re.match('^(\d+)D', x) if d4dl: d4dl = d4dl.group(1) d4dl = int(d4dl) # ex. 1950/09/09 if re.match('^\d{4}/\d{2}/\d{2}$', x): x = dt.strptime(x, '%Y/%m/%d') if zeichnis == '' or zeichnis == 'd': return dy(x.year, x.month, x.day) # ex. 1955-03-07 elif re.match('^\d{4}-\d{2}-\d{2}$', x): x = dt.strptime(x, '%Y-%m-%d') if zeichnis == '' or zeichnis == 'd': return dy(x.year, x.month, x.day) # ex. 18:39 elif re.match('^\d{2}:\d{2}$', x): x = dt.strptime(x, '%H:%M') if zeichnis == '' or zeichnis == 't': return tm(x.hour, x.minute, 0) # ex. 18:10:40 elif re.match('^\d{2}:\d{2}:\d{2}$', x): x = dt.strptime(x, '%H:%M:%S') if zeichnis == '' or zeichnis == 't': return tm(x.hour, x.minute, x.second) # ex. 18:10.40 elif re.match('^\d{2}:\d{2}\.\d+$', x): x = dt.strptime(x, '%M:%S.%f') return tm(0, x.minute, x.second, x.microsecond) # ex. 1983-01-22 18:40:15 elif re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}$', x): x = dt.strptime(x, '%Y-%m-%d %H:%M:%S') if zeichnis == '' or zeichnis == 'p': return dt(x.year, x.month, x.day, x.hour, x.minute, x.second) # ex. 1989-11-07 18:40:15 elif re.match('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$', x): x = dt.strptime(x, '%Y-%m-%d %H:%M:%S') if zeichnis == '' or zeichnis == 'p': return dt(x.year, x.month, x.day, x.hour, x.minute, x.second) # ex. 1989-11-07 18:40:15.500189 elif re.match('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?$', x): x = re.sub('\.\d+$', '', x) x = dt.strptime(x, '%Y-%m-%d %H:%M:%S') if zeichnis == '' or zeichnis == 'p': return dt(x.year, x.month, x.day, x.hour, x.minute, x.second) ### 2017-09-16 ### # ex. 19800528 elif re.match('^\d{4}\d{2}\d{2}$', x): x = dt.strptime(x, '%Y%m%d') if zeichnis == '' or zeichnis == 'd': return dy(x.year, x.month, x.day) elif re.match('^\d{2}\d{2}\d{2}$', x): if int(x[0:2]) >= 80: w = '19' else: w = '20' x = dt.strptime(w + x, '%Y%m%d') if zeichnis == '' or zeichnis == 'd': return dy(x.year, x.month, x.day) # ex. 0909 elif re.match('^[01]\d[0-3]\d$', x): x = dt.strptime(x, '%m%d') if zeichnis == '' or zeichnis == 'd': return dy(dy.today().year, x.month, x.day) ### 2018-11-04 ### elif re.match('^\d+D \d{1,2}:\d{1,2}:\d{1,2}$', x): x = dt.strptime(x, '%dD %H:%M:%S') x = dl(days=d4dl, hours=x.hour, minutes=x.minute, seconds=x.second) return x # elif re.match('^\d+D \d{1,2}:\d{1,2}$', x): x = dt.strptime(x, '%dD %H:%M') x = dl(days=d4dl, hours=x.hour, minutes=x.minute) return x return x
def t2k(x): return dl(hours=x.hour, minutes=x.minute)