Skip to content

Commit

Permalink
Use display instead of print for existing Py API ee.Date and ee.DateR…
Browse files Browse the repository at this point in the history
…ange code snippets

PiperOrigin-RevId: 571353560
  • Loading branch information
jdbcode authored and copybara-github committed Oct 6, 2023
1 parent 3817138 commit 559d4c1
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 146 deletions.
23 changes: 4 additions & 19 deletions samples/python/apidocs/ee_date_advance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,14 @@
# limitations under the License.

# [START earthengine__apidocs__ee_date_advance]
"""Demonstrates the ee.Date.advance method."""

import ee


# Authenticates to the Earth Engine servers.
ee.Authenticate()
# Initializes the client library.
ee.Initialize()


def print_date(ee_date, message):
"""Prints a formatted date, along with a descriptive message."""
print(ee_date.format().getInfo(), message)

# Defines a base date/time for the following examples.
BASE_DATE = ee.Date('2020-01-01T00:00', 'UTC')
print_date(BASE_DATE, 'The base date/time')
display('The base date/time', BASE_DATE)

# Demonstrates basic usage.
print_date(BASE_DATE.advance(1, 'week'), '+1 week')
print_date(BASE_DATE.advance(2, 'years'), '+2 years')
display('+1 week', BASE_DATE.advance(1, 'week'))
display('+2 years', BASE_DATE.advance(2, 'years'))

# Demonstrates that negative delta moves back in time.
print_date(BASE_DATE.advance(-1, 'second'), '-1 second')
display('-1 second', BASE_DATE.advance(-1, 'second'))
# [END earthengine__apidocs__ee_date_advance]
2 changes: 1 addition & 1 deletion samples/python/apidocs/ee_date_aside.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

def print_date(ee_date, message):
"""Prints a formatted date, along with a descriptive message."""
print(message, ee_date.format().getInfo())
display(message, ee_date.format("YYYY-mm-dd"))

# Print a message when constructing the ee.Date.
ee_date = ee.Date(datetime.now()).aside(print_date, "Today's date (UTC):")
Expand Down
10 changes: 0 additions & 10 deletions samples/python/apidocs/ee_date_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
# limitations under the License.

# [START earthengine__apidocs__ee_date_difference]
"""Demonstrates the ee.Date.difference method."""

import ee


# Authenticates to the Earth Engine servers.
ee.Authenticate()
# Initializes the client library.
ee.Initialize()

DATE_1 = ee.Date('2020-01-01')
DATE_2 = ee.Date('2020-01-15')

Expand Down
72 changes: 36 additions & 36 deletions samples/python/apidocs/ee_date_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,71 @@
# Various examples of ee.Date.format with Joda-Time formatting and time zones.

date = ee.Date('2020-08-18') # Defaults to UTC
print(date.getInfo()) # Date (2020-08-18 00:00:00)
display(date) # Date (2020-08-18 00:00:00)

# List of time zones:
# https://www.joda.org/joda-time/timezones.html

print(date.format(None, 'GMT').getInfo()) # 2020-08-18T00:00:00
print(date.format(None, 'Etc/GMT').getInfo()) # 2020-08-18T00:00:00
print(date.format(None, 'Etc/GMT+0').getInfo()) # 2020-08-18T00:00:00
print(date.format(None, 'Zulu').getInfo()) # 2020-08-18T00:00:00
print(date.format(None, 'UTC').getInfo()) # 2020-08-18T00:00:00
display(date.format(None, 'GMT')) # 2020-08-18T00:00:00
display(date.format(None, 'Etc/GMT')) # 2020-08-18T00:00:00
display(date.format(None, 'Etc/GMT+0')) # 2020-08-18T00:00:00
display(date.format(None, 'Zulu')) # 2020-08-18T00:00:00
display(date.format(None, 'UTC')) # 2020-08-18T00:00:00

