def test_download_date_tz_4(temp_file): """ test tz_aware property """ from osxmetadata import OSXMetaData from osxmetadata.datetime_utils import datetime_naive_to_local import datetime meta = OSXMetaData(temp_file) dt = datetime.datetime.now() # test tz_aware assert not meta.tz_aware meta.tz_aware = True assert meta.tz_aware # test tz_aware dt_tz = datetime_naive_to_local(dt) meta.downloadeddate = dt_tz assert meta.downloadeddate == [dt_tz] assert meta.get_attribute("downloadeddate") == [dt_tz] # test timezone == UTC dld = meta.downloadeddate[0] tz = dld.tzinfo.tzname(dld) assert tz == "UTC" # turn tz_aware off meta.tz_aware = False assert meta.downloadeddate == [dt] assert meta.get_attribute("downloadeddate") == [dt] assert meta.downloadeddate[0].tzinfo is None
def test_cli_downloadeddate(temp_file): # pass ISO 8601 format with timezone, get back naive local time import datetime from osxmetadata import OSXMetaData, kMDItemDownloadedDate from osxmetadata.datetime_utils import ( datetime_naive_to_utc, datetime_utc_to_local, datetime_remove_tz, ) from osxmetadata.__main__ import cli runner = CliRunner() dt = "2020-02-23:00:00:00+00:00" # UTC time utc_time = datetime.datetime.fromisoformat(dt) local_time = datetime_remove_tz(datetime_utc_to_local(utc_time)) result = runner.invoke(cli, ["--set", "downloadeddate", dt, "--list", temp_file]) assert result.exit_code == 0 output = parse_cli_output(result.stdout) assert output["downloadeddate"] == f"['{local_time.isoformat()}']" meta = OSXMetaData(temp_file) meta.tz_aware = True assert meta.get_attribute(kMDItemDownloadedDate) == [utc_time] assert meta.downloadeddate == [utc_time]