00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPObject.j"
00024 @import "CPString.j"
00025
00026
00027 var CPDateReferenceDate = new Date(Date.UTC(2001,1,1,0,0,0,0));
00028
00036 @implementation CPDate : CPObject
00037 {
00038 }
00039
00040 + (id)alloc
00041 {
00042 return new Date;
00043 }
00044
00045 + (id)date
00046 {
00047 return [[self alloc] init];
00048 }
00049
00050 + (id)dateWithTimeIntervalSinceNow:(CPTimeInterval)seconds
00051 {
00052 return [[CPDate alloc] initWithTimeIntervalSinceNow:seconds];
00053 }
00054
00055 + (id)dateWithTimeIntervalSince1970:(CPTimeInterval)seconds
00056 {
00057 return [[CPDate alloc] initWithTimeIntervalSince1970:seconds];
00058 }
00059
00060 + (id)dateWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds
00061 {
00062 return [[CPDate alloc] initWithTimeIntervalSinceReferenceDate:seconds];
00063 }
00064
00065 + (id)distantPast
00066 {
00067 return new Date(-10000,1,1,0,0,0,0);
00068 }
00069
00070 + (id)distantFuture
00071 {
00072 return new Date(10000,1,1,0,0,0,0);
00073 }
00074
00075 - (id)initWithTimeIntervalSinceNow:(CPTimeInterval)seconds
00076 {
00077 self = new Date((new Date()).getTime() + seconds * 1000);
00078 return self;
00079 }
00080
00081 - (id)initWithTimeIntervalSince1970:(CPTimeInterval)seconds
00082 {
00083 self = new Date(seconds * 1000);
00084 return self;
00085 }
00086
00087 - (id)initWithTimeIntervalSinceReferenceDate:(CPTimeInterval)seconds
00088 {
00089 self = [self initWithTimeInterval:seconds sinceDate:CPDateReferenceDate];
00090 return self;
00091 }
00092
00093 - (id)initWithTimeInterval:(CPTimeInterval)seconds sinceDate:(CPDate)refDate
00094 {
00095 self = new Date(refDate.getTime() + seconds * 1000);
00096 return self;
00097 }
00098
00104 - (id)initWithString:(CPString)description
00105 {
00106 var format = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([-+])(\d{2})(\d{2})/,
00107 d = description.match(new RegExp(format));
00108
00109 if (!d || d.length != 10)
00110 [CPException raise:CPInvalidArgumentException
00111 reason:"initWithString: the string must be of YYYY-MM-DD HH:MM:SS ±HHMM format"];
00112
00113 var date = new Date(d[1], d[2]-1, d[3]),
00114 timeZoneOffset = (Number(d[8]) * 60 + Number(d[9])) * (d[7] === '-' ? -1 : 1);
00115
00116 date.setHours(d[4]);
00117 date.setMinutes(d[5]);
00118 date.setSeconds(d[6]);
00119
00120 self = new Date(date.getTime() + (timeZoneOffset - date.getTimezoneOffset()) * 60 * 1000);
00121 return self;
00122 }
00123
00124 - (CPTimeInterval)timeIntervalSinceDate:(CPDate)anotherDate
00125 {
00126 return (self.getTime() - anotherDate.getTime()) / 1000.0;
00127 }
00128
00129 - (CPTimeInterval)timeIntervalSinceNow
00130 {
00131 return [self timeIntervalSinceDate:[CPDate date]];
00132 }
00133
00134 - (CPTimeInterval)timeIntervalSince1970
00135 {
00136 return self.getTime() / 1000.0;
00137 }
00138
00139 - (CPTimeInterval)timeIntervalSinceReferenceDate
00140 {
00141 return (self.getTime() - CPDateReferenceDate.getTime()) / 1000.0;
00142 }
00143
00144 + (CPTimeInterval)timeIntervalSinceReferenceDate
00145 {
00146 return [[CPDate date] timeIntervalSinceReferenceDate];
00147 }
00148
00149 - (BOOL)isEqual:(CPDate)aDate
00150 {
00151 return [self isEqualToDate:aDate];
00152 }
00153
00154 - (BOOL)isEqualToDate:(CPDate)anotherDate
00155 {
00156 return self === anotherDate || (anotherDate !== nil && anotherDate.isa && [anotherDate isKindOfClass:CPDate] && !(self < anotherDate || self > anotherDate));
00157 }
00158
00159 - (CPComparisonResult)compare:(CPDate)anotherDate
00160 {
00161 return (self > anotherDate) ? CPOrderedDescending : ((self < anotherDate) ? CPOrderedAscending : CPOrderedSame);
00162 }
00163
00164 - (CPDate)earlierDate:(CPDate)anotherDate
00165 {
00166 return (self < anotherDate) ? self : anotherDate;
00167 }
00168
00169 - (CPDate)laterDate:(CPDate)anotherDate
00170 {
00171 return (self > anotherDate) ? self : anotherDate;
00172 }
00173
00178 - (CPString)description
00179 {
00180 var hours = Math.floor(self.getTimezoneOffset() / 60),
00181 minutes = self.getTimezoneOffset() - hours * 60;
00182
00183 return [CPString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d +%02d%02d", self.getFullYear(), self.getMonth()+1, self.getDate(), self.getHours(), self.getMinutes(), self.getSeconds(), hours, minutes];
00184 }
00185
00186 - (id)copy
00187 {
00188 return new Date(self.getTime());
00189 }
00190
00191 @end
00192
00193 var CPDateTimeKey = @"CPDateTimeKey";
00194
00195 @implementation CPDate (CPCoding)
00196
00197 - (id)initWithCoder:(CPCoder)aCoder
00198 {
00199 if (self)
00200 {
00201 self.setTime([aCoder decodeIntForKey:CPDateTimeKey]);
00202 }
00203
00204 return self;
00205 }
00206
00207 - (void)encodeWithCoder:(CPCoder)aCoder
00208 {
00209 [aCoder encodeInt:self.getTime() forKey:CPDateTimeKey];
00210 }
00211
00212 @end
00213
00214 Date.prototype.isa = CPDate;