Force NSCalendarDate to show dates in UTC

Hi folks,

I have an app that calculates Julian Day from Gregorian Date and vice-versa. However, I would like to force NSCalendarDate to use the timezone regarding UTC and not local. Where in the script should I declare such parameter?

Any help is welcome!

Best,
mairan

Model: MacBook White
Browser: Firefox 13.0.1
Operating System: Mac OS X (10.7)

Perhaps you could show some code – I’m not sure how you’re getting at NSCalendarDate from AppleScript (and NSCalendarDate has been deprecated since OS X 10.6).

Sure! Here it goes. This is the controller. I also have the JulianDate.m which is a class (I can post it later).


#import "Controller.h"
#import "JulianDate.h"

@implementation Controller

- (id)init {
	if( self = [super init] ){
		[NSApp setDelegate:self]; 
		[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
		date = [JulianDate calendarDate];
	}
	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	return self;
}

- (void)awakeFromNib {
	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	// creo l'array con i mesi
	NSMutableArray *mesi = [[NSMutableArray alloc] init];
	
	NSString *nomeStringa; 
	int i, totMesi = 12;
	for(i=1;i<=totMesi;i++){
		nomeStringa = [NSString stringWithFormat:@"month_%d", i];
		[mesi addObject:NSLocalizedString(nomeStringa, nil)];
	}
	
	// riempio la lista dei mesi
	[listaMesi removeAllItems];
	[listaMesi addItemsWithTitles:mesi];
	
	// setto la data a quella attuale
	[self settaDataOdierna];

	// centro la finestra
	[[datagiuliana window] center];
	
}

- (void)dealloc {
	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	[date release];
	[super release];
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	// quando viene chiusa la finestra principale viene
	// terminata l'applicazione
	return YES;
}

- (void)controlTextDidChange:(NSNotification *)aNotification {
	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	// elimino la referenze per l'istanza date
	[date release];
				
	// converto la data giuliana in data normale
	if( [aNotification object] == datagiuliana ) {
		
		if( [[datagiuliana stringValue] isEqualToString:@""] ){
			return;
		}
		
		// creo il nuovo oggetto date
		date = [JulianDate createFromJulianDate:[datagiuliana doubleValue]];
		
		// setto i valori alla data della macchina
		[giorno setIntValue:[date dayOfMonth]];
		[listaMesi selectItemAtIndex:[date monthOfYear]-1];
		[anno setIntValue:[date yearOfCommonEra]];
		
		[ore		setIntValue:[date hourOfDay] ];		
		[minuti		setIntValue:[date minuteOfHour] ];
		[secondi	setIntValue:[date secondOfMinute] ];
		if( [binaryCheckBox state] == NSOnState ){
			[cp setStringValue: [NSString stringWithFormat:@"%0.6f", [date binmod]]];
			[jdt0 setStringValue: [NSString stringWithFormat:@"%0.2f", [date pontozero]]];
			[per setStringValue: [NSString stringWithFormat:@"%0.4f", [date period]]];
		}else{
			[cp setStringValue:@""];
			[jdt0 setStringValue:@""];
			[per setStringValue:@""];
		}
		
	}else{

		[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
		// creo il nuovo oggetto date
		date = [JulianDate dateWithYear: [anno intValue]
								  month: [listaMesi indexOfSelectedItem]+1
									day: [giorno intValue]
								   hour: [ore intValue]
								 minute: [minuti intValue]
								 second: [secondi intValue]
								//timeZone: [NSTimeZone systemTimeZone]];
							   timeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

		@try {
			[datagiuliana setStringValue: [NSString stringWithFormat:@"%0.5f", [date convertToJulianDate]]];
			if( [binaryCheckBox state] == NSOnState ){
				[cp setStringValue: [NSString stringWithFormat:@"%0.6f", [date binmod]]];
				[jdt0 setStringValue: [NSString stringWithFormat:@"%0.2f", [date pontozero]]];
				[per setStringValue: [NSString stringWithFormat:@"%0.4f", [date period]]];
			}else{
				[cp setStringValue:@""];
				[jdt0 setStringValue:@""];
				[per setStringValue:@""];
			}

		} @catch(NSException *ex){
			[datagiuliana setStringValue:@""];
			NSRunCriticalAlertPanel(nil, [ex reason], nil, nil, nil);
		}
	}
	
	// aggiorno i valori degli steppers
	[stepperGiorno setIntValue:[date dayOfMonth]];
	[stepperAnno setIntValue:[date yearOfCommonEra]];
	[stepperOre setIntValue:[date hourOfDay]];
	[stepperMinuti setIntValue:[date minuteOfHour]];
	[stepperSecondi setIntValue:[date secondOfMinute]];
	
	[date retain];
}

- (IBAction)dataOdierna:(id)sender {

	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	// setto la data a quella attuale
	[self settaDataOdierna];

}

- (IBAction)modificaPopUpMese:(id)sender {
	date = [JulianDate dateWithYear: [anno intValue]
							  month: [listaMesi indexOfSelectedItem]+1
								day: [giorno intValue]
							   hour: [ore intValue]
							 minute: [minuti intValue]
							 second: [secondi intValue]
						   //timeZone: [NSTimeZone systemTimeZone]];
						   timeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
	
	@try{	
		[datagiuliana setStringValue: [NSString stringWithFormat:@"%0.5f", [date convertToJulianDate]]];
		if( [binaryCheckBox state] == NSOnState ){
			[cp setStringValue: [NSString stringWithFormat:@"%0.6f", [date binmod]]];
			[jdt0 setStringValue: [NSString stringWithFormat:@"%0.2f", [date pontozero]]];
			[per setStringValue: [NSString stringWithFormat:@"%0.4f", [date period]]];
			//[jdt0 setStringValue:@"not imp. yet"];
			//[per setStringValue:@"not imp. yet"];
		}else{
			[cp setStringValue:@""];
			[jdt0 setStringValue:@""];
			[per setStringValue:@""];
		}

	} @catch(NSException *ex){
		[datagiuliana setStringValue:@""];
		NSRunCriticalAlertPanel(nil, [ex reason], nil, nil, nil);
	}

	[date retain];
}

- (IBAction)cambiaTimer:(id)sender {

	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	if( [timerCheckBox state] == NSOnState ){
		timer = [NSTimer scheduledTimerWithTimeInterval:1
												 target:self
											   selector:@selector(settaDataOdierna)
											   userInfo:NULL
												repeats:YES];
	}else{
		[timer invalidate];		
	}

}


- (void)settaDataOdierna {
	[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	// inizializzo l'oggetto JulianDate
	date = [JulianDate calendarDate];
			
	// setto i valori alla data della macchina
	[giorno setIntValue:[date dayOfMonth]];
	[listaMesi selectItemAtIndex:[date monthOfYear]-1];
	[anno setIntValue:[date yearOfCommonEra]];
	
	[ore		setIntValue:[date hourOfDay] ];
	[minuti		setIntValue:[date minuteOfHour] ];
	[secondi	setIntValue:[date secondOfMinute] ];

	// calcolo subito la data giuliana
	[datagiuliana setStringValue: [NSString stringWithFormat:@"%0.5f", [date convertToJulianDate]]];
	if( [binaryCheckBox state] == NSOnState ){
		[cp setStringValue: [NSString stringWithFormat:@"%0.6f", [date binmod]]];
		[jdt0 setStringValue: [NSString stringWithFormat:@"%0.2f", [date pontozero]]];
		[per setStringValue: [NSString stringWithFormat:@"%0.4f", [date period]]];
	}else{
		[cp setStringValue:@""];
		[jdt0 setStringValue:@""];
		[per setStringValue:@""];
	}	

	// aggiorno i valori degli steppers
	[stepperGiorno setIntValue:[date dayOfMonth]];
	[stepperAnno setIntValue:[date yearOfCommonEra]];
	[stepperOre setIntValue:[date hourOfDay]];
	[stepperMinuti setIntValue:[date minuteOfHour]];
	[stepperSecondi setIntValue:[date secondOfMinute]];
	
	[date retain];
}

@end


Model: MacBook White
Browser: Firefox 13.0.1
Operating System: Mac OS X (10.7)

I’m afraid you’ve come to the wrong place – this is an AppleScript forum, not Objective-C. I suspect Google will be your best bet.

Consider that NSCalendarDate is deprecated since 10.6

Edit: by the way, all lines [NSTimeZone timeZoneWithAbbreviation:@“GMT”] do actually nothing.
A time zone object is created, but never been used

Hello

To Shane and Stephan

It seems that some users don’t understand what means the word « deprecated »
I receive at least once a week pieces of code using « Info for » which is deprecated for years.

Yvan KOENIG (VALLAURIS, France) vendredi 6 juillet 2012 10:47:54

Possibly, getting UTC should be done in AppleScript itself. As a starting point:

on date_to_utc_string(dt)
	tell application "System Events"
		set pl_item to (make new property list item with properties {value:dt})
		set pl to text of pl_item
	end tell
	set old_delims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"<date>", "</date>"}
	set date_string to text item 2 of pl
	set AppleScript's text item delimiters to old_delims
	return date_string
end date_to_utc_string

Regards, Jürgen

Yvan,

I think there’s another problem: some of the people applying the term don’t understand its meaning. That would explain why some deprecated commands live on and on, well after they were first deprecated.

Hi all,

Thank you for your kindly responses and forgive me for being such a newbie.

m.