From b7da92d2254e81f40410f8b9d0b64429e704a4cb Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Thu, 19 Nov 2009 23:25:35 -0300 Subject: [PATCH] CPDate initWithString: now takes proper international format strings like NSDate. Similarly CPDate description generates such strings. --- Foundation/CPDate.j | 31 +++++++++++++++++++++++++++++-- Tests/Foundation/CPDateTest.j | 29 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Foundation/CPDate.j b/Foundation/CPDate.j index 3d712734c..16d82f52f 100644 --- a/Foundation/CPDate.j +++ b/Foundation/CPDate.j @@ -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 diff --git a/Tests/Foundation/CPDateTest.j b/Tests/Foundation/CPDateTest.j index b09567b31..fe2de759b 100644 --- a/Tests/Foundation/CPDateTest.j +++ b/Tests/Foundation/CPDateTest.j @@ -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