Julian date setting added to Set Time panel

ver1_5_1
Da Woon Jung 2007-12-16 21:22:52 +00:00
parent 52f1319053
commit 73e12ea7b0
13 changed files with 244 additions and 39 deletions

View File

@ -16,7 +16,38 @@ NSDictionary* coordinateDict;
@implementation NSDate(AstroAPI)
+(NSDate*)dateWithJulian:(NSNumber*)jd
{
return [NSDate dateWithTimeIntervalSince1970:[[Astro julianDateToSeconds:jd] doubleValue]-[[Astro julianDateToSeconds:[NSNumber numberWithDouble:astro::Date(1970,1,1)]] doubleValue]];
NSDate *date = nil;
astro::Date astroDate([jd doubleValue]);
int year = astroDate.year;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
[currentCalendar setTimeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
int era = 1;
if (year < 1)
{
era = 0;
year = 1 - year;
}
[comps setEra: era];
[comps setYear: year];
[comps setMonth: astroDate.month];
[comps setDay: astroDate.day];
[comps setHour: astroDate.hour];
[comps setMinute: astroDate.minute];
[comps setSecond: (int)astroDate.seconds];
date = [currentCalendar dateFromComponents: comps];
#else
date = [NSCalendarDate dateWithYear: year
month: astroDate.month
day: astroDate.day
hour: astroDate.hour
minute: astroDate.minute
second: (int)astroDate.seconds
timeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
#endif
return date;
}
@end
@ -64,7 +95,34 @@ NSDictionary* coordinateDict;
+(NSNumber*)julianDate:(NSDate *)date
{
return [NSNumber numberWithDouble:([[Astro secondsToJulianDate:[NSNumber numberWithDouble:(double)[date timeIntervalSince1970]]] doubleValue]+(double)astro::Date(1970,1,1))];
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
[currentCalendar setTimeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateComponents *comps = [currentCalendar components:
NSEraCalendarUnit |
NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
fromDate: date];
int era = [comps era];
int year = [comps year];
if (era < 1) year = 1 - year;
astro::Date astroDate(year, [comps month], [comps day]);
astroDate.hour = [comps hour];
astroDate.minute = [comps minute];
astroDate.seconds = [comps second];
#else
NSCalendarDate *cd = [date dateWithCalendarFormat: nil timeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
// astro::Date requires GMT (UTC) times
astro::Date astroDate([cd yearOfCommonEra],
[cd monthOfYear],
[cd dayOfMonth]);
astroDate.hour = [cd hourOfDay]; // takes DST in to account
astroDate.minute = [cd minuteOfHour];
astroDate.seconds = [cd secondOfMinute];
#endif
double jd = (double)astroDate;
return [NSNumber numberWithDouble: jd];
}
+(NSNumber*)speedOfLight

View File

@ -84,6 +84,7 @@
};
SUPERCLASS = NSWindowController;
},
{CLASS = NSDatePicker; LANGUAGE = ObjC; SUPERCLASS = NSControl; },
{
CLASS = RenderPanelController;
LANGUAGE = ObjC;
@ -100,7 +101,7 @@
ACTIONS = {setTime = id; showWindow = id; };
CLASS = SetTimeWindowController;
LANGUAGE = ObjC;
OUTLETS = {dateField = NSTextField; timeField = NSTextField; };
OUTLETS = {dateField = NSTextField; jdField = NSTextField; timeField = NSTextField; };
SUPERCLASS = NSWindowController;
},
{CLASS = SplashImageView; LANGUAGE = ObjC; SUPERCLASS = NSImageView; },

Binary file not shown.

View File

@ -9,7 +9,13 @@
@interface SetTimeWindowController : NSWindowController
{
IBOutlet NSTextField *dateField;
IBOutlet NSTextField *jdField;
IBOutlet NSTextField *timeField;
NSDateFormatter *dateTimeFormat;
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
NSDateFormatter *bcFormat;
NSDateFormatter *zeroFormat;
#endif
}
- (IBAction)setTime:(id)sender;
@end

View File

@ -3,68 +3,205 @@
// celestia
//
// Created by Bob Ippolito on Tue May 28 2002.
// Copyright (c) 2002 Chris Laurel. All rights reserved.
// Copyright (C) 2007, Celestia Development Team
//
#import "SetTimeWindowController.h"
#import "CelestiaAppCore.h"
#import "CelestiaSimulation.h"
#import "Astro.h"
@interface SetTimeWindowController(Private)
- (NSNumber *) julianDateFromDateAndTime;
@end
@implementation SetTimeWindowController
- (void) awakeFromNib
{
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
dateTimeFormat = [[NSDateFormatter alloc] init];
[ dateTimeFormat setFormatterBehavior: NSDateFormatterBehavior10_4 ];
[ dateTimeFormat setDateFormat: @"MM/dd/uuuu HH:mm:ss" ];
// uuuu handles -ve (BCE) years
// 1 BCE = 0 (not -1)
// Reference: Unicode Technical Standard #35
// http://unicode.org/reports/tr35/tr35-4.html
#else
dateTimeFormat = [[NSDateFormatter alloc] initWithDateFormat:
@"%m/%d/%Y %H:%M:%S"
allowNaturalLanguage: NO ];
bcFormat = [[NSDateFormatter alloc] initWithDateFormat:
@"%m/%d/-%Y %H:%M:%S"
allowNaturalLanguage: NO ];
zeroFormat = [[NSDateFormatter alloc] initWithDateFormat:
@"%m/%d/%y %H:%M:%S"
allowNaturalLanguage: NO ];
// Due to a "reverse Y2K" bug, the 4-digit %Y format rejects
// the year 0 (1 BCE), even if 0000 is entered.
// But the 2-digit %y format ends up working!
#endif
}
- (void) dealloc
{
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
[zeroFormat release];
[bcFormat release];
#endif
[dateTimeFormat release];
[super dealloc];
}
- (IBAction)setTime:(id)sender
{
CelestiaSimulation *sim;
NSString* dateString = [dateField stringValue];
NSString* timeString = [timeField stringValue];
NSString* inputString;
NSString* fmtString = @"%m/%d/%Y %H:%M:%S";
NSCalendarDate* cdate;
NSNumber* jd;
sim = [[CelestiaAppCore sharedAppCore] simulation];
if ( [dateString isEqualToString: @""] && [timeString isEqualToString: @""])
{
NSRunAlertPanel(NSLocalizedString(@"No Date or Time Entered",@""),
NSLocalizedString(@"Please enter a date and/or time.",@""),
nil,nil,nil);
return;
NSRunAlertPanel(NSLocalizedString(@"No Date or Time Entered",@""),
NSLocalizedString(@"Please enter a date and/or time.",@""),
nil,nil,nil);
return;
}
CelestiaSimulation *sim = [[CelestiaAppCore sharedAppCore] simulation];
NSNumber *jd = [self julianDateFromDateAndTime];
if (jd)
[sim setDate: jd ];
}
- (NSNumber *) julianDateFromDateAndTime
{
NSNumber *jd = nil;
NSDate *date = nil;
BOOL dateValid = NO;
NSString *inputString = nil;
NSString *errorString = nil;
NSString* dateString = [dateField stringValue];
NSString* timeString = [timeField stringValue];
if ( [timeString isEqualToString: @""] )
{
// NSLog(@"emptyTime");
timeString = @"00:00:00";
}
else
{
NSArray* pieces = [ timeString componentsSeparatedByString: @":" ];
if ( [[pieces objectAtIndex: [pieces count]-1 ] isEqualToString: @"" ])
timeString = [timeString stringByAppendingString: @"00" ];
timeString = [timeString stringByAppendingString: @"00" ];
if ([ pieces count] == 1)
timeString = [ timeString stringByAppendingString: @":00:00" ];
timeString = [ timeString stringByAppendingString: @":00:00" ];
else if ([ pieces count] == 2)
timeString = [ timeString stringByAppendingString: @":00" ];
// NSLog(timeString);
timeString = [ timeString stringByAppendingString: @":00" ];
}
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
{
// 1 BCE - only works if year is 2 digits (00)
// so transform 0000 etc to 2 digits
NSArray* pieces = [dateString componentsSeparatedByString: @"/"];
if ([pieces count] == 3)
{
NSString *yearString = [pieces objectAtIndex: 2];
NSScanner *zeroScan = [NSScanner scannerWithString: yearString];
int yearVal = 0;
if ([zeroScan scanInt: &yearVal] && 0 == yearVal)
{
dateString = [NSString stringWithFormat: @"%@/%@/00", [pieces objectAtIndex: 0], [pieces objectAtIndex: 1]];
}
}
}
#endif
inputString = [ [ dateString stringByAppendingString: @" " ]
stringByAppendingString: timeString ];
cdate = [NSCalendarDate dateWithString: inputString calendarFormat: fmtString ];
if ( cdate == NULL )
{
// NSLog(@"bad date");
NSRunAlertPanel(NSLocalizedString(@"Improper Date or Time Format",@""),
NSLocalizedString(@"Please enter the date as \"mm/dd/yyyy\" and the time as \"hh:mm:ss\".",@""),
nil,nil,nil);
return;
}
// NSLog( [cdate description] );
// float fjd = [jd intValue];
// NSLog ( @"jdate = %", fjd);
jd = [ Astro julianDate: cdate];
[sim setDate: jd ];
dateValid = [ dateTimeFormat getObjectValue: &date
forString: inputString
errorDescription: &errorString ];
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
if (!dateValid)
{
// BCE dates (except 1 BCE)
dateValid = [ bcFormat getObjectValue: &date
forString: inputString
errorDescription: &errorString ];
}
if (!dateValid)
{
// 1 BCE - only works if year is 2 digits (00)
dateValid = [ zeroFormat getObjectValue: &date
forString: inputString
errorDescription: &errorString ];
}
#endif
if (dateValid)
{
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
// +[Astro julianDate:] also converts NSDate->NSCalendarDate
// but it doesn't work so well with -ve (BCE) dates
// so help it by passing a ready-made NSCalendarDate
NSArray *dtParts = [dateString componentsSeparatedByString: @"/"];
NSArray *tmParts = [timeString componentsSeparatedByString: @":"];
if (dtParts && tmParts &&
[dtParts count] > 0 && [tmParts count] > 0)
{
date = [NSCalendarDate dateWithYear: [[dtParts objectAtIndex: 2] intValue]
month: [[dtParts objectAtIndex: 0] intValue]
day: [[dtParts objectAtIndex: 1] intValue]
hour: [[tmParts objectAtIndex: 0] intValue]
minute: [[tmParts objectAtIndex: 1] intValue]
second: [[tmParts objectAtIndex: 2] intValue]
timeZone: [NSTimeZone defaultTimeZone]];
}
#endif
jd = [ Astro julianDate: date];
}
return jd;
}
- (void) controlTextDidEndEditing: (NSNotification *) aNotification
{
id sender = [aNotification object];
if (dateField == sender || timeField == sender)
{
if ([[dateField stringValue] isEqualToString: @""])
return;
NSNumber *jd = [self julianDateFromDateAndTime];
if (jd)
{
[ jdField setDoubleValue: [jd doubleValue] ];
}
else
{
NSRunAlertPanel(NSLocalizedString(@"Improper Date or Time Format",@""),
NSLocalizedString(@"Please enter the date as \"mm/dd/yyyy\" and the time as \"hh:mm:ss\".",@""),
nil,nil,nil);
}
}
else if (jdField == sender)
{
if ([[jdField stringValue] isEqualToString: @""])
return;
NSDate *date = [ NSDate dateWithJulian: [NSNumber numberWithDouble: [jdField doubleValue] ] ];
NSString *dateTimeString = nil;
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
dateTimeString = [dateTimeFormat stringFromDate: date];
#else
dateTimeString = [dateTimeFormat stringForObjectValue: date];
#endif
if (dateTimeString && [dateTimeString length] > 0)
{
NSArray *components = [dateTimeString componentsSeparatedByString: @" "];
if (components && [components count] > 1)
{
[dateField setStringValue: [components objectAtIndex: 0]];
[timeField setStringValue: [components objectAtIndex: 1]];
}
}
}
}
@end

View File

@ -2064,6 +2064,8 @@
E50E8708097436A4006687D3 /* Universal_Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
MACOSX_DEPLOYMENT_TARGET_i386 = 10.4;
MACOSX_DEPLOYMENT_TARGET_ppc = 10.3;
PREBINDING = NO;
SDKROOT = /Developer/SDKs/MacOSX10.4u.sdk;
STRIP_INSTALLED_PRODUCT = NO;

View File

@ -95,7 +95,7 @@
ACTIONS = {setTime = id; showWindow = id; };
CLASS = SetTimeWindowController;
LANGUAGE = ObjC;
OUTLETS = {dateField = NSTextField; timeField = NSTextField; };
OUTLETS = {dateField = NSTextField; jdField = NSTextField; timeField = NSTextField; };
SUPERCLASS = NSWindowController;
},
{CLASS = SplashImageView; LANGUAGE = ObjC; SUPERCLASS = NSImageView; },

View File

@ -25,8 +25,9 @@
</array>
<key>IBOpenObjects</key>
<array>
<integer>29</integer>
<integer>1659</integer>
<integer>29</integer>
<integer>716</integer>
</array>
<key>IBSystem Version</key>
<string>8S165</string>

Binary file not shown.

View File

@ -95,7 +95,7 @@
ACTIONS = {setTime = id; showWindow = id; };
CLASS = SetTimeWindowController;
LANGUAGE = ObjC;
OUTLETS = {dateField = NSTextField; timeField = NSTextField; };
OUTLETS = {dateField = NSTextField; jdField = NSTextField; timeField = NSTextField; };
SUPERCLASS = NSWindowController;
},
{CLASS = SplashImageView; LANGUAGE = ObjC; SUPERCLASS = NSImageView; },

Binary file not shown.

View File

@ -100,7 +100,7 @@
ACTIONS = {setTime = id; showWindow = id; };
CLASS = SetTimeWindowController;
LANGUAGE = ObjC;
OUTLETS = {dateField = NSTextField; timeField = NSTextField; };
OUTLETS = {dateField = NSTextField; jdField = NSTextField; timeField = NSTextField; };
SUPERCLASS = NSWindowController;
},
{CLASS = SplashImageView; LANGUAGE = ObjC; SUPERCLASS = NSImageView; },

Binary file not shown.