Skip to content

Commit

Permalink
feat: new now Date constructor (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored Oct 17, 2024
2 parents dfe1cd4 + 4b00c3d commit 5c37f58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions geetools/Date.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ def fromDOY(cls, doy: int, year: int) -> ee.Date:
d, y = ee.Number(doy).toInt(), ee.Number(year).toInt()
return ee.Date.fromYMD(y, 1, 1).advance(d.subtract(1), "day")

@classmethod
def now(cls) -> ee.Date:
"""Create a date on current date.
Returns:
The date as a ``ee.Date`` object.
Examples:
.. code-block:: python
import ee, geetools
ee.Initialize()
d = ee.Date.geetools.now()
d.getInfo()
"""
return ee.Date(datetime.now().isoformat())

# -- export date -----------------------------------------------------------
def to_datetime(self) -> datetime:
"""Convert a ``ee.Date`` to a ``datetime.datetime``.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_Date.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import ee
import pytest

import geetools # noqa: F401


class TestToDatetime:
"""Test the toDatetime method."""
Expand Down Expand Up @@ -69,3 +71,11 @@ def test_is_leap_2000(self):
def test_is_leap_1900(self):
leap = ee.Date("1900-01-01").geetools.isLeap()
assert leap.getInfo() == 0


class TestNow:
"""Test the now method."""

def test_now(self):
date = ee.Date.geetools.now()
assert date.getInfo() is not None

0 comments on commit 5c37f58

Please sign in to comment.