Mac: Set Time was not respecting the timezone (fixes #2459894)

ver1_6_1
Da Woon Jung 2009-02-11 12:56:16 +00:00
parent 3c71abc625
commit 6ceb3e28ff
5 changed files with 61 additions and 28 deletions

View File

@ -3,11 +3,10 @@
// celestia // celestia
// //
// Created by Bob Ippolito on Fri Jun 07 2002. // Created by Bob Ippolito on Fri Jun 07 2002.
// Copyright (c) 2002 Chris Laurel. All rights reserved. // Copyright (C) 2001-9, the Celestia Development Team
// //
#import "Astro.h" #import "Astro.h"
#import "Astro_PrivateAPI.h"
#import "Observer.h" #import "Observer.h"
#import "CelestiaUniversalCoord_PrivateAPI.h" #import "CelestiaUniversalCoord_PrivateAPI.h"
#import "CelestiaVector_PrivateAPI.h" #import "CelestiaVector_PrivateAPI.h"
@ -52,8 +51,6 @@ NSDictionary* coordinateDict;
} }
@end @end
@implementation Astro(PrivateAPI)
@end
@implementation Astro @implementation Astro
+(NSString*)stringWithCoordinateSystem:(NSNumber*)n +(NSString*)stringWithCoordinateSystem:(NSNumber*)n
@ -69,7 +66,15 @@ NSDictionary* coordinateDict;
+(void)initialize +(void)initialize
{ {
// compiler macro would be prettier I guess // compiler macro would be prettier I guess
coordinateDict = [[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:ObserverFrame::Universal],@"Universal",[NSNumber numberWithInt:ObserverFrame::Ecliptical],@"Ecliptical",[NSNumber numberWithInt:ObserverFrame::Equatorial],@"Equatorial",[NSNumber numberWithInt:ObserverFrame::BodyFixed],@"Geographic",[NSNumber numberWithInt:ObserverFrame::ObserverLocal],@"ObserverLocal",[NSNumber numberWithInt:ObserverFrame::PhaseLock],@"PhaseLock",[NSNumber numberWithInt:ObserverFrame::Chase],@"Chase",nil,nil] retain]; coordinateDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:ObserverFrame::Universal], @"Universal",
[NSNumber numberWithInt:ObserverFrame::Ecliptical], @"Ecliptical",
[NSNumber numberWithInt:ObserverFrame::Equatorial], @"Equatorial",
[NSNumber numberWithInt:ObserverFrame::BodyFixed], @"Geographic",
[NSNumber numberWithInt:ObserverFrame::ObserverLocal], @"ObserverLocal",
[NSNumber numberWithInt:ObserverFrame::PhaseLock], @"PhaseLock",
[NSNumber numberWithInt:ObserverFrame::Chase], @"Chase",
nil];
} }
+(NSNumber*)sphereIlluminationFraction:(CelestiaVector*)spherePos viewerPosition:(CelestiaVector*)viewerPos +(NSNumber*)sphereIlluminationFraction:(CelestiaVector*)spherePos viewerPosition:(CelestiaVector*)viewerPos
{ {
@ -96,9 +101,13 @@ NSDictionary* coordinateDict;
+(NSNumber*)julianDate:(NSDate *)date +(NSNumber*)julianDate:(NSDate *)date
{ {
NSTimeZone *prevTimeZone = [NSTimeZone defaultTimeZone];
// UTCtoTDB() expects GMT
[NSTimeZone setDefaultTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"GMT"]];
NSDate *roundedDate = nil;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
[currentCalendar setTimeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateComponents *comps = [currentCalendar components: NSDateComponents *comps = [currentCalendar components:
NSEraCalendarUnit | NSEraCalendarUnit |
NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
@ -111,18 +120,26 @@ NSDictionary* coordinateDict;
astroDate.hour = [comps hour]; astroDate.hour = [comps hour];
astroDate.minute = [comps minute]; astroDate.minute = [comps minute];
astroDate.seconds = [comps second]; astroDate.seconds = [comps second];
// -[NSDateComponents second] is rounded to an integer,
// so have to calculate and add decimal part
roundedDate = [currentCalendar dateFromComponents: comps];
[currentCalendar release];
#else #else
NSCalendarDate *cd = [date dateWithCalendarFormat: nil timeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; NSCalendarDate *cd = [date dateWithCalendarFormat: nil timeZone: nil];
// astro::Date requires GMT (UTC) times
astro::Date astroDate([cd yearOfCommonEra], astro::Date astroDate([cd yearOfCommonEra],
[cd monthOfYear], [cd monthOfYear],
[cd dayOfMonth]); [cd dayOfMonth]);
astroDate.hour = [cd hourOfDay]; // takes DST in to account astroDate.hour = [cd hourOfDay]; // takes DST in to account
astroDate.minute = [cd minuteOfHour]; astroDate.minute = [cd minuteOfHour];
astroDate.seconds = [cd secondOfMinute]; astroDate.seconds = [cd secondOfMinute];
roundedDate = cd;
#endif #endif
double jd = (double)astroDate; NSTimeInterval extraSeconds = [date timeIntervalSinceDate: roundedDate];
astroDate.seconds += extraSeconds;
[NSTimeZone setDefaultTimeZone: prevTimeZone];
double jd = astro::UTCtoTDB(astroDate);
return [NSNumber numberWithDouble: jd]; return [NSNumber numberWithDouble: jd];
} }

View File

@ -377,13 +377,14 @@ NSString* fatalErrorMessage;
[ appCore setStartURL: pendingUrl ]; [ appCore setStartURL: pendingUrl ];
} }
// Settings used to be loaded after starting simulation due to
// timezone setting requiring simulation time, but this dependency
// has been removed. In fact timezone needs to be set in order to
// correctly set the simulation time so settings loaded before starting.
[settings loadUserDefaults];
[appCore start:[NSDate date]]; [appCore start:[NSDate date]];
// load settings
// Delay applying settings until after setting simulation time
// This allows the timezone to be set correctly
[settings loadUserDefaults];
ready = YES; ready = YES;
timer = [[NSTimer timerWithTimeInterval: 0.01 target: self selector:@selector(timeDisplay) userInfo:nil repeats:YES] retain]; timer = [[NSTimer timerWithTimeInterval: 0.01 target: self selector:@selector(timeDisplay) userInfo:nil repeats:YES] retain];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

View File

@ -39,6 +39,8 @@
-(double) time ; -(double) time ;
-(void) setTime: (double) value ; -(void) setTime: (double) value ;
-(int) timeZone ;
-(void) setTimeZone: (int) value ;
-(double) timeScale ; -(double) timeScale ;
-(void) setTimeScale: (double) value ; -(void) setTimeScale: (double) value ;

View File

@ -3,7 +3,7 @@
// celestia // celestia
// //
// Created by Hank Ramsey on Fri Oct 29 2004. // Created by Hank Ramsey on Fri Oct 29 2004.
// Copyright (C) 2007, Celestia Development Team // Copyright (C) 2001-9, the Celestia Development Team
// //
#import "CelestiaSettings.h" #import "CelestiaSettings.h"
@ -642,20 +642,11 @@ FEATUREMETHODS(Other)
// Time settings // Time settings
// Timezone values are inverted to maintain backward compatibility
-(int) timeZone { return appCore->getTimeZoneBias()==0 ? 1 : 0; } -(int) timeZone { return appCore->getTimeZoneBias()==0 ? 1 : 0; }
-(void) setTimeZone: (int) value -(void) setTimeZone: (int) value
{ {
NSTimeZone *tz; appCore->setTimeZoneBias(0==value ? 1 : 0);
NSDate *date = [NSDate dateWithJulian: [NSNumber numberWithDouble: [self time]]];
if (0 == value)
{
tz = [NSTimeZone defaultTimeZone];
}
else
{
tz = [NSTimeZone timeZoneWithAbbreviation: @"GMT"];
}
[[CelestiaAppCore sharedAppCore] setTimeZone: tz withDate: date];
} }
-(int) dateFormat { return appCore->getDateFormat(); } -(int) dateFormat { return appCore->getDateFormat(); }

View File

@ -91,6 +91,7 @@
NSString *errorString = nil; NSString *errorString = nil;
NSString* dateString = [dateField stringValue]; NSString* dateString = [dateField stringValue];
NSString* timeString = [timeField stringValue]; NSString* timeString = [timeField stringValue];
BOOL useUTC = (0 != [[CelestiaSettings shared] timeZone]);
if ( [timeString isEqualToString: @""] ) if ( [timeString isEqualToString: @""] )
{ {
@ -126,6 +127,17 @@
inputString = [ [ dateString stringByAppendingString: @" " ] inputString = [ [ dateString stringByAppendingString: @" " ]
stringByAppendingString: timeString ]; stringByAppendingString: timeString ];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
if (useUTC)
{
[dateTimeFormat setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"GMT"]];
}
else
{
[NSTimeZone resetSystemTimeZone];
[dateTimeFormat setTimeZone: [NSTimeZone systemTimeZone]];
}
#endif
dateValid = [ dateTimeFormat getObjectValue: &date dateValid = [ dateTimeFormat getObjectValue: &date
forString: inputString forString: inputString
errorDescription: &errorString ]; errorDescription: &errorString ];
@ -157,13 +169,23 @@
if (dtParts && tmParts && if (dtParts && tmParts &&
[dtParts count] > 0 && [tmParts count] > 0) [dtParts count] > 0 && [tmParts count] > 0)
{ {
NSTimeZone *tz = nil;
if (useUTC)
{
tz = [NSTimeZone timeZoneWithAbbreviation: @"GMT"];
}
else
{
[NSTimeZone resetSystemTimeZone];
tz = [NSTimeZone systemTimeZone];
}
date = [NSCalendarDate dateWithYear: [[dtParts objectAtIndex: 2] intValue] date = [NSCalendarDate dateWithYear: [[dtParts objectAtIndex: 2] intValue]
month: [[dtParts objectAtIndex: 0] intValue] month: [[dtParts objectAtIndex: 0] intValue]
day: [[dtParts objectAtIndex: 1] intValue] day: [[dtParts objectAtIndex: 1] intValue]
hour: [[tmParts objectAtIndex: 0] intValue] hour: [[tmParts objectAtIndex: 0] intValue]
minute: [[tmParts objectAtIndex: 1] intValue] minute: [[tmParts objectAtIndex: 1] intValue]
second: [[tmParts objectAtIndex: 2] intValue] second: [[tmParts objectAtIndex: 2] intValue]
timeZone: [NSTimeZone defaultTimeZone]]; timeZone: tz];
} }
#endif #endif
jd = [ Astro julianDate: date]; jd = [ Astro julianDate: date];