• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful


  • Subject: Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
  • From: Ralf Schuchardt <email@hidden>
  • Date: Thu, 16 Oct 2008 21:15:53 +0200


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


Hi Timo,

you might also try the joda-time library. It has functions for using the "natural" meaning of intervals like "1 month".

...
NSTimestamp start = new NSTimestamp(year, month, 1, 0, 0, 0, timezone);
DateTimeZone dtz = DateTimeZone.forTimeZone(timezone);
DateTime ydt = new DateTime(start, dtz);
NSTimestamp end = new NSTimestamp(ydt.plusMonths(1).minusSeconds(1).toDate());
...

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-31 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-31 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-31 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-31 23: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-31 23:59:59 Europe/Berlin


Ralf


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);
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

  • Follow-Ups:
    • Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
      • From: Timo Hoepfner <email@hidden>
    • Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
      • From: Lachlan Deck <email@hidden>
References: 
 >NSTimestamp.timestampByAddingGregorianUnits considered harmful (From: Timo Hoepfner <email@hidden>)

  • Prev by Date: Re: credit card processing
  • Next by Date: Handling Relationships trough migrations...
  • Previous by thread: Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
  • Next by thread: Re: NSTimestamp.timestampByAddingGregorianUnits considered harmful
  • Index(es):
    • Date
    • Thread