mirror of
https://github.com/google/earthengine-api.git
synced 2025-12-08 19:26:12 +00:00
85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
"""A wrapper for dates."""
|
|
|
|
|
|
|
|
import datetime
|
|
import math
|
|
import numbers
|
|
|
|
import apifunction
|
|
import computedobject
|
|
import ee_exception
|
|
import ee_types as types
|
|
import serializer
|
|
|
|
# Using lowercase function naming to match the JavaScript names.
|
|
# pylint: disable=g-bad-name
|
|
|
|
|
|
class Date(computedobject.ComputedObject):
|
|
"""An object to represent dates."""
|
|
|
|
_initialized = False
|
|
|
|
def __init__(self, date, opt_tz=None):
|
|
"""Construct a date.
|
|
|
|
This sends all inputs (except another Date) through the Date function.
|
|
|
|
This constuctor accepts the following args:
|
|
1) A bare date.
|
|
2) An ISO string
|
|
3) A integer number of milliseconds since the epoch.
|
|
4) A ComputedObject.
|
|
|
|
Args:
|
|
date: The date to wrap.
|
|
opt_tz: An optional timezone, only useable with a string date.
|
|
"""
|
|
self.initialize()
|
|
|
|
func = apifunction.ApiFunction('Date')
|
|
args = None
|
|
if isinstance(date, datetime.datetime):
|
|
args = {'value':
|
|
math.floor(serializer.DatetimeToMicroseconds(date) / 1000)}
|
|
elif isinstance(date, numbers.Number) or types.isVarOfType(date, object):
|
|
args = {'value': date}
|
|
elif isinstance(date, basestring):
|
|
args = {'value': date}
|
|
if opt_tz:
|
|
if isinstance(opt_tz, basestring):
|
|
args['timeZone'] = opt_tz
|
|
else:
|
|
raise ee_exception.EEException(
|
|
'Invalid argument specified for ee.Date(..., opt_tz): %s' % date)
|
|
elif isinstance(date, computedobject.ComputedObject):
|
|
if date.func != 'Date':
|
|
args = {'value': date}
|
|
else:
|
|
func = date.func
|
|
args = date.args
|
|
else:
|
|
raise ee_exception.EEException(
|
|
'Invalid argument specified for ee.Date(): %s' % date)
|
|
|
|
super(Date, self).__init__(func, args)
|
|
self._date = None
|
|
|
|
@classmethod
|
|
def initialize(cls):
|
|
"""Imports API functions to this class."""
|
|
if not cls._initialized:
|
|
apifunction.ApiFunction.importApi(cls, 'Date', 'Date')
|
|
cls._initialized = True
|
|
|
|
@classmethod
|
|
def reset(cls):
|
|
"""Removes imported API functions from this class."""
|
|
apifunction.ApiFunction.clearApi(cls)
|
|
cls._initialized = False
|
|
|
|
@staticmethod
|
|
def name():
|
|
return 'Date'
|