print(date.format(None, 'America/Los_Angeles').getInfo()) # 2020-08-17T17:00:00
print(date.format(None, 'US/Pacific').getInfo()) # 2020-08-17T17:00:00
print(date.format(None, 'Etc/GMT+8').getInfo()) # 2020-08-17T17:00:00
print(date.format(None, 'PST8PDT').getInfo()) # 2020-08-17T17:00:00
display(date.format(None, 'America/Los_Angeles')) # 2020-08-17T17:00:00
display(date.format(None, 'US/Pacific')) # 2020-08-17T17:00:00
display(date.format(None, 'Etc/GMT+8')) # 2020-08-17T17:00:00
display(date.format(None, 'PST8PDT')) # 2020-08-17T17:00:00

print(date.format(None, 'Australia/Tasmania').getInfo()) # 2020-08-18T10:00:00
print(date.format(None, 'Etc/GMT-10').getInfo()) # 2020-08-18T10:00:00
display(date.format(None, 'Australia/Tasmania')) # 2020-08-18T10:00:00
display(date.format(None, 'Etc/GMT-10')) # 2020-08-18T10:00:00

# Reference for Joda-Time format characters:
# http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html

datetime = ee.Date('1975-07-23T21:13:59') # Defaults to UTC
print(datetime.getInfo()) # Date (1972-07-25 21:13:59)
display(datetime) # Date (1972-07-25 21:13:59)

# year of era and era
print(datetime.format('YYYY GG').getInfo()) # 1975 AD
display(datetime.format('YYYY GG')) # 1975 AD
# century and year
print(datetime.format('CC YY').getInfo()) # 19 75
display(datetime.format('CC YY')) # 19 75
# weekyear and week of weekyear
print(datetime.format('xxxx ww').getInfo()) # 1975 30
display(datetime.format('xxxx ww')) # 1975 30

# year and day of year
print(datetime.format('yy DDD').getInfo()) # 75 204
display(datetime.format('yy DDD')) # 75 204
# month of year and day of month
print(datetime.format('MM dd').getInfo()) # 07 23
display(datetime.format('MM dd')) # 07 23

# day of week number and day of week text
print(datetime.format('e E').getInfo()) # 3 Wed
print(datetime.format('e EEEEEEEE').getInfo()) # 3 Wednesday
display(datetime.format('e E')) # 3 Wed
display(datetime.format('e EEEEEEEE')) # 3 Wednesday

# half of day, hour of halfday, and clockhour of halfday
print(datetime.format('a K h').getInfo()) # PM 9 9
print(datetime.format('a KK hh').getInfo()) # PM 09 09
display(datetime.format('a K h')) # PM 9 9
display(datetime.format('a KK hh')) # PM 09 09

# hour of day, clockhour of day, minute, second, fraction of second
print(datetime.format('H k m s S').getInfo()) # 21 21 13 59 0
print(datetime.format('HH kk mm ss SS').getInfo()) # 21 21 13 59 00
display(datetime.format('H k m s S')) # 21 21 13 59 0
display(datetime.format('HH kk mm ss SS')) # 21 21 13 59 00

# time zone
print(datetime.format('z').getInfo()) # UTC
print(datetime.format('zzzz').getInfo()) # Coordinated Universal Time
print(datetime.format('z', 'PST8PDT').getInfo()) # PDT
print(datetime.format('zzzz', 'PST8PDT').getInfo()) # Pacific Daylight Time
display(datetime.format('z')) # UTC
display(datetime.format('zzzz')) # Coordinated Universal Time
display(datetime.format('z', 'PST8PDT')) # PDT
display(datetime.format('zzzz', 'PST8PDT')) # Pacific Daylight Time

# time zone offset/id
print(datetime.format('Z').getInfo()) # +0000
print(datetime.format('ZZ').getInfo()) # +00:00
print(datetime.format('ZZZ').getInfo()) # UTC
print(datetime.format('Z', 'PST8PDT').getInfo()) # -0700
print(datetime.format('ZZ', 'PST8PDT').getInfo()) # -07:00
print(datetime.format('ZZZ', 'PST8PDT').getInfo()) # PST8PDT
display(datetime.format('Z')) # +0000
display(datetime.format('ZZ')) # +00:00
display(datetime.format('ZZZ')) # UTC
display(datetime.format('Z', 'PST8PDT')) # -0700
display(datetime.format('ZZ', 'PST8PDT')) # -07:00
display(datetime.format('ZZZ', 'PST8PDT')) # PST8PDT

