Created
May 7, 2012 21:27
-
-
Save atomicbird/2630584 to your computer and use it in GitHub Desktop.
Half past midnight
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> | |
// Get an NSDate representing 30 minutes past midnight in an arbitrary time zone, and print it in a readable form. | |
// Tom Harrington, [email protected] | |
int main(int argc, char *argv[]) { | |
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init]; | |
NSString *timeZoneName = @"America/New_York"; | |
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:timeZoneName]; | |
// For local time zone, use | |
//NSTimeZone *timeZone = [NSTimeZone localTimeZone]; | |
NSLog(@"Zone name: %@", timeZoneName); | |
NSLog(@"Zone abberviation: %@", [timeZone abbreviation]); | |
NSCalendar *calendar = [NSCalendar currentCalendar]; | |
[calendar setTimeZone:timeZone]; | |
NSDate *now = [NSDate date]; | |
NSUInteger componentFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit; | |
NSDateComponents *nowDateComponents = [calendar components:componentFlags fromDate:now]; | |
[nowDateComponents setHour:0]; | |
[nowDateComponents setMinute:30]; | |
[nowDateComponents setSecond:0]; | |
[nowDateComponents setDay:[nowDateComponents day]+1]; | |
NSDate *midnight = [calendar dateFromComponents:nowDateComponents]; | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
[formatter setTimeZone:timeZone]; | |
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss VVVV"]; | |
NSString *midnightString = [formatter stringFromDate:midnight]; | |
NSLog(@"Midnight in %@: %@", timeZoneName, midnightString); | |
[p release]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment