38 var result =
new Date;
45 return [[
self alloc] init];
48 + (id)dateWithTimeIntervalSinceNow:(CPTimeInterval)seconds
53 + (id)dateWithTimeIntervalSince1970:(CPTimeInterval)seconds
58 + (id)dateWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds
73 - (id)initWithTimeIntervalSinceNow:(CPTimeInterval)seconds
75 self =
new Date((
new Date()).getTime() + seconds * 1000);
79 - (id)initWithTimeIntervalSince1970:(CPTimeInterval)seconds
81 self =
new Date(seconds * 1000);
85 - (id)initWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds
91 - (id)initWithTimeInterval:(CPTimeInterval)seconds sinceDate:(
CPDate)refDate
93 self =
new Date(refDate.getTime() + seconds * 1000);
104 var format = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([-+])(\d{2})(\d{2})/,
105 d = description.match(
new RegExp(format));
107 if (!d || d.length != 10)
109 reason:"initWithString: the string must be in YYYY-MM-DD HH:MM:SS ±HHMM format"];
111 var date =
new Date(d[1], d[2] - 1, d[3]),
112 timeZoneOffset = (Number(d[8]) * 60 + Number(d[9])) * (d[7] ===
'-' ? 1 : -1);
115 date.setMinutes(d[5]);
116 date.setSeconds(d[6]);
118 self =
new Date(date.getTime() + (timeZoneOffset - date.getTimezoneOffset()) * 60 * 1000);
122 - (CPTimeInterval)timeIntervalSinceDate:(
CPDate)anotherDate
124 return (
self.getTime() - anotherDate.getTime()) / 1000.0;
127 - (CPTimeInterval)timeIntervalSinceNow
132 - (CPTimeInterval)timeIntervalSince1970
134 return self.getTime() / 1000.0;
137 - (CPTimeInterval)timeIntervalSinceReferenceDate
142 + (CPTimeInterval)timeIntervalSinceReferenceDate
152 if (!aDate || ![aDate isKindOfClass:[
CPDate class]])
163 return !(self < aDate || self > aDate);
166 - (CPComparisonResult)compare:(
CPDate)anotherDate
173 return (
self < anotherDate) ?
self : anotherDate;
178 return (
self > anotherDate) ?
self : anotherDate;
184 + (
CPString)timezoneOffsetString:(
int)timezoneOffset
186 var offset = -timezoneOffset,
187 positive = offset >= 0,
188 hours = positive ? FLOOR(offset / 60) : CEIL(offset / 60),
189 minutes = offset - hours * 60;
204 return new Date(
self.getTime());
225 [aCoder encodeInt:self.getTime() forKey:CPDateTimeKey];
233 Date.parseISO8601 =
function (date)
240 timestamp = Date.parse(date);
242 if (isNaN(timestamp) && (
struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(
Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date)))
246 struct[k] = +
struct[k] || 0;
249 struct[2] = (+
struct[2] || 1) - 1;
250 struct[3] = +
struct[3] || 1;
252 if (
struct[8] !==
'Z' &&
struct[9] !== undefined)
254 minutesOffset =
struct[10] * 60 +
struct[11];
256 if (
struct[9] ===
'+')
257 minutesOffset = 0 - minutesOffset;
260 return Date.UTC(
struct[1],
struct[2],
struct[3],
struct[4],
struct[5] + minutesOffset,
struct[6],
struct[7]);