# single quotes for text
print(datetime.format("YY 'yada' MM").getInfo()) # 75 yada 07
display(datetime.format("YY 'yada' MM")) # 75 yada 07
# '' for a single quote
print(datetime.format("YY ''MM'' dd").getInfo()) # 75 '07' 23
display(datetime.format("YY ''MM'' dd")) # 75 '07' 23
# [END earthengine__apidocs__ee_date_format]
9 changes: 5 additions & 4 deletions samples/python/apidocs/ee_date_fromymd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# limitations under the License.

# [START earthengine__apidocs__ee_date_fromymd]
print('Date with default UTC:',
ee.Date.fromYMD(2021, 4, 30).format().getInfo())
display('Date with default UTC:', ee.Date.fromYMD(2021, 4, 30))

print('Date with time zone specified:',
ee.Date.fromYMD(2021, 4, 30, 'America/Los_Angeles').format().getInfo())
display(
'Date with time zone specified:',
ee.Date.fromYMD(2021, 4, 30, 'America/Los_Angeles')
)
# [END earthengine__apidocs__ee_date_fromymd]
14 changes: 7 additions & 7 deletions samples/python/apidocs/ee_date_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
# [START earthengine__apidocs__ee_date_get]
date = ee.Date('2021-4-30T07:15:31')

print('Year:', date.get('year').getInfo())
print('Month:', date.get('month').getInfo())
print('Week:', date.get('week').getInfo())
print('Day:', date.get('day').getInfo())
print('Hour:', date.get('hour').getInfo())
print('Minute:', date.get('minute').getInfo())
print('Second:', date.get('second').getInfo())
display('Year:', date.get('year'))
display('Month:', date.get('month'))
display('Week:', date.get('week'))
display('Day:', date.get('day'))
display('Hour:', date.get('hour'))
display('Minute:', date.get('minute'))
display('Second:', date.get('second'))
# [END earthengine__apidocs__ee_date_get]
14 changes: 7 additions & 7 deletions samples/python/apidocs/ee_date_getfraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
# [START earthengine__apidocs__ee_date_getfraction]
date = ee.Date('2021-4-30T07:15:31.24')

print('Elapsed fraction of a year:', date.getFraction('year').getInfo())
print('Elapsed fraction of a month:', date.getFraction('month').getInfo())
print('Elapsed fraction of a week:', date.getFraction('week').getInfo())
print('Elapsed fraction of a day:', date.getFraction('day').getInfo())
print('Elapsed fraction of an hour:', date.getFraction('hour').getInfo())
print('Elapsed fraction of a minute:', date.getFraction('minute').getInfo())
print('Elapsed fraction of a second:', date.getFraction('second').getInfo())
display('Elapsed fraction of a year:', date.getFraction('year'))
display('Elapsed fraction of a month:', date.getFraction('month'))
display('Elapsed fraction of a week:', date.getFraction('week'))
display('Elapsed fraction of a day:', date.getFraction('day'))
display('Elapsed fraction of an hour:', date.getFraction('hour'))
display('Elapsed fraction of a minute:', date.getFraction('minute'))
display('Elapsed fraction of a second:', date.getFraction('second'))
# [END earthengine__apidocs__ee_date_getfraction]
17 changes: 7 additions & 10 deletions samples/python/apidocs/ee_date_getrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
# [START earthengine__apidocs__ee_date_getrange]
date = ee.Date('2021-4-30T07:15:31.24')

print('1-year date range covering input date:', date.getRange('year').getInfo())
print('1-month date range covering input date:',
date.getRange('month').getInfo())
print('1-week date range covering input date:', date.getRange('week').getInfo())
print('1-day date range covering input date:', date.getRange('day').getInfo())
print('1-hour date range covering input date:', date.getRange('hour').getInfo())
print('1-minute date range covering input date:',
date.getRange('minute').getInfo())
print('1-second date range covering input date:',
date.getRange('second').getInfo())
display('1-year date range covering input date:', date.getRange('year'))
display('1-month date range covering input date:', date.getRange('month'))
display('1-week date range covering input date:', date.getRange('week'))
display('1-day date range covering input date:', date.getRange('day'))
display('1-hour date range covering input date:', date.getRange('hour'))
display('1-minute date range covering input date:', date.getRange('minute'))
display('1-second date range covering input date:', date.getRange('second'))
# [END earthengine__apidocs__ee_date_getrange]
16 changes: 7 additions & 9 deletions samples/python/apidocs/ee_date_getrelative.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
# [START earthengine__apidocs__ee_date_getrelative]
date = ee.Date('2021-4-30T07:15:31.24')

