forked from google/earthengine-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
date.js
114 lines (97 loc) · 2.75 KB
/
date.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @fileoverview A wrapper for Date.
*
* We don't autogenerate this class because we want the constructor
* to promote by pushing things through the server-side Date() function.
*
*/
goog.provide('ee.Date');
goog.require('ee.ApiFunction');
goog.require('ee.ComputedObject');
goog.require('ee.Types');
goog.require('ee.arguments');
/**
* Constructs a new Date object.
*
* @param {number|String|ee.ComputedObject|Date} date The date to convert,
* one of:
* a number (number of milliseconds since the epoch),
* an ISO Date string,
* a JavaScript Date
* or a ComputedObject.
* @param {string=} opt_tz An optional timezone only to be used with a
* string date.
*
* @constructor
* @extends {ee.ComputedObject}
* @export
*/
ee.Date = function(date, opt_tz) {
// Note: We don't autoconstruct this class because we want all objects to go
// through the server-side constructor.
// Constructor safety.
if (!(this instanceof ee.Date)) {
return ee.ComputedObject.construct(ee.Date, arguments);
} else if (date instanceof ee.Date) {
return date;
}
ee.Date.initialize();
var jsArgs = ee.arguments.extract(ee.Date, arguments);
date = jsArgs['date'];
var tz = jsArgs['tz'];
var func = new ee.ApiFunction('Date');
var args = {};
var varName = null;
if (ee.Types.isString(date)) {
args['value'] = date;
if (tz) {
if (ee.Types.isString(tz)) {
args['timeZone'] = tz;
} else {
throw Error(
'Invalid argument specified for ee.Date(..., opt_tz): ' + tz);
}
}
} else if (ee.Types.isNumber(date)) {
args['value'] = date;
} else if (goog.isDateLike(date)) {
args['value'] = Math.floor(/** @type {Date} */(date).getTime());
} else if (date instanceof ee.ComputedObject) {
if (date.func && date.func.getSignature()['returns'] == 'Date') {
// If it's a call that's already returning a Date, just cast.
func = date.func;
args = date.args;
varName = date.varName;
} else {
args['value'] = date;
}
} else {
throw Error('Invalid argument specified for ee.Date(): ' + date);
}
goog.base(this, func, args, varName);
};
goog.inherits(ee.Date, ee.ComputedObject);
/**
* Whether the class has been initialized with API functions.
* @type {boolean}
* @private
*/
ee.Date.initialized_ = false;
/** Imports API functions to this class. */
ee.Date.initialize = function() {
if (!ee.Date.initialized_) {
ee.ApiFunction.importApi(ee.Date, 'Date', 'Date');
ee.Date.initialized_ = true;
}
};
/** Removes imported API functions from this class. */
ee.Date.reset = function() {
ee.ApiFunction.clearApi(ee.Date);
ee.Date.initialized_ = false;
};
/**
* @override
*/
ee.Date.prototype.name = function() {
return 'Date';
};