def parseDateTime(str0_ : str) -> datetime.datetime: if (Utils.isNullOrEmpty(str0_)): return None try: prts = Utils.splitString(str0_, '.', False) wrapy804 = RefOutArgWrapper(0) inoutres805 = Utils.tryParseInt(prts[0], wrapy804) y = wrapy804.value if (not inoutres805): return None mon = 0 day = 0 if (len(prts) > 1): wrapmon802 = RefOutArgWrapper(0) inoutres803 = Utils.tryParseInt(prts[1], wrapmon802) mon = wrapmon802.value if (inoutres803): if (len(prts) > 2): wrapday801 = RefOutArgWrapper(0) Utils.tryParseInt(prts[2], wrapday801) day = wrapday801.value if (mon <= 0): mon = 1 if (day <= 0): day = 1 if (day > Utils.lastDayOfMonth(y, mon)): day = Utils.lastDayOfMonth(y, mon) return datetime.datetime(y, mon, day, 0, 0, 0) except Exception as ex: pass return None
def parse_date_time(str0_: str) -> datetime.datetime: if (Utils.isNullOrEmpty(str0_)): return None try: prts = Utils.splitString(str0_, '.', False) y = 0 wrapy831 = RefOutArgWrapper(0) inoutres832 = Utils.tryParseInt(prts[0], wrapy831) y = wrapy831.value if (not inoutres832): return None mon = 0 day = 0 if (len(prts) > 1): wrapmon829 = RefOutArgWrapper(0) inoutres830 = Utils.tryParseInt(prts[1], wrapmon829) mon = wrapmon829.value if (inoutres830): if (len(prts) > 2): wrapday828 = RefOutArgWrapper(0) Utils.tryParseInt(prts[2], wrapday828) day = wrapday828.value if (mon <= 0): mon = 1 if (day <= 0): day = 1 if (day > Utils.lastDayOfMonth(y, mon)): day = Utils.lastDayOfMonth(y, mon) return datetime.datetime(y, mon, day, 0, 0, 0) except Exception as ex: pass return None
def generateDate(self, today: datetime.datetime, end_of_diap: bool) -> datetime.datetime: year_ = self.year if (year_ == 0): year_ = today.year mon = self.month1 if (mon == 0): mon = (12 if end_of_diap else 1) elif (end_of_diap and self.month2 > 0): mon = self.month2 day = self.day1 if (day == 0): day = (31 if end_of_diap else 1) elif (self.day2 > 0): day = self.day2 if (day > Utils.lastDayOfMonth(year_, mon)): day = Utils.lastDayOfMonth(year_, mon) return datetime.datetime(year_, mon, day, 0, 0, 0)
def dt(self) -> datetime.datetime: """ Дата в стандартной структуре .NET (null, если что-либо неопределено или дата некорректна) """ if (self.year > 0 and self.month > 0 and self.day > 0): if (self.month > 12): return None if (self.day > Utils.lastDayOfMonth(self.year, self.month)): return None h = self.hour m = self.minute s = self.second if (h < 0): h = 0 if (m < 0): m = 0 if (s < 0): s = 0 try: return datetime.datetime(self.year, self.month, self.day, h, m, (s if s >= 0 and (s < 60) else 0)) except Exception as ex: pass return None
def tryCreate(list0_: typing.List['DateExItemToken'], today: datetime.datetime, tense: int) -> 'DateValues': oo = False if (list0_ is not None): for v in list0_: if (v.typ != DateExToken.DateExItemTokenType.HOUR and v.typ != DateExToken.DateExItemTokenType.MINUTE): oo = True if (not oo): return DateExToken.DateValues._new639(today.year, today.month, today.day) res = DateExToken.DateValues() i = 0 has_rel = False if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.YEAR): it = list0_[i] if (not it.is_value_relate): res.year = it.value else: res.year = (today.year + it.value) has_rel = True i += 1 if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.QUARTAL): it = list0_[i] v = 0 if (not it.is_value_relate): if (res.year == 0): v0 = 1 + ((math.floor(((today.month - 1)) / 4))) if (it.value > v0 and (tense < 0)): res.year = (today.year - 1) elif ((it.value < v0) and tense > 0): res.year = (today.year + 1) else: res.year = today.year v = it.value else: if (res.year == 0): res.year = today.year v = (1 + ((math.floor( ((today.month - 1)) / 4))) + it.value) while v > 4: v -= 4 res.year += 1 while v <= 0: v += 4 res.year -= 1 res.month1 = ((((v - 1)) * 4) + 1) res.month2 = (res.month1 + 3) return res if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.MONTH): it = list0_[i] if (not it.is_value_relate): if (res.year == 0): if (it.value > today.month and (tense < 0)): res.year = (today.year - 1) elif ((it.value < today.month) and tense > 0): res.year = (today.year + 1) else: res.year = today.year res.month1 = it.value else: has_rel = True if (res.year == 0): res.year = today.year v = today.month + it.value while v > 12: v -= 12 res.year += 1 while v <= 0: v += 12 res.year -= 1 res.month1 = v i += 1 if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.DAY): it = list0_[i] if (not it.is_value_relate): res.day1 = it.value if (res.month1 == 0): if (res.year == 0): res.year = today.year if (it.value > today.day and (tense < 0)): res.month1 = (today.month - 1) if (res.month1 <= 0): res.month1 = 12 res.year -= 1 elif ((it.value < today.day) and tense > 0): res.month1 = (today.month + 1) if (res.month1 > 12): res.month1 = 1 res.year += 1 else: res.month1 = today.month else: has_rel = True if (res.year == 0): res.year = today.year if (res.month1 == 0): res.month1 = today.month v = today.day + it.value while v > Utils.lastDayOfMonth(res.year, res.month1): v -= Utils.lastDayOfMonth(res.year, res.month1) res.month1 += 1 if (res.month1 > 12): res.month1 = 1 res.year += 1 while v <= 0: res.month1 -= 1 if (res.month1 <= 0): res.month1 = 12 res.year -= 1 v += Utils.lastDayOfMonth(res.year, res.month1) res.day1 = v i += 1 if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.DAYOFWEEK): it = list0_[i] return res
def getDatesOld(self, now: datetime.datetime, from0_: datetime.datetime, to: datetime.datetime, can_be_future: bool = False) -> bool: yfrom = None yto = None yfrom_def = False yto_def = False qfrom = None qto = None mfrom = None mto = None dfrom = None dto = None hfrom = None hto = None min_from = None min_to = None for k in range(2): its = (self.items_from if k == 0 else self.items_to) i = 0 v = 0 if ((i < len(its)) and its[i].typ == DateExToken.DateExItemTokenType.YEAR): if (not its[i].is_value_relate): v = its[i].value else: v = (now.year + its[i].value) i += 1 if (k == 0): yfrom = v else: yto = v if (k == 0): yfrom_def = True else: yto_def = True if ((i < len(its)) and its[i].typ == DateExToken.DateExItemTokenType.QUARTAL): if (not its[i].is_value_relate): v = its[i].value else: v = (1 + ((math.floor( ((now.month - 1)) / 4))) + its[i].value) i = len(its) if (k == 0): qfrom = v else: qto = v if ((i < len(its)) and its[i].typ == DateExToken.DateExItemTokenType.MONTH): if (not its[i].is_value_relate): v = its[i].value else: v = (now.month + its[i].value) i += 1 if (k == 0): mfrom = v else: mto = v if ((i < len(its)) and its[i].typ == DateExToken.DateExItemTokenType.DAY): if (not its[i].is_value_relate): v = its[i].value else: v = (now.day + its[i].value) i += 1 if (k == 0): dfrom = v else: dto = v if ((i < len(its)) and its[i].typ == DateExToken.DateExItemTokenType.DAYOFWEEK): v = its[i].value if (its[i].value < 0): v = (-v) ddd = now while True: if (ddd.weekday() == 0): ddd = (ddd + datetime.timedelta(days=-7)) if (v > 1): ddd = (ddd + datetime.timedelta(days=v - 1)) if (k == 0): yfrom = ddd.year mfrom = ddd.month dfrom = ddd.day else: yto = ddd.year mto = ddd.month dto = ddd.day break ddd = (ddd + datetime.timedelta(days=-1)) else: dow = now.weekday() if (dow == 0): dow = 7 ddd = now if (v > dow): ddd = (ddd + datetime.timedelta(days=v - dow)) else: if (can_be_future): ddd = (ddd + datetime.timedelta(days=7)) if (dow > v): ddd = (ddd + datetime.timedelta(days=v - dow)) if (k == 0): yfrom = ddd.year mfrom = ddd.month dfrom = ddd.day else: yto = ddd.year mto = ddd.month dto = ddd.day i += 1 if ((i < len(its)) and its[i].typ == DateExToken.DateExItemTokenType.HOUR): if (not its[i].is_value_relate): v = its[i].value else: v = (now.hour + its[i].value) i += 1 if (k == 0): hfrom = v else: hto = v if ((i < len(its)) and its[i].typ == DateExToken.DateExItemTokenType.MINUTE): if (not its[i].is_value_relate and len(its) > 1): v = its[i].value else: v = (now.minute + its[i].value) i += 1 if (k == 0): min_from = v else: min_to = v if (yfrom is None): if (yto is None): yto = now.year yfrom = yto else: yfrom = yto elif (yto is None): yto = yfrom if (qfrom is None and dfrom is None): qfrom = qto elif (qto is None and dto is None): qto = qfrom if (qfrom is not None): mfrom = (1 + (((qfrom - 1)) * 4)) if (qto is not None): mto = ((qto * 4) - 1) if (mfrom is None and (((dfrom is not None or mfrom is not None or hfrom is not None) or min_from != 0 or len(self.items_from) == 0))): if (mto is not None): mfrom = mto else: mto = now.month mfrom = mto if (mto is None and (((dto is not None or mto is not None or hto is not None) or min_to is not None or len(self.items_to) == 0))): if (mfrom is not None): mto = mfrom else: mto = now.month mfrom = mto if (mfrom is not None): while mfrom > 12: yfrom += 1 mfrom -= 12 while mfrom <= 0: yfrom -= 1 mfrom += 12 else: mfrom = 1 if (mto is not None): while mto > 12: yto += 1 mto -= 12 while mto <= 0: yto -= 1 mto += 12 else: mto = 12 if (dfrom is None and ((hfrom is not None or min_from is not None or len(self.items_from) == 0))): if (dto is not None): dfrom = dto else: dto = now.day dfrom = dto if (dto is None and ((hto is not None or min_to is not None or len(self.items_to) == 0))): if (dfrom is not None): dto = dfrom else: dto = now.day dfrom = dto if (dfrom is not None): while dfrom > Utils.lastDayOfMonth(yfrom, mfrom): dfrom -= Utils.lastDayOfMonth(yfrom, mfrom) mfrom += 1 if (mfrom > 12): mfrom = 1 yfrom += 1 while dfrom <= 0: mfrom -= 1 if (mfrom <= 0): mfrom = 12 yfrom -= 1 dfrom += Utils.lastDayOfMonth(yfrom, mfrom) if (dto is not None): while dto > Utils.lastDayOfMonth(yto, mto): dto -= Utils.lastDayOfMonth(yto, mto) mto += 1 if (mto > 12): mto = 1 yto += 1 while dto <= 0: mto -= 1 if (mto <= 0): mto = 12 yto -= 1 dto += Utils.lastDayOfMonth(yto, mto) elif (dfrom is not None and mfrom == mto and yfrom == yto): dto = dfrom else: dto = Utils.lastDayOfMonth(yto, mto) if (dfrom is None): dfrom = 1 try: from0_.value = datetime.datetime(yfrom, mfrom, dfrom, 0, 0, 0) to.value = datetime.datetime(yto, mto, dto, 0, 0, 0) except Exception as ex: from0_.value = datetime.datetime(1, 1, 1, 0, 0, 0) to.value = datetime.datetime(1, 1, 1, 0, 0, 0) return False if ((not yfrom_def and len(self.items_from) > 0 and Utils.getDate( from0_.value) > Utils.getDate(datetime.datetime.today())) and not can_be_future): if (not yto_def and to.value.year == from0_.value.year): to.value = (to.value + datetime.timedelta(days=-1 * 365)) from0_.value = (from0_.value + datetime.timedelta(days=-1 * 365)) if (hfrom is None and hto is not None): hfrom = hto elif (hto is None and hfrom is not None): hto = hfrom if (min_from is None and min_to is not None): min_from = min_to elif (min_to is None and min_from is not None): min_to = min_from if (hfrom is not None or min_from is not None): if (hfrom is None): hfrom = now.hour if (min_from is None): min_from = 0 while min_from >= 60: hfrom += 1 min_from -= 60 while min_from < 0: hfrom -= 1 min_from += 60 while hfrom >= 24: from0_.value = (from0_.value + datetime.timedelta(days=1)) hfrom -= 24 while hfrom < 0: from0_.value = (from0_.value + datetime.timedelta(days=-1)) hfrom += 24 from0_.value = ((from0_.value + datetime.timedelta(hours=hfrom)) + datetime.timedelta(minutes=min_from)) if (hto is not None or min_to is not None): if (hto is None): hto = now.hour if (min_to is None): min_to = 0 while min_to >= 60: hto += 1 min_to -= 60 while min_to < 0: hto -= 1 min_to += 60 while hto >= 24: to.value = (to.value + datetime.timedelta(days=1)) hto -= 24 while hto < 0: to.value = (to.value + datetime.timedelta(days=-1)) hto += 24 to.value = ((to.value + datetime.timedelta(hours=hto)) + datetime.timedelta(minutes=min_to)) return True
def try_create(list0_: typing.List['DateExItemToken'], today: datetime.datetime, tense: int) -> 'DateValues': oo = False if (list0_ is not None): for v in list0_: if (v.typ != DateExToken.DateExItemTokenType.HOUR and v.typ != DateExToken.DateExItemTokenType.MINUTE): oo = True if (not oo): return DateExToken.DateValues._new606(today.year, today.month, today.day) if (list0_ is None or len(list0_) == 0): return None j = 0 while j < len(list0_): if (list0_[j].typ == DateExToken.DateExItemTokenType.DAYOFWEEK ): if (j > 0 and list0_[j - 1].typ == DateExToken.DateExItemTokenType.WEEK): break we = DateExToken.DateExItemToken._new607( list0_[j].begin_token, list0_[j].end_token, DateExToken.DateExItemTokenType.WEEK, True) if (list0_[j].is_value_relate): list0_[j].is_value_relate = False if (list0_[j].value < 0): we.value = -1 list0_[j].value = (-list0_[j].value) list0_.insert(j, we) break j += 1 res = DateExToken.DateValues() it = None i = 0 has_rel = False if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.YEAR): it = list0_[i] if (not it.is_value_relate): res.year = it.value else: res.year = (today.year + it.value) has_rel = True i += 1 if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.QUARTAL): it = list0_[i] v = 0 if (not it.is_value_relate): if (res.year == 0): v0 = 1 + ((math.floor(((today.month - 1)) / 3))) if (it.value > v0 and (tense < 0)): res.year = (today.year - 1) elif ((it.value < v0) and tense > 0): res.year = (today.year + 1) else: res.year = today.year v = it.value else: if (res.year == 0): res.year = today.year v = (1 + ((math.floor( ((today.month - 1)) / 3))) + it.value) while v > 3: v -= 3 res.year += 1 while v <= 0: v += 3 res.year -= 1 res.month1 = ((((v - 1)) * 3) + 1) res.month2 = (res.month1 + 2) return res if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.MONTH): it = list0_[i] if (not it.is_value_relate): if (res.year == 0): if (it.value > today.month and (tense < 0)): res.year = (today.year - 1) elif ((it.value < today.month) and tense > 0): res.year = (today.year + 1) else: res.year = today.year res.month1 = it.value else: has_rel = True if (res.year == 0): res.year = today.year v = today.month + it.value while v > 12: v -= 12 res.year += 1 while v <= 0: v += 12 res.year -= 1 res.month1 = v i += 1 if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.WEEKEND and i == 0): it = list0_[i] has_rel = True if (res.year == 0): res.year = today.year if (res.month1 == 0): res.month1 = today.month if (res.day1 == 0): res.day1 = today.day dt0 = datetime.datetime(res.year, res.month1, res.day1, 0, 0, 0) dow = dt0.weekday() if (dow == 0): dt0 = (dt0 + datetime.timedelta(days=5)) elif (dow == 1): dt0 = (dt0 + datetime.timedelta(days=4)) elif (dow == 2): dt0 = (dt0 + datetime.timedelta(days=3)) elif (dow == 3): dt0 = (dt0 + datetime.timedelta(days=2)) elif (dow == 4): dt0 = (dt0 + datetime.timedelta(days=1)) elif (dow == 5): dt0 = (dt0 + datetime.timedelta(days=-1)) elif (dow == 6): pass if (it.value != 0): dt0 = (dt0 + datetime.timedelta(days=it.value * 7)) res.year = dt0.year res.month1 = dt0.month res.day1 = dt0.day dt0 = (dt0 + datetime.timedelta(days=1)) res.year = dt0.year res.month2 = dt0.month res.day2 = dt0.day i += 1 if (((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.WEEK and i == 0) and list0_[i].is_value_relate): it = list0_[i] has_rel = True if (res.year == 0): res.year = today.year if (res.month1 == 0): res.month1 = today.month if (res.day1 == 0): res.day1 = today.day dt0 = datetime.datetime(res.year, res.month1, res.day1, 0, 0, 0) dow = dt0.weekday() if (dow == 1): dt0 = (dt0 + datetime.timedelta(days=-1)) elif (dow == 2): dt0 = (dt0 + datetime.timedelta(days=-2)) elif (dow == 3): dt0 = (dt0 + datetime.timedelta(days=-3)) elif (dow == 4): dt0 = (dt0 + datetime.timedelta(days=-4)) elif (dow == 5): dt0 = (dt0 + datetime.timedelta(days=-5)) elif (dow == 6): dt0 = (dt0 + datetime.timedelta(days=-6)) if (it.value != 0): dt0 = (dt0 + datetime.timedelta(days=it.value * 7)) res.year = dt0.year res.month1 = dt0.month res.day1 = dt0.day dt0 = (dt0 + datetime.timedelta(days=6)) res.year = dt0.year res.month2 = dt0.month res.day2 = dt0.day i += 1 if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.DAY): it = list0_[i] if (not it.is_value_relate): res.day1 = it.value if (res.month1 == 0): if (res.year == 0): res.year = today.year if (it.value > today.day and (tense < 0)): res.month1 = (today.month - 1) if (res.month1 <= 0): res.month1 = 12 res.year -= 1 elif ((it.value < today.day) and tense > 0): res.month1 = (today.month + 1) if (res.month1 > 12): res.month1 = 1 res.year += 1 else: res.month1 = today.month else: has_rel = True if (res.year == 0): res.year = today.year if (res.month1 == 0): res.month1 = today.month v = today.day + it.value while v > Utils.lastDayOfMonth(res.year, res.month1): v -= Utils.lastDayOfMonth(res.year, res.month1) res.month1 += 1 if (res.month1 > 12): res.month1 = 1 res.year += 1 while v <= 0: res.month1 -= 1 if (res.month1 <= 0): res.month1 = 12 res.year -= 1 v += Utils.lastDayOfMonth(res.year, res.month1) res.day1 = v i += 1 if ((i < len(list0_)) and list0_[i].typ == DateExToken.DateExItemTokenType.DAYOFWEEK): it = list0_[i] if ((i > 0 and list0_[i - 1].typ == DateExToken.DateExItemTokenType.WEEK and it.value >= 1) and it.value <= 7): res.day1 = ((res.day1 + it.value) - 1) while res.day1 > Utils.lastDayOfMonth( res.year, res.month1): res.day1 -= Utils.lastDayOfMonth(res.year, res.month1) res.month1 += 1 if (res.month1 > 12): res.month1 = 1 res.year += 1 res.day2 = res.day1 res.month2 = res.month1 i += 1 return res