def sj2d(x): gengou = '(明治|大正|昭和|平成|令和|民國)' jmt = '\s*(\d{1,3})年\s*(\d{1,3})月\s*(\d{1,3})日' m = re.findall(gengou + jmt, x) if len(m) == 0: gengou = '' jmt = '()(\d{4})年(\d{1,3})月(\d{1,3})日' m = re.findall(gengou + jmt, x) if len(m) == 0: return x res = [] for n in m: if gengou == '': jr = int(n[1]) else: jr = g2y(n[0]) jr = jr + int(n[1]) mn = int(n[2]) tg = int(n[3]) tg = dy(jr, mn, tg) res.append(tg) if len(res) == 1: return res[0] else: return res
def dateiheute(w): import os if os.path.exists(w): t = os.path.getmtime(w) t = u2p(t) t = str(dy(t.year, t.month, t.day)) if t == heute(): return True return False
def sg2d(x): if not is_sg(x): return x w = re.findall('^([MTSHR])(\d{1,2})\.(1?\d)\.([1-3]?\d)$', x) g = g2y(w[0][0]) j = int(w[0][1]) m = int(w[0][2]) t = int(w[0][3]) w = dy(g + j, m, t) return w
def n2d(m, ende=True): assert is_n(m) j = m // 100 m = m % 100 if ende == True: t = calendar.monthrange(j, m)[1] elif ende == False: t = 1 return dy(j, m, t)
def d2sg(x): g = '' y2 = 0 y = x.year m = x.month d = x.day # ## Transitional ## if y == 2019: if x < dy(2019, 5, 1): g = 'H' y2 = 1988 else: g = 'R' y2 = 2018 elif y == 1926: if x < dy(1926, 7, 31): g = 'T' y2 = 1911 else: g = 'S' y2 = 1925 # # Simple else: if y > 2018: g = 'R' y2 = 2018 elif y > 1988: g = 'H' y2 = 1988 elif y > 1925: g = 'S' y2 = 1925 elif y > 1911: g = 'T' y2 = 1911 elif y > 1868: g = 'M' y2 = 1868 y3 = y - y2 # print( y,y2,y3 ) #d z = '%s%d.%d.%d' % (g, y3, m, d) return z
def is_me(x): # Monatsende if isinstance(x, str): x = s2z(x) assert (isinstance(x, dy)) y = calendar.monthrange(x.year, x.month)[1] y = dy(x.year, x.month, y) # print( x ) #d # print( y ) #d if x == y: return True else: return False
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 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 u2d(x): x = u2p(x) x = dy(x.year, x.month, x.day) return x
def x2d(x): if x == None: return x x = dy(x.year, x.month, x.day) return x
def p2d(x): return dy(x.year, x.month, x.day)