Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
- Subject: Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
- From: Chuck Hill <email@hidden>
- Date: Thu, 16 Oct 2008 10:29:39 -0700
On Oct 16, 2008, at 9:52 AM, Timo Hoepfner wrote:
Hi,
I have modelled calendar entries, which have a timestamp attribute.
I wanted to build a qualifier that matches all entries within a
month so I created start and end timestamps programatically. The
start timestamp is no problem. To get the end timestamp, I used
NSTimestamp.timestampByAddingGregorianUnits and added "a month minus
one second". This works fine for GMT, but gives me funky results for
e.g. Europe/Berlin. Please see code and output below. Any ideas?
This is with WO 5.3.3.
Timo
public class NSTimestampTest {
public static void main(String[] args) {
int year = 2008;
// This one works
NSTimeZone gmt = NSTimeZone.getGMT();
test(gmt, year);
// This one doesn't
NSTimeZone berlin = NSTimeZone.timeZoneWithName("Europe/Berlin",
false);
test(berlin, year);
}
private static void test(NSTimeZone timezone, int year) {
NSTimestampFormatter formatter = new NSTimestampFormatter("%Y-%m-
%d %H:%M:%S %Z");
formatter.setDefaultFormatTimeZone(timezone);
for (int month = 1; month <= 12; month++) {
NSTimestamp start = new NSTimestamp(year, month, 1, 0, 0, 0,
timezone);
Daylight Saving TIme?
Try NSTimestamp start = new NSTimestamp(year, month, 1, 12, 0, 0,
timezone);
Does that give the right days? If so, you might do better by creating
a new timestamp and subtracting a second. e.g.
NSTimestamp end = new NSTimestamp(year, month + 1, 1, 0, 0, 0,
timezone)stampByAddingGregorianUnits(0, 0, 0, 0, 0, -1);
Chuck
NSTimestamp end = start.timestampByAddingGregorianUnits(0, 1, 0,
0, 0, -1);
System.out.printf("%s -> %s\n", formatter.format(start),
formatter.format(end));
}
}
}
results in
2008-01-01 00:00:00 Etc/GMT -> 2008-01-31 23:59:59 Etc/GMT
2008-02-01 00:00:00 Etc/GMT -> 2008-02-29 23:59:59 Etc/GMT
2008-03-01 00:00:00 Etc/GMT -> 2008-03-31 23:59:59 Etc/GMT
2008-04-01 00:00:00 Etc/GMT -> 2008-04-30 23:59:59 Etc/GMT
2008-05-01 00:00:00 Etc/GMT -> 2008-05-31 23:59:59 Etc/GMT
2008-06-01 00:00:00 Etc/GMT -> 2008-06-30 23:59:59 Etc/GMT
2008-07-01 00:00:00 Etc/GMT -> 2008-07-31 23:59:59 Etc/GMT
2008-08-01 00:00:00 Etc/GMT -> 2008-08-31 23:59:59 Etc/GMT
2008-09-01 00:00:00 Etc/GMT -> 2008-09-30 23:59:59 Etc/GMT
2008-10-01 00:00:00 Etc/GMT -> 2008-10-31 23:59:59 Etc/GMT
2008-11-01 00:00:00 Etc/GMT -> 2008-11-30 23:59:59 Etc/GMT
2008-12-01 00:00:00 Etc/GMT -> 2008-12-31 23:59:59 Etc/GMT
2008-01-01 00:00:00 Europe/Berlin -> 2008-01-31 23:59:59 Europe/Berlin
2008-02-01 00:00:00 Europe/Berlin -> 2008-02-29 23:59:59 Europe/Berlin
2008-03-01 00:00:00 Europe/Berlin -> 2008-03-29 23:59:59 Europe/Berlin
2008-04-01 00:00:00 Europe/Berlin -> 2008-04-30 23:59:59 Europe/Berlin
2008-05-01 00:00:00 Europe/Berlin -> 2008-05-30 23:59:59 Europe/Berlin
2008-06-01 00:00:00 Europe/Berlin -> 2008-06-30 23:59:59 Europe/Berlin
2008-07-01 00:00:00 Europe/Berlin -> 2008-07-30 23:59:59 Europe/Berlin
2008-08-01 00:00:00 Europe/Berlin -> 2008-08-31 23:59:59 Europe/Berlin
2008-09-01 00:00:00 Europe/Berlin -> 2008-09-30 23:59:59 Europe/Berlin
2008-10-01 00:00:00 Europe/Berlin -> 2008-10-30 22:59:59 Europe/Berlin
2008-11-01 00:00:00 Europe/Berlin -> 2008-11-30 23:59:59 Europe/Berlin
2008-12-01 00:00:00 Europe/Berlin -> 2008-12-30 23:59:59 Europe/Berlin
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
--
Chuck Hill Senior Consultant / VP Development
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden