Skip to content

Instantly share code, notes, and snippets.

@atomicbird
Created May 7, 2012 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomicbird/2630584 to your computer and use it in GitHub Desktop.
Save atomicbird/2630584 to your computer and use it in GitHub Desktop.
Half past midnight
#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, tph@atomicbird.com
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