print('0-based month of year:', date.getRelative('year', 'year').getInfo())
print('0-based week of year:', date.getRelative('week', 'year').getInfo())
print('0-based day of year:', date.getRelative('day', 'year').getInfo())
print('0-based day of month:', date.getRelative('day', 'month').getInfo())
print('0-based minute of day:', date.getRelative('minute', 'day').getInfo())
print('0-based second of minute:',
date.getRelative('second', 'minute').getInfo())
display('0-based month of year:', date.getRelative('year', 'year'))
display('0-based week of year:', date.getRelative('week', 'year'))
display('0-based day of year:', date.getRelative('day', 'year'))
display('0-based day of month:', date.getRelative('day', 'month'))
display('0-based minute of day:', date.getRelative('minute', 'day'))
display('0-based second of minute:', date.getRelative('second', 'minute'))

# 0 is returned when unit argument is larger than inUnit argument.
print('0-based year of month (bad form):',
date.getRelative('year', 'month').getInfo())
display('0-based year of month (bad form):', date.getRelative('year', 'month'))
# [END earthengine__apidocs__ee_date_getrelative]
3 changes: 2 additions & 1 deletion samples/python/apidocs/ee_date_millis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

# [START earthengine__apidocs__ee_date_millis]
from datetime import datetime

date = ee.Date(datetime.now())

print('Milliseconds since Unix epoch:', date.millis().getInfo())
display('Milliseconds since Unix epoch:', date.millis())
# [END earthengine__apidocs__ee_date_millis]
13 changes: 6 additions & 7 deletions samples/python/apidocs/ee_date_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
# limitations under the License.

# [START earthengine__apidocs__ee_date_parse]
print(ee.Date.parse('YYYY MM dd', '2021 4 30').format().getInfo())
print(ee.Date.parse('YYYY-MM-dd', '2021-4-30').format().getInfo())
print(ee.Date.parse('YYYY/MM/dd', '2021/4/30').format().getInfo())
print(ee.Date.parse('MM/dd/YY', '4/30/21').format().getInfo())
print(ee.Date.parse('MMM. dd, YYYY', 'Apr. 30, 2021').format().getInfo())
print(ee.Date.parse('YYYY-MM-dd HH:mm:ss', '2021-4-30 00:00:00').format()
.getInfo())
display(ee.Date.parse('YYYY MM dd', '2021 4 30'))
display(ee.Date.parse('YYYY-MM-dd', '2021-4-30'))
display(ee.Date.parse('YYYY/MM/dd', '2021/4/30'))
display(ee.Date.parse('MM/dd/YY', '4/30/21'))
display(ee.Date.parse('MMM. dd, YYYY', 'Apr. 30, 2021'))
display(ee.Date.parse('YYYY-MM-dd HH:mm:ss', '2021-4-30 00:00:00'))
# [END earthengine__apidocs__ee_date_parse]
2 changes: 1 addition & 1 deletion samples/python/apidocs/ee_date_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
# [START earthengine__apidocs__ee_date_serialize]
date = ee.Date('2021-4-30')

print('Serialized representation of ee.Date:', date.serialize())
display('Serialized representation of ee.Date:', date.serialize())
# [END earthengine__apidocs__ee_date_serialize]
8 changes: 4 additions & 4 deletions samples/python/apidocs/ee_date_unitratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

