Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass custom calendar #389

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions JTCalendar/JTCalendarManager.h
Expand Up @@ -34,6 +34,7 @@
@property (nonatomic, readonly) JTCalendarScrollManager * _Nullable scrollManager;

- (instancetype _Nullable )initWithLocale:(NSLocale *_Nullable)locale andTimeZone:(NSTimeZone *_Nullable)timeZone;
- (instancetype _Nullable )initWithCalendar:(NSCalendar *_Nullable)calendar;

- (NSDate *_Nonnull)date;
- (void)setDate:(NSDate *_Nullable)date;
Expand Down
27 changes: 24 additions & 3 deletions JTCalendar/JTCalendarManager.m
Expand Up @@ -22,14 +22,35 @@ - (instancetype)initWithLocale:(NSLocale *)locale andTimeZone:(NSTimeZone *)time
if(!self){
return nil;
}
[self commonInit:locale andTimeZone:timeZone];

#ifdef __IPHONE_8_0
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
#else
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
#endif

calendar.timeZone = timeZone;
calendar.locale = locale;

[self commonInit:calendar];

return self;
}

- (instancetype _Nullable )initWithCalendar:(NSCalendar *_Nullable)calendar
{
self = [super init];
if(!self){
return nil;
}
[self commonInit:calendar];

return self;
}

- (void)commonInit:(NSLocale *)locale andTimeZone:(NSTimeZone *)timeZone
- (void)commonInit:(NSCalendar *)calendar
{
_dateHelper = [[JTDateHelper alloc] initWithLocale:locale andTimeZone:timeZone];
_dateHelper = [[JTDateHelper alloc] initWithCalendar:calendar];
_settings = [JTCalendarSettings new];

_delegateManager = [JTCalendarDelegateManager new];
Expand Down
2 changes: 1 addition & 1 deletion JTCalendar/JTDateHelper.h
Expand Up @@ -9,7 +9,7 @@

@interface JTDateHelper : NSObject

- initWithLocale:(NSLocale *)locale andTimeZone:(NSTimeZone *)timeZone;
- initWithCalendar:(NSCalendar *)calendar;

- (NSCalendar *)calendar;
- (NSDateFormatter *)createDateFormatter;
Expand Down
26 changes: 6 additions & 20 deletions JTCalendar/JTDateHelper.m
Expand Up @@ -9,39 +9,21 @@

@interface JTDateHelper (){
NSCalendar *_calendar;
NSLocale *_locale;
NSTimeZone *_timeZone;
}

@end

@implementation JTDateHelper

- (instancetype)initWithLocale:(NSLocale *)locale andTimeZone:(NSTimeZone *)timeZone
- (instancetype)initWithCalendar:(NSCalendar *)calendar
{
self = [super init];
if (self) {
_locale = locale;
_timeZone = timeZone;
_calendar = calendar;
}
return self;
}

- (NSCalendar *)calendar
{
if(!_calendar){
#ifdef __IPHONE_8_0
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
#else
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
#endif
_calendar.timeZone = _timeZone;
_calendar.locale = _locale;
}

return _calendar;
}

- (NSDateFormatter *)createDateFormatter
{
NSDateFormatter *dateFormatter = [NSDateFormatter new];
Expand All @@ -52,6 +34,10 @@ - (NSDateFormatter *)createDateFormatter
return dateFormatter;
}

- (NSCalendar *)calendar {
return _calendar;
}

#pragma mark - Operations

- (NSDate *)addToDate:(NSDate *)date months:(NSInteger)months
Expand Down