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

Updated NSDate+Datetools category to reuse formatter instances. #13

Merged
merged 1 commit into from Apr 25, 2014
Merged
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
46 changes: 19 additions & 27 deletions DateTools/NSDate+DateTools.m
Expand Up @@ -1406,10 +1406,7 @@ -(BOOL)isLaterThanOrEqualTo:(NSDate *)date{
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
[formatter setLocale:[NSLocale currentLocale]];
return [formatter stringFromDate:self];
return [self formattedDateWithStyle:style timeZone:[NSTimeZone systemTimeZone] locale:[NSLocale currentLocale]];
}

/**
Expand All @@ -1421,10 +1418,7 @@ -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style{
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
[formatter setTimeZone:timeZone];
return [formatter stringFromDate:self];
return [self formattedDateWithStyle:style timeZone:timeZone locale:[NSLocale autoupdatingCurrentLocale]];
}

/**
Expand All @@ -1436,10 +1430,7 @@ -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTime
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style locale:(NSLocale *)locale{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
[formatter setLocale:locale];
return [formatter stringFromDate:self];
return [self formattedDateWithStyle:style timeZone:[NSTimeZone systemTimeZone] locale:locale];
}

/**
Expand All @@ -1452,7 +1443,12 @@ -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style locale:(NSLocale
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
static NSDateFormatter *formatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [[NSDateFormatter alloc] init];
});

[formatter setDateStyle:style];
[formatter setTimeZone:timeZone];
[formatter setLocale:locale];
Expand All @@ -1468,10 +1464,7 @@ -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTime
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithFormat:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
[formatter setLocale:[NSLocale currentLocale]];
return [formatter stringFromDate:self];
return [self formattedDateWithFormat:format timeZone:[NSTimeZone systemTimeZone] locale:[NSLocale autoupdatingCurrentLocale]];
}

/**
Expand All @@ -1483,10 +1476,7 @@ -(NSString *)formattedDateWithFormat:(NSString *)format{
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
[formatter setTimeZone:timeZone];
return [formatter stringFromDate:self];
return [self formattedDateWithFormat:format timeZone:timeZone locale:[NSLocale autoupdatingCurrentLocale]];
}

/**
Expand All @@ -1498,10 +1488,7 @@ -(NSString *)formattedDateWithFormat:(NSString *)format timeZone:(NSTimeZone *)t
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithFormat:(NSString *)format locale:(NSLocale *)locale{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
[formatter setLocale:locale];
return [formatter stringFromDate:self];
return [self formattedDateWithFormat:format timeZone:[NSTimeZone systemTimeZone] locale:locale];
}

/**
Expand All @@ -1514,9 +1501,14 @@ -(NSString *)formattedDateWithFormat:(NSString *)format locale:(NSLocale *)local
* @return NSString representing the formatted date string
*/
-(NSString *)formattedDateWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
static NSDateFormatter *formatter = nil;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry guys for saying this a little late, but this change makes this method thread-unsafe. Because NSDateFormatter is not thread safe, but NSDate is thread safe. Calling this method from different threads at the same time might cause an issue, because from the both threads the same NSDateFormatter object will be accessed.

Personally I use https://github.com/mysterioustrousers/MTDates for date calculations and this problem is solved there using locks. You could take a look at the implementation there.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. This, I believe, also extends to NSCalendar, which is used pervasively in the library. Feel free to raise an issue. Pull requests will be welcomed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatthewYork I'm sorry, I don't use this library now. I've noticed the library probably in cocoapods feed and started watching :) Unfortunately don't have enough of time to contribute to libraries I don't use, yet. I'm sorry.

Maybe I'll give this library a try on a next project in future ;)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yas375 I completely understand. I will create an issue and mark thread safety as an enhancement.

Cheers.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatthewYork thank you!

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [[NSDateFormatter alloc] init];
});

[formatter setDateFormat:format];
[formatter setTimeZone:timeZone];
[formatter setTimeZone:timeZone];
[formatter setLocale:locale];
return [formatter stringFromDate:self];
}
Expand Down