Skip to content

Instantly share code, notes, and snippets.

@Legoless
Created September 11, 2014 09:07
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 Legoless/0b694b5a45a12c8a1971 to your computer and use it in GitHub Desktop.
Save Legoless/0b694b5a45a12c8a1971 to your computer and use it in GitHub Desktop.
Weak or strong property?
@interface DateObject : NSObject
@property (nonatomic, strong) NSDate *date_UTC;
// Wrapper property, strong or weak?
@property (nonatomic) NSDate *date;
@end
@implementation DateObject
- (void)setDate:(NSDate *)date
{
//
// The date is transformed to UTC retaining only the day, month, year
//
// - Get components from local calendar
// - Insert components into the UTC calendar
// - Get date
//
NSDateComponents *components = [[NSCalendar autoupdatingCurrentCalendar] component:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *resampledDate = [gregorian dateFromComponents:components]
self.date_UTC = resampledDate;
}
- (NSDate *)date
{
//
// The date is transformed from UTC retaining only the day, month, year
//
// - Get components from UTC calendar
// - Insert components into the local calendar
// - Get date
//
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:self.date_UTC];
return [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:components];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment