Created
November 22, 2012 23:58
-
-
Save mkalmes/4133375 to your computer and use it in GitHub Desktop.
Fun with NSDate
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
| #import <Foundation/Foundation.h> | |
| // clang -g -fobjc-arc -Wall -framework Foundation -o dateTest dateTest.m | |
| @interface DateTest : NSObject | |
| @end | |
| @implementation DateTest | |
| int main (void) { | |
| { | |
| NSLog(@"Creating a NSDate object..."); | |
| NSDateComponents *components = [[NSDateComponents alloc] init]; | |
| [components setYear:2012]; | |
| [components setMonth:12]; | |
| [components setDay:24]; | |
| [components setHour:23]; | |
| [components setMinute:42]; | |
| [components setSecond:5]; | |
| NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
| NSDate *christmas = [gregorian dateFromComponents:components]; | |
| NSLog(@"Woohoo! Christmas time: %@", christmas); | |
| } | |
| { | |
| NSLog(@"Calendar fun..."); | |
| NSDateComponents *components = [[NSDateComponents alloc] init]; | |
| [components setYear:2012]; | |
| [components setMonth:12]; | |
| [components setDay:24]; | |
| [components setHour:23]; | |
| [components setMinute:42]; | |
| [components setSecond:5]; | |
| NSCalendar *currentCalendar = [NSCalendar currentCalendar]; | |
| NSCalendar *japaneseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSJapaneseCalendar]; | |
| NSCalendar *buddhistCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSBuddhistCalendar]; | |
| NSDate *currentChristmas = [currentCalendar dateFromComponents:components]; | |
| NSDate *japaneseChristmas = [japaneseCalendar dateFromComponents:components]; | |
| NSDate *buddhistChristmas = [buddhistCalendar dateFromComponents:components]; | |
| NSLog(@"Current Christmas:\t%@", currentChristmas); | |
| NSLog(@"Japanese Christmas:\t%@", japaneseChristmas); | |
| NSLog(@"Buddhist Christmas:\t%@", buddhistChristmas); | |
| } | |
| { | |
| NSLog(@"Time Zone fun..."); | |
| NSDateComponents *components = [[NSDateComponents alloc] init]; | |
| [components setYear:2012]; | |
| [components setMonth:12]; | |
| [components setDay:24]; | |
| [components setHour:23]; | |
| [components setMinute:42]; | |
| [components setSecond:5]; | |
| NSCalendar *usersCalendar = [[NSLocale currentLocale] objectForKey:NSLocaleCalendar]; | |
| NSDate *date = [usersCalendar dateFromComponents:components]; | |
| NSString *dateFormat = @"dd.MM.yyyy G 'at' HH:mm:ss Z"; | |
| NSDateFormatter *formatterPDT = [[NSDateFormatter alloc] init]; | |
| [formatterPDT setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"PDT"]]; | |
| [formatterPDT setDateFormat:dateFormat]; | |
| NSDateFormatter *formatterUTC = [[NSDateFormatter alloc] init]; | |
| [formatterUTC setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; | |
| [formatterUTC setDateFormat:dateFormat]; | |
| NSDateFormatter *formatterCET = [[NSDateFormatter alloc] init]; | |
| [formatterCET setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CET"]]; | |
| [formatterCET setDateFormat:dateFormat]; | |
| NSDateFormatter *formatterJST = [[NSDateFormatter alloc] init]; | |
| [formatterJST setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"JST"]]; | |
| [formatterJST setDateFormat:dateFormat]; | |
| NSArray *formatters = @[formatterPDT, formatterUTC, formatterCET, formatterJST]; | |
| for (NSDateFormatter *formatter in formatters) { | |
| NSLog(@"%@", [formatter stringFromDate:date]); | |
| } | |
| } | |
| } | |
| @end | |
| #ifdef OUTPUT | |
| > ./dateTest | |
| dateTest[836:707] Creating a NSDate object... | |
| dateTest[836:707] Woohoo! Christmas time: 2012-12-24 22:42:05 +0000 | |
| dateTest[836:707] Calendar fun... | |
| dateTest[836:707] Current Christmas: 2012-12-24 22:42:05 +0000 | |
| dateTest[836:707] Japanese Christmas: 4000-12-24 22:42:05 +0000 | |
| dateTest[836:707] Buddhist Christmas: 1469-12-24 22:48:37 +0000 | |
| dateTest[836:707] Time Zone fun... | |
| dateTest[836:707] 24.12.2012 AD at 14:42:05 -0800 | |
| dateTest[836:707] 24.12.2012 AD at 22:42:05 +0000 | |
| dateTest[836:707] 24.12.2012 AD at 23:42:05 +0100 | |
| dateTest[836:707] 25.12.2012 AD at 07:42:05 +0900 | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment