Пример #1
0
 def to_timezone(self, timezone: zoneinfo.ZoneInfo) -> "Track":
     return Track(
         artist=self.artist,
         track=self.track,
         start=self.start.replace(tzinfo=zoneinfo.ZoneInfo("UTC")).astimezone(timezone),
         end=self.end.replace(tzinfo=zoneinfo.ZoneInfo("UTC")).astimezone(timezone),
         duration=self.duration,
     )
Пример #2
0
def to_internal_dt(date_time):
    """
    Convert input datetime to internal timezone
    and removes microsecond component.
    """
    return date_time.astimezone(zoneinfo.ZoneInfo(
        settings.internal_tz)).replace(microsecond=0)
Пример #3
0
def make_package_zip(pkgname,
                     info,
                     author_info,
                     tz_name,
                     current_project='default'):
    directory = make_package_dir(pkgname,
                                 info,
                                 author_info,
                                 current_project=current_project)
    trimlength = len(directory) + 1
    packagedir = os.path.join(directory, 'docassemble-' + str(pkgname))
    temp_zip = tempfile.NamedTemporaryFile(suffix=".zip")
    zf = zipfile.ZipFile(temp_zip, mode='w')
    the_timezone = zoneinfo.ZoneInfo(tz_name)
    for root, dirs, files in os.walk(packagedir):
        for file in files:
            thefilename = os.path.join(root, file)
            zinfo = zipfile.ZipInfo(
                thefilename[trimlength:],
                date_time=datetime.datetime.utcfromtimestamp(
                    os.path.getmtime(thefilename)).replace(
                        tzinfo=datetime.timezone.utc).astimezone(
                            the_timezone).timetuple())
            zinfo.compress_type = zipfile.ZIP_DEFLATED
            zinfo.external_attr = 0o644 << 16
            with open(thefilename, 'rb') as fp:
                zf.writestr(zinfo, fp.read())
    zf.close()
    shutil.rmtree(directory)
    return temp_zip
Пример #4
0
 def __str__(self):
     """
     Format the object for display.
     Uses a git log like structure.
     """
     user_time = self.timestamp.astimezone(
         zoneinfo.ZoneInfo(settings.user_tz))
     time = user_time.strftime(settings.DATETIME_FORMAT)
     return f'\n{click.style(f"id: {self.uuid}", fg="green")}' \
            f'\n{click.style(f"Date: {time}")}' \
            f'\n\n\t{click.style(f"{self.work}", bold=True, fg="white")}\n'
Пример #5
0
 def filter(self, tracks: List[Track]) -> List[Track]:
     if zone := self._combo_var.get():
         tracks = [
             track.to_timezone(zoneinfo.ZoneInfo(zone)) for track in tracks
         ]
Пример #6
0
def now():
    """
    Current datetime in user's local timezone
    """
    return datetime.now(zoneinfo.ZoneInfo(settings.user_tz))
Пример #7
0
#!/usr/bin/env python3.7

import datetime
import sys

import pytz
from backports import zoneinfo

PYTZ_TIME_ZONE = pytz.timezone('America/Chicago')
ZONEINFO_TIME_ZONE = zoneinfo.ZoneInfo('America/Chicago')


def arithmetic_example(show_zoneinfo: bool = False):
    """Display the differences between zoneinfo and pytz in regards to DST boundaries"""

    if not show_zoneinfo:
        print(
            "Next, we'll show how pytz handles date arithmetic around DST boundaries"
        )
    else:
        print(
            "Next, we'll show the difference as to how they handle date arithmetic around DST boundaries"
        )

    # NOTE: Halloween 2020 is on a Saturday, so Sunday, November 1 is the end of DST
    # in the USA
    pytz_start = PYTZ_TIME_ZONE.localize(datetime.datetime(2020, 10, 31, 12))
    # pytz is quite literal: one day is 24 hours, so 24 hours after noon CDT is
    # 11 AM CDT
    next_day = PYTZ_TIME_ZONE.normalize(pytz_start +
                                        datetime.timedelta(days=1))