# [START earthengine__apidocs__ee_date_unitratio]
print('Minutes in a day:', ee.Date.unitRatio('day', 'minute').getInfo())
print('Seconds in a year:', ee.Date.unitRatio('year', 'second').getInfo())
print('Years in a month:', ee.Date.unitRatio('month', 'year').getInfo())
print('Hours in a week:', ee.Date.unitRatio('week', 'hour').getInfo())
display('Minutes in a day:', ee.Date.unitRatio('day', 'minute'))
display('Seconds in a year:', ee.Date.unitRatio('year', 'second'))
display('Years in a month:', ee.Date.unitRatio('month', 'year'))
display('Hours in a week:', ee.Date.unitRatio('week', 'hour'))
# [END earthengine__apidocs__ee_date_unitratio]
6 changes: 4 additions & 2 deletions samples/python/apidocs/ee_date_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# [START earthengine__apidocs__ee_date_update]
date = ee.Date('2021-4-30T07:15:31.24')

print('Updated year and minute components of the input date:',
date.update(year=2010, minute=59).format().getInfo())
display(
'Updated year and minute components of the input date:',
date.update(year=2010, minute=59)
)
# [END earthengine__apidocs__ee_date_update]
12 changes: 8 additions & 4 deletions samples/python/apidocs/ee_daterange_contains.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
date_range_3 = ee.DateRange('2010-06-24', '2020-07-24')

# Determine if an ee.DateRange is contained within another.
print('Does date_range_1 contain date_range_2?',
date_range_1.contains(date_range_2).getInfo())
print('Does date_range_1 contain date_range_3?',
date_range_1.contains(date_range_3).getInfo())
display(
'Does date_range_1 contain date_range_2?',
date_range_1.contains(date_range_2)
)
display(
'Does date_range_1 contain date_range_3?',
date_range_1.contains(date_range_3)
)
# [END earthengine__apidocs__ee_daterange_contains]
2 changes: 1 addition & 1 deletion samples/python/apidocs/ee_daterange_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
date_range = ee.DateRange('2017-06-24', '2017-07-24')

# Return the end of the ee.DateRange as an ee.Date.
print('The end of dateRange is', date_range.end().format().getInfo())
display('The end of dateRange is', date_range.end())
# [END earthengine__apidocs__ee_daterange_end]
6 changes: 4 additions & 2 deletions samples/python/apidocs/ee_daterange_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
date_range_2 = ee.DateRange('2017-07-01', '2018-08-24')

# Determine the intersection of two ee.DateRange objects.
print('Intersection of date_range_1 and date_range_2:',
date_range_1.intersection(date_range_2).getInfo())
display(
'Intersection of date_range_1 and date_range_2:',
date_range_1.intersection(date_range_2)
)
# [END earthengine__apidocs__ee_daterange_intersection]
18 changes: 12 additions & 6 deletions samples/python/apidocs/ee_daterange_intersects.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
date_range_4 = ee.DateRange('2017-06-25', '2017-07-25')

# Determine if an ee.DateRange intersects another.
print('Does date_range_1 contain date_range_2?',
date_range_1.intersects(date_range_2).getInfo())
print('Does date_range_1 contain date_range_3?',
date_range_1.intersects(date_range_3).getInfo())
print('Does date_range_1 contain date_range_4?',
date_range_1.intersects(date_range_4).getInfo())
display(
'Does date_range_1 contain date_range_2?',
date_range_1.intersects(date_range_2)
)
display(
'Does date_range_1 contain date_range_3?',
date_range_1.intersects(date_range_3)
)
display(
'Does date_range_1 contain date_range_4?',
date_range_1.intersects(date_range_4)
)
# [END earthengine__apidocs__ee_daterange_intersects]
6 changes: 3 additions & 3 deletions samples/python/apidocs/ee_daterange_isempty.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
date_range_3 = ee.DateRange('2017-07-24', '2017-07-24')

# Determine if the ee.DateRange is empty.
print('Is date_range_1 empty?', date_range_1.isEmpty().getInfo())
print('Is date_range_2 empty?', date_range_2.isEmpty().getInfo())
print('Is date_range_3 empty?', date_range_3.isEmpty().getInfo())
display('Is date_range_1 empty?', date_range_1.isEmpty())
display('Is date_range_2 empty?', date_range_2.isEmpty())
display('Is date_range_3 empty?', date_range_3.isEmpty())
# [END earthengine__apidocs__ee_daterange_isempty]
Loading

0 comments on commit 559d4c1

Please sign in to comment.