MCPcopy Create free account
hub / github.com/python/cpython / year_to_epoch

Method year_to_epoch

Lib/zoneinfo/_zoneinfo.py:593–624  ·  view source on GitHub ↗

Calculates the datetime of the occurrence from the year

(self, year)

Source from the content-addressed store, hash-verified

591
592 # TODO: These are not actually epoch dates as they are expressed in local time
593 def year_to_epoch(self, year):
594 """Calculates the datetime of the occurrence from the year"""
595 # We know year and month, we need to convert w, d into day of month
596 #
597 # Week 1 is the first week in which day `d` (where 0 = Sunday) appears.
598 # Week 5 represents the last occurrence of day `d`, so we need to know
599 # the range of the month.
600 first_day, days_in_month = calendar.monthrange(year, self.m)
601
602 # This equation seems magical, so I'll break it down:
603 # 1. calendar says 0 = Monday, POSIX says 0 = Sunday
604 # so we need first_day + 1 to get 1 = Monday -> 7 = Sunday,
605 # which is still equivalent because this math is mod 7
606 # 2. Get first day - desired day mod 7: -1 % 7 = 6, so we don't need
607 # to do anything to adjust negative numbers.
608 # 3. Add 1 because month days are a 1-based index.
609 month_day = (self.d - (first_day + 1)) % 7 + 1
610
611 # Now use a 0-based index version of `w` to calculate the w-th
612 # occurrence of `d`
613 month_day += (self.w - 1) * 7
614
615 # month_day will only be > days_in_month if w was 5, and `w` means
616 # "last occurrence of `d`", so now we just check if we over-shot the
617 # end of the month and if so knock off 1 week.
618 if month_day > days_in_month:
619 month_day -= 7
620
621 ordinal = self._ymd2ord(year, self.m, month_day)
622 epoch = ordinal * 86400
623 epoch += self.hour * 3600 + self.minute * 60 + self.second
624 return epoch
625
626
627def _parse_tz_str(tz_str):

Callers

nothing calls this directly

Calls 1

_ymd2ordMethod · 0.95

Tested by

no test coverage detected