Skip to content

Commit

Permalink
UTC -> timezone.utc for py 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dugalh committed Oct 10, 2024
1 parent 4bbb5e2 commit 2f61dd2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
8 changes: 4 additions & 4 deletions geedim/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import re
import textwrap as wrap
from collections import OrderedDict
from datetime import datetime, timedelta, UTC
from datetime import datetime, timedelta, timezone
from typing import Dict, List, Union

import ee
Expand Down Expand Up @@ -80,7 +80,7 @@ def parse_date(date: Union[datetime, str], var_name=None) -> datetime:
var_name = var_name or 'date'
if isinstance(date, str):
try:
date = datetime.strptime(date, '%Y-%m-%d').replace(tzinfo=UTC)
date = datetime.strptime(date, '%Y-%m-%d').replace(tzinfo=timezone.utc)
except ValueError:
raise ValueError(f'{var_name} should be a datetime instance or UTC string with format: "%Y-%m-%d"')
return date
Expand Down Expand Up @@ -371,7 +371,7 @@ def _get_properties_table(self, properties: Dict, schema: Dict = None) -> str:
for prop_name, key_dict in schema.items():
if prop_name in im_prop_dict:
if prop_name == 'system:time_start': # convert timestamp to date string
dt = datetime.fromtimestamp(im_prop_dict[prop_name] / 1000, tz=UTC)
dt = datetime.fromtimestamp(im_prop_dict[prop_name] / 1000, tz=timezone.utc)
abbrev_dict[key_dict['abbrev']] = datetime.strftime(dt, '%Y-%m-%d %H:%M')
else:
abbrev_dict[key_dict['abbrev']] = im_prop_dict[prop_name]
Expand Down Expand Up @@ -616,7 +616,7 @@ def composite(
comp_image = comp_image.set('INPUT_IMAGES', 'TABLE:\n' + props_str)

# construct an ID for the composite
dates = [datetime.fromtimestamp(item['system:time_start'] / 1000, tz=UTC) for item in props.values()]
dates = [datetime.fromtimestamp(item['system:time_start'] / 1000, tz=timezone.utc) for item in props.values()]
start_date = min(dates).strftime('%Y_%m_%d')
end_date = max(dates).strftime('%Y_%m_%d')

Expand Down
4 changes: 2 additions & 2 deletions geedim/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import time
import warnings
from concurrent.futures import as_completed, ThreadPoolExecutor
from datetime import datetime, UTC
from datetime import datetime, timezone
from itertools import product
from typing import Dict, Iterator, List, Optional, Tuple, Union

Expand Down Expand Up @@ -164,7 +164,7 @@ def id(self) -> Optional[str]:
def date(self) -> datetime | None:
"""Image capture date & time. None if the image ``system:time_start`` property is not set."""
if 'system:time_start' in self.properties:
return datetime.fromtimestamp(self.properties['system:time_start'] / 1000, tz=UTC)
return datetime.fromtimestamp(self.properties['system:time_start'] / 1000, tz=timezone.utc)
else:
return None

Expand Down
4 changes: 2 additions & 2 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
"""

from datetime import datetime, UTC
from datetime import datetime, timezone
from typing import Dict, List, Union

import ee
Expand Down Expand Up @@ -659,7 +659,7 @@ def test_composite_date(image_list: str, request: pytest.FixtureRequest):
gd_collection = MaskedCollection.from_list(image_list)
# assumes the image_list's are in date order
first_date = datetime.fromtimestamp(
gd_collection.ee_collection.first().get('system:time_start').getInfo() / 1000, tz=UTC
gd_collection.ee_collection.first().get('system:time_start').getInfo() / 1000, tz=timezone.utc
)
comp_im = gd_collection.composite()
assert comp_im.date == first_date
Expand Down
6 changes: 4 additions & 2 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from __future__ import annotations

import pathlib
from datetime import datetime, UTC
from datetime import datetime, timezone
from itertools import product
from typing import Dict, List

Expand Down Expand Up @@ -232,7 +232,9 @@ def test_s2_props(s2_sr_base_image: BaseImage):
assert s2_sr_base_image.scale == min_band_info['crs_transform'][0]
assert s2_sr_base_image.transform == Affine(*min_band_info['crs_transform'])
assert s2_sr_base_image.shape == tuple(min_band_info['dimensions'][::-1])
assert s2_sr_base_image.date == datetime.fromtimestamp(s2_sr_base_image.properties['system:time_start'] / 1000, UTC)
assert s2_sr_base_image.date == datetime.fromtimestamp(
s2_sr_base_image.properties['system:time_start'] / 1000, timezone.utc
)
assert s2_sr_base_image.size is not None
assert s2_sr_base_image.footprint is not None
assert s2_sr_base_image.footprint['type'] == 'Polygon'
Expand Down

0 comments on commit 2f61dd2

Please sign in to comment.