Re: Bug ID 6342679: NSTimestamp Happy New Year Bug
Re: Bug ID 6342679: NSTimestamp Happy New Year Bug
- Subject: Re: Bug ID 6342679: NSTimestamp Happy New Year Bug
- From: Lachlan Deck <email@hidden>
- Date: Fri, 16 Jan 2009 06:41:06 +1100
On 15/01/2009, at 6:43 AM, Ken Anderson wrote:
Me comment - if mismatches between APIs from different vendors were
always considered bugs, I could think of a few hundred off the top
of my head :)
:)
The NSTimestamp documentation doesn't say what a NULL timezone will
represent, so it's unfortunately undetermined.
In theory that's true. They could in future decide to change said
unspecified behaviour ;-)
Empirical data supports the idea that it just ignores timezone
conversion, which makes sense, since it's only going to use the
timezone to modify the date/time passed in before it stores it as
GMT. I would guess the code looks like:
if (timezone != null) {
// morph year/month/day/hour/minute based on passed in time zone
}
Roughly correct says my jady friend.
On Jan 14, 2009, at 12:11 PM, Barði Einarsson wrote:
OK, so when one gives NSTimestamp a null as a time zone
it uses GMT, not the default time zone.
I'd suggest you want to (for clarity in your code) pass
NSTimeZone.getGMT() or NSTimeZone.defaultTimeZone(). Otherwise you're
again opening yourself up for misbehaviour in /your/ code.
However, GregorianCalendar uses the default time zone.
Which is exactly what the api for GC states. NSTimestamp is not GC.
NSTimestamp clearly states ... All NSTimestamps are 64 bit millisecond
offsets since the reference date in the reference time zone.
So, if one wants to use time zones with NSTimestamp, not
just GMT, one has to explicitly specify the default time
zone in the NSTimestamp constructor.
Correct.
Otherwise, GregorianCalendar will give one unexpected results.
Nope. GC will give the results it states it will -- according to the
timezone it is constructed with or changed to (via setter). In both
circumstances the underlying time is exactly the same millisecond
offset from the reference date. It, as Ken stated, simply returns the
values for year, month, date according to the tz you've chosen.
The mismatch in the use of default time zones between NSTimestamp
and GregorianCalendar is a bug.
Comments please.
There is no mismatch. They are different classes. I say again - it's
your misunderstanding that has the bug :)
ldecks-macbook-pro:Java ldeck$ java -cp /Library/WebObjects/lib/
JavaFoundation.jar:. HappyNewYearReykjavik
TimeZone:Atlantic/Reykjavik
timestamp (UTC): 1964-01-01 01:00:00 Etc/GMT
yearFromDate:1964
TimeZone:Atlantic/Reykjavik
timestamp (UTC): 1964-01-01 01:00:00 Etc/GMT
yearFromDate:1964
TimeZone:Etc/GMT
timestamp (UTC): 1964-01-01 00:00:00 Etc/GMT
yearFromDate:1964
ldecks-macbook-pro:Java ldeck$
import java.util.Calendar;
import java.util.TimeZone;
import com.webobjects.foundation.NSTimeZone;
import com.webobjects.foundation.NSTimestamp;
public class HappyNewYearReykjavik {
static {
TimeZone.setDefault(TimeZone.getTimeZone("Atlantic/
Reykjavik"));
NSTimeZone.setDefault(TimeZone.getTimeZone("Atlantic/
Reykjavik"));
}
public static void main(String[] args) {
HappyNewYearReykjavik.happyNewYear(1964);
HappyNewYearReykjavik.happyNewYear(1964,
NSTimeZone.defaultTimeZone());
HappyNewYearReykjavik.happyNewYear(1964, NSTimeZone.getGMT());
}
/**
* @param timestamp - the timestamp to use.
* @return the year from the given date in the default timezone.
*/
public static int yearFromDate(NSTimestamp timestamp) {
return yearFromDate(timestamp, TimeZone.getDefault());
}
/**
* @param timestamp - the timestamp to use.
* @param timezone - the timezone to use or, if null, the default
timezone.
* @return the year from the given date in the given timezone.
*/
public static int yearFromDate(NSTimestamp timestamp, TimeZone
timezone) {
int result = 0;
if (timestamp != null) {
TimeZone tz = timezone == null ? TimeZone.getDefault() :
timezone;
Calendar cal = Calendar.getInstance(tz);
cal.setTime(timestamp);
result = cal.get(Calendar.YEAR);
}
return result;
}
/**
* Prints the year in the default timezone.
*/
public static void happyNewYear(int year) {
happyNewYear(year, NSTimeZone.defaultTimeZone());
}
/**
* Prints the year in the given timezone.
* If the given timezone is null then the default timezone is used.
*/
public static void happyNewYear(int year, NSTimeZone timezone) {
if (timezone == null) {
happyNewYear(year);
} else {
System.out.println("TimeZone:" + timezone.getID());
NSTimestamp timestamp = new NSTimestamp(year, 1, 1, 0, 0,
0, timezone);
System.out.println("timestamp (UTC): " + timestamp);
System.out.println("yearFromDate:" +
yearFromDate(timestamp, timezone));
}
}
}
with regards,
--
Lachlan Deck
_______________________________________________
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