Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: MetroWind/TimeConv
base: 529b860c1d
...
head fork: MetroWind/TimeConv
compare: 153db0cd07
  • 2 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 10, 2012
@MetroWind Move update timer to controller. 0803aae
Commits on Mar 11, 2012
@MetroWind Fix a giant :bug:.
NSDatePicker assumes that user keyboard input are in local time.  Therefore when date picker's time zone is not local, the string displayed in it is not string user input.  This forces us to set both picker's zone to local.
153db0c
View
2  TimeConv.xcodeproj/project.pbxproj
@@ -110,11 +110,11 @@
DA9B7E64150A832F00831928 /* AppDelegate.h */,
DA9B7E65150A832F00831928 /* AppDelegate.m */,
DA9B7E67150A832F00831928 /* MainMenu.xib */,
- DA9B7E59150A832F00831928 /* Supporting Files */,
DA9B7E72150A86FF00831928 /* TimeController.h */,
DA9B7E73150A86FF00831928 /* TimeController.m */,
DA9B7E75150A88DE00831928 /* TimeConverter.h */,
DA9B7E76150A88DE00831928 /* TimeConverter.m */,
+ DA9B7E59150A832F00831928 /* Supporting Files */,
DA034887150B0F4D00168B58 /* Resources */,
);
path = TimeConv;
View
14 TimeConv/AppDelegate.m
@@ -39,6 +39,10 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[Seperator setImageScaling: NSScaleProportionally];
[Seperator setImage: ImageSep];
+ NSTimeZone* LocalZone = [NSTimeZone localTimeZone];
+ [SrcTimeView setTimeZone: LocalZone];
+ [DestTimeView setTimeZone: LocalZone];
+
TimeControl = [[TimeController alloc] init];
TimeConv = [[TimeConverter alloc] init];
@@ -50,18 +54,13 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[self onBtnSrcZoneShortClick: self];
[self onBtnDestZoneShortClick: self];
- // Set default time zones
- [TimeConv setSrcZoneToLocal];
- [TimeConv setDestZoneToLocal];
// [TimeConv setDestZoneViewWithZone: [NSTimeZone localTimeZone]];
[SrcTimeZoneView setDelegate: TimeControl];
[DestTimeZoneView setDelegate: TimeControl];
-// TODO: get time zone working. Move timer to controller.
-
[TimeConv updateTimeSrcView];
- [TimeConv startUpdating];
+ [TimeControl startUpdating];
NSLog(@"Application initialized.");
}
@@ -92,13 +91,14 @@ - (IBAction)onBtnDestZoneShortClick:(id)sender
- (IBAction)onZoneSrcChange:(id)sender
{
+ NSLog(@"User is changing src zone...");
[TimeConv setSrcZoneWithStr:
[SrcTimeZoneView objectValueOfSelectedItem]];
}
- (IBAction)onZoneDestChange:(id)sender
{
- NSLog(@"Zone is changing to %@.", [DestTimeZoneView objectValueOfSelectedItem]);
+ NSLog(@"User is changing dest zone...");
[TimeConv setDestZoneWithStr:
[DestTimeZoneView objectValueOfSelectedItem]];
}
View
1  TimeConv/TimeController.h
@@ -17,4 +17,5 @@
- (void) shortSrcZone: (bool) shortp;
- (void) shortDestZone: (bool) shortp;
+- (void) startUpdating;
@end
View
13 TimeConv/TimeController.m
@@ -17,6 +17,8 @@ @implementation TimeController
id SrcZoneView;
id DestTimeView;
id DestZoneView;
+
+ NSTimer* UpdateTimer;
}
- (id)init
{
@@ -84,4 +86,15 @@ - (void) shortDestZone: (bool) shortp
// return FALSE;
//}
+- (void) startUpdating
+{
+ UpdateTimer =
+ [NSTimer scheduledTimerWithTimeInterval: 0.2
+ target: TimeConv
+ selector: @selector(updateViews)
+ userInfo: [NSDictionary dictionaryWithObject:[NSDate date] forKey:@"StartDate"]
+ repeats: YES];
+ NSLog(@"Timer fired.");
+}
+
@end
View
9 TimeConv/TimeConverter.h
@@ -9,10 +9,9 @@
#import <Foundation/Foundation.h>
@interface TimeConverter : NSObject
-@property (strong) NSDate* Time;
+@property (strong) NSDate* TimeSrc;
+@property (strong) NSDate* TimeDest;
-- (void) setTimeToNowByTimer: (NSTimer*) timer;
-- (void) setTimeToNow;
- (void) setSrcView: (id) view srcZoneView: (id) view;
- (void) setDestView: (id) view destZoneView: (id) view;
@@ -32,5 +31,7 @@
- (void) updateZoneDestView;
- (void) updateViews;
-- (void) startUpdating;
+
+- (NSDate*) convertTime: (NSDate*) time fromZone: (NSTimeZone*) zone_src
+ toZone: (NSTimeZone*) zone_dest;
@end
View
185 TimeConv/TimeConverter.m
@@ -6,6 +6,11 @@
// Copyright (c) 2012 Home. All rights reserved.
//
+// NSDatePicker always interpret user keyboard input as local time,
+// therefore if its time zone is set to be non-local, what user types
+// is not what s/he sees. Consequently, we have to set both date
+// pickers to local time.
+
#import "TimeConverter.h"
@implementation TimeConverter
@@ -20,8 +25,6 @@ @implementation TimeConverter
bool ZoneSrcShort;
bool ZoneDestShort;
- NSTimer* UpdateTimer;
-
id SrcTimeView;
id SrcZoneView;
id DestTimeView;
@@ -30,9 +33,12 @@ @implementation TimeConverter
NSDate* LastTimeSrc;
NSDate* LastTimeDest;
+ // For debug
+ NSDateFormatter* Formatter;
}
-@synthesize Time;
+@synthesize TimeSrc;
+@synthesize TimeDest;
- (id)init
{
@@ -43,38 +49,33 @@ - (id)init
ZoneNames = [NSTimeZone knownTimeZoneNames];
ZoneNamesShort = [[NSTimeZone abbreviationDictionary] allKeys];
LocalZone = [NSTimeZone localTimeZone];
+ ZoneSrc = LocalZone;
+ ZoneDest = LocalZone;
- Time = [NSDate date];
+ NSDate* Time = [NSDate date];
+ TimeSrc = Time;
+ TimeDest = Time;
+
+ Formatter = [[NSDateFormatter alloc] init];
+ [Formatter setTimeZone: LocalZone];
+ [Formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
NSLog(@"Time converter initialized.");
}
return self;
}
-- (void) setTimeToNowByTimer: (NSTimer*) timer
-{
- NSLog(@"Timer %@ requests to update src time.", timer);
- [self setTimeToNow];
- [self updateTimeSrcView];
-}
-
-- (void) setTimeToNow
-{
- Time = [NSDate date];
-}
-
-
- (void) setSrcView: (id) src_view srcZoneView: (id) src_zone_view
{
SrcTimeView = src_view;
SrcZoneView = src_zone_view;
- [SrcTimeView setDateValue: Time];
+ [SrcTimeView setDateValue: TimeSrc];
}
- (void) setDestView: (id) dest_view destZoneView: (id) dest_zone_view
{
DestTimeView = dest_view;
DestZoneView = dest_zone_view;
- [DestTimeView setDateValue: Time];
+ [DestTimeView setDateValue: TimeDest];
}
- (void) genSrcZoneViewList: (bool) shortp
@@ -117,25 +118,44 @@ - (void) genDestZoneViewList: (bool) shortp
- (void) setSrcZoneWithZone: (NSTimeZone*) zone
{
+ NSLog(@"Setting src zone to %@...", zone);
+
+ NSTimeZone* OldZone = ZoneSrc;
ZoneSrc = zone;
- [SrcTimeView setTimeZone: ZoneSrc];
+ // [SrcTimeView setTimeZone: ZoneSrc];
+ TimeSrc = [self convertTime: TimeSrc fromZone: OldZone
+ toZone:ZoneSrc];
+ NSLog(@"Src time changed to %@ because of zone change.",
+ [Formatter stringFromDate: TimeSrc]);
+
+ [self updateTimeSrcView];
[self updateZoneSrcView];
}
- (void) setDestZoneWithZone: (NSTimeZone*) zone
{
+ NSLog(@"Setting dest zone to %@...", zone);
+
+ NSTimeZone* OldZone = ZoneDest;
ZoneDest = zone;
- [DestTimeView setTimeZone: ZoneDest];
+ // [DestTimeView setTimeZone: ZoneDest];
+ TimeDest = [self convertTime: TimeDest fromZone: OldZone
+ toZone:ZoneDest];
+
+ NSLog(@"Dest time changed to %@ because of zone change.",
+ [Formatter stringFromDate: TimeDest]);
+
+ [self updateTimeDestView];
[self updateZoneDestView];
}
- (bool) setSrcZoneWithStr: (NSString*) zone
{
- NSLog(@"Trying to set src zone to %@", zone);
+ NSLog(@"Trying to figure out what zone is %@...", zone);
if(!zone)
{
- ZoneSrc = nil;
+ // ZoneSrc = nil;
return false;
}
@@ -145,7 +165,7 @@ - (bool) setSrcZoneWithStr: (NSString*) zone
Zone = [NSTimeZone timeZoneWithName: zone];
if(!Zone)
{
- ZoneSrc = nil;
+ // ZoneSrc = nil;
return false;
}
}
@@ -156,11 +176,11 @@ - (bool) setSrcZoneWithStr: (NSString*) zone
- (bool) setDestZoneWithStr: (NSString*) zone
{
- NSLog(@"Trying to set dest zone to %@", zone);
+ NSLog(@"Trying to figure out what zone is %@...", zone);
if(!zone)
{
- ZoneDest = nil;
+ // ZoneDest = nil;
return false;
}
NSTimeZone* Zone = [NSTimeZone timeZoneWithAbbreviation: zone];
@@ -169,7 +189,7 @@ - (bool) setDestZoneWithStr: (NSString*) zone
Zone = [NSTimeZone timeZoneWithName: zone];
if(!Zone)
{
- ZoneDest = nil;
+ // ZoneDest = nil;
return false;
}
}
@@ -190,12 +210,16 @@ - (void) setDestZoneToLocal
- (void) updateTimeSrcView
{
- [SrcTimeView setDateValue: Time];
+ NSLog(@"Syncing src view to %@...",
+ [Formatter stringFromDate: TimeSrc]);
+ [SrcTimeView setDateValue: TimeSrc];
}
- (void) updateTimeDestView
{
- [DestTimeView setDateValue: Time];
+ NSLog(@"Syncing dest view to %@...",
+ [Formatter stringFromDate: TimeDest]);
+ [DestTimeView setDateValue: TimeDest];
}
- (void) updateZoneSrcView
@@ -229,30 +253,58 @@ - (void) updateZoneDestView
- (void) updateViews
{
- NSDate* TimeSrc = [SrcTimeView dateValue];
- NSDate* TimeDest = [DestTimeView dateValue];
- if([TimeSrc isEqualToDate: TimeDest])
+ NSDate* TimeSrcNew = [SrcTimeView dateValue];
+ NSDate* TimeDestNew = [DestTimeView dateValue];
+
+ if([TimeSrcNew isEqualToDate: TimeSrc])
{
- Time = TimeSrc;
- NSLog(@"Time set according to both views.");
- return;
- }
-
- if([TimeSrc isEqualToDate: Time])
- {// TimeDest changed
- Time = TimeDest;
- [self updateTimeSrcView];
- NSLog(@"Time set according to dest time.");
- return;
+ if([TimeDestNew isEqualToDate: TimeDest])
+ return;
+ else
+ {
+ NSLog(@"Dest time changed to %@",
+ [Formatter stringFromDate: TimeDestNew]);
+ TimeDest = TimeDestNew;
+ TimeSrc = [self convertTime: TimeDest
+ fromZone: ZoneDest
+ toZone: ZoneSrc];
+ }
}
-
- if([TimeDest isEqualToDate: Time])
- { // TimeSrc changed
- Time = TimeSrc;
- [self updateTimeDestView];
- NSLog(@"Time set according to src time.");
- return;
+ else
+ {
+ if([TimeDestNew isEqualToDate: TimeDest])
+ {
+ NSLog(@"Src time changed to %@",
+ [Formatter stringFromDate: TimeSrcNew]);
+ TimeSrc = TimeSrcNew;
+ TimeDest = [self convertTime: TimeSrc
+ fromZone: ZoneSrc
+ toZone: ZoneDest];
+ }
}
+
+// if([TimeSrcNew isEqualToDate: TimeDestNew])
+// {
+// Time = TimeSrcNew;
+// NSLog(@"Time set according to both views.");
+// return;
+// }
+//
+// if([TimeSrcNew isEqualToDate: Time])
+// {// TimeDest changed
+// Time = TimeDestNew;
+// [self updateTimeSrcView];
+// NSLog(@"Time set according to dest time.");
+// return;
+// }
+//
+// if([TimeDestNew isEqualToDate: Time])
+// { // TimeSrc changed
+// Time = TimeSrcNew;
+// [self updateTimeDestView];
+// NSLog(@"Time set according to src time.");
+// return;
+// }
[self updateTimeSrcView];
[self updateTimeDestView];
@@ -260,15 +312,30 @@ - (void) updateViews
return;
}
-- (void) startUpdating
-{
- UpdateTimer =
- [NSTimer scheduledTimerWithTimeInterval: 0.2
- target: self
- selector: @selector(updateViews)
- userInfo: [NSDictionary dictionaryWithObject:[NSDate date] forKey:@"StartDate"]
- repeats: YES];
- NSLog(@"Timer fired.");
+- (NSDate*) convertTime: (NSDate*) time fromZone: (NSTimeZone*) zone_src
+ toZone: (NSTimeZone*) zone_dest
+{ // `time' is actually interpreted as local time.
+ NSDateFormatter* FormatterWoZone = [[NSDateFormatter alloc] init];
+ // We need to get the numbers displayed in date picker
+ [FormatterWoZone setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
+ [FormatterWoZone setTimeZone: LocalZone];
+
+ NSString* TimeStr = [FormatterWoZone stringFromDate: time];
+ NSLog(@"Converting %@ from %@ to %@...",
+ TimeStr, zone_src, zone_dest);
+
+ // Now we'll see what the user actually means
+ [FormatterWoZone setTimeZone: zone_src];
+ NSDate* ProperTime = [FormatterWoZone dateFromString: TimeStr];
+ NSLog(@"... i.e. local time %@...",
+ [Formatter stringFromDate: ProperTime]);
+
+ // What is the time string for this time in `zone_dest'?
+ [FormatterWoZone setTimeZone: zone_dest];
+ NSString* TimeStrDest = [FormatterWoZone stringFromDate: ProperTime];
+ // Acquire the corresponding NSDate (which points to the wrong time but with the same time string).
+ [FormatterWoZone setTimeZone: LocalZone];
+ return [FormatterWoZone dateFromString: TimeStrDest];
}
@end
View
5 TimeConv/en.lproj/MainMenu.xib
@@ -1334,7 +1334,7 @@
<string key="NSCellIdentifier">_NS:9</string>
<reference key="NSControlView" ref="430054726"/>
<double key="NSTimeInterval">0.0</double>
- <int key="NSDatePickerElements">252</int>
+ <int key="NSDatePickerElements">236</int>
<object class="NSColor" key="NSBackgroundColor" id="827730175">
<int key="NSColorSpace">6</int>
<string key="NSCatalogName">System</string>
@@ -1529,7 +1529,7 @@ AQABAAEAAQAB//+dkAEA//+PgAAE//+dkAEI//+dkAEMUERUAFBTVABQV1QAUFBUAAAAAAEAAAABA</b
<string key="NSCellIdentifier">_NS:9</string>
<reference key="NSControlView" ref="701298308"/>
<double key="NSTimeInterval">0.0</double>
- <int key="NSDatePickerElements">252</int>
+ <int key="NSDatePickerElements">236</int>
<reference key="NSBackgroundColor" ref="827730175"/>
<reference key="NSTextColor" ref="368305583"/>
</object>
@@ -1622,7 +1622,6 @@ AQABAAEAAQAB//+dkAEA//+PgAAE//+dkAEI//+dkAEMUERUAFBTVABQV1QAUFBUAAAAAAEAAAABA</b
<string key="NSFrame">{{444, 7}, {39, 38}}</string>
<reference key="NSSuperview" ref="439893737"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView"/>
<int key="NSViewLayerContentsRedrawPolicy">2</int>
<string key="NSReuseIdentifierKey">_NS:1529</string>
<bool key="NSEnabled">YES</bool>

No commit comments for this range

Something went wrong with that request. Please try again.