Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.