mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-09-14 14:41:28 +00:00
CPDate initWithString: now takes proper international format strings like NSDate. Similarly CPDate description generates such strings.
This commit is contained in:
committed by
Ross Boucher
parent
e4f6d0db1f
commit
b7da92d225
+29
-2
@@ -96,9 +96,30 @@ var CPDateReferenceDate = new Date(Date.UTC(2001,1,1,0,0,0,0));
|
||||
return self;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a CPDate initialized with a date and time specified by the given
|
||||
string in international date format YYYY-MM-DD HH:MM:SS ±HHMM (e.g.
|
||||
2009-11-17 17:52:04 +0000).
|
||||
*/
|
||||
- (id)initWithString:(CPString)description
|
||||
{
|
||||
self = new Date(description); // FIXME: not same format as NSString
|
||||
var format = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([-+])(\d{2})(\d{2})/;
|
||||
var d = description.match(new RegExp(format));
|
||||
|
||||
if (d == null || d.length != 10)
|
||||
[CPException raise:CPInvalidArgumentException
|
||||
reason:"initWithString: the string must be of YYYY-MM-DD HH:MM:SS ±HHMM format"];
|
||||
|
||||
var date = new Date(d[1], d[2]-1, d[3]);
|
||||
date.setHours(d[4]);
|
||||
date.setMinutes(d[5]);
|
||||
date.setSeconds(d[6]);
|
||||
|
||||
var tzOffset = Number(d[8]) * 60 + Number(d[9]);
|
||||
if (d[7] == '-')
|
||||
tzOffset = -tzOffset;
|
||||
|
||||
self = new Date(date.getTime() + (tzOffset - date.getTimezoneOffset()) * 60 * 1000);
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -152,9 +173,15 @@ var CPDateReferenceDate = new Date(Date.UTC(2001,1,1,0,0,0,0));
|
||||
return (self > anotherDate) ? self : anotherDate;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the date as a string in the international format
|
||||
YYYY-MM-DD HH:MM:SS ±HHMM.
|
||||
*/
|
||||
- (CPString)description
|
||||
{
|
||||
return self.toString(); // FIXME: not same format as NSDate
|
||||
var hours = Math.floor(self.getTimezoneOffset() / 60);
|
||||
var minutes = self.getTimezoneOffset() - hours * 60;
|
||||
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];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -25,4 +25,33 @@
|
||||
[self assert:middle equals:[middle laterDate:past] message:"laterDate incorrect"];
|
||||
}
|
||||
|
||||
- (void)testInitWithString
|
||||
{
|
||||
var tests = [
|
||||
["1970-01-01 00:00:00 +0000", 0],
|
||||
["1970-01-01 00:01:00 +0000", 60],
|
||||
["1970-01-01 01:00:00 +0000", 60*60],
|
||||
["1970-01-02 00:00:00 +0000", 24*60*60],
|
||||
["2009-11-17 17:52:04 +0000", 1258480324],
|
||||
];
|
||||
|
||||
for (var i = 0; i < tests.length; i++)
|
||||
{
|
||||
var parsed = [[CPDate alloc] initWithString:tests[i][0]];
|
||||
var correctSeconds = tests[i][1];
|
||||
[self assert:correctSeconds equals:[parsed timeIntervalSince1970]];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)testDescription
|
||||
{
|
||||
// Unfortunately the result will be different depending on the testing machine's timezone.
|
||||
var expectedHour = 23
|
||||
var expectedMinute = 31;
|
||||
var offsetHours = Math.floor(new Date().getTimezoneOffset() / 60);
|
||||
var offsetMinutes = new Date().getTimezoneOffset() - offsetHours * 60;
|
||||
var expectedString = [CPString stringWithFormat:"2009-02-13 %02d:%02d:30 +%02d%02d", expectedHour-offsetHours, expectedMinute-offsetMinutes, offsetHours, offsetMinutes];
|
||||
[self assert:expectedString equals: [[CPDate dateWithTimeIntervalSince1970: 1234567890] description]];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user