Created
December 11, 2012 01:16
-
-
Save joshdholtz/4254921 to your computer and use it in GitHub Desktop.
Dates bow down to me
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// Stupid | |
// | |
// Created by Josh Holtz on 12/10/12. | |
// Copyright (c) 2012 RokkinCat. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
NSString *dateStr = @"2013-02-06T08:00:00+00:00"; | |
NSDate *date = [self doShit:dateStr]; | |
NSLog(@"Stupid date - %@", date); | |
NSLog(@"Stupid date in local - %@", [self dateToLocalTimezone:date]); | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (NSDate*)doShit:(NSString*)str { | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; | |
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; | |
NSDate *date = nil; | |
NSError *error = nil; | |
if (![dateFormatter getObjectValue:&date forString:str range:nil error:&error]) { | |
NSLog(@"Date '%@' could not be parsed: %@", self, error); | |
} | |
return date; | |
} | |
- (NSDate*)dateToLocalTimezone:(NSDate*)date { | |
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; | |
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; | |
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date]; | |
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date]; | |
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; | |
return [[NSDate alloc] initWithTimeInterval:interval sinceDate:date]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2012-12-10 19:15:49.010 Stupid[13503:c07] Stupid date - 2013-02-06 08:00:00 +0000 | |
2012-12-10 19:15:49.012 Stupid[13503:c07] Stupid date in local - 2013-02-06 02:00:00 +0000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment