New Transformers for NSString to NSDate and NSDate to NSString #9

Closed
wants to merge 18 commits into
from

Conversation

Projects
None yet
2 participants
@kwylez

kwylez commented Apr 24, 2013

I added these new transforms to provide and easy and straightforward way to go between NSDate object and NSString representation of dates.

Features include:

  • setting standard date formatter. Right now it is hard coded in the NSFormatter category
  • cache/reuse of all custom formatters
  • thread safe

I also add .gitignore file

@mattt

This comment has been minimized.

Show comment
Hide comment
@mattt

mattt Apr 26, 2013

Owner

Thanks so much for your contribution, @kwylez. However, this strikes me as perhaps a rather complicated to about this...

NSDateFormatter encapsulates a lot of formatting behavior—far more than I'd want to or could duplicate in the interface of an NSValueTransformer. Exposing them as class variables is a good API decision on that count, but introduces shared state to the point of not being particularly useful (e.g. wanting more than one at one time).

Another thing is that most of the time, NSDateFormatter is used to convert NSDate to localized, human readable forms, using -setDateStyle: and -setTimeStyle:. That is to say, format strings are not really used.

My advice for anyone wanting a transformer interface for a given date formatter is to use NSValueTransformer+TransformerKit to declare a new transformer for that pre-configured formatter.

Your patch did remind me of one of the reasons I made this library to begin with: converting between ISO 8601 timestamps and NSDates. I've added these in c064303.

Owner

mattt commented Apr 26, 2013

Thanks so much for your contribution, @kwylez. However, this strikes me as perhaps a rather complicated to about this...

NSDateFormatter encapsulates a lot of formatting behavior—far more than I'd want to or could duplicate in the interface of an NSValueTransformer. Exposing them as class variables is a good API decision on that count, but introduces shared state to the point of not being particularly useful (e.g. wanting more than one at one time).

Another thing is that most of the time, NSDateFormatter is used to convert NSDate to localized, human readable forms, using -setDateStyle: and -setTimeStyle:. That is to say, format strings are not really used.

My advice for anyone wanting a transformer interface for a given date formatter is to use NSValueTransformer+TransformerKit to declare a new transformer for that pre-configured formatter.

Your patch did remind me of one of the reasons I made this library to begin with: converting between ISO 8601 timestamps and NSDates. I've added these in c064303.

@mattt mattt closed this Apr 26, 2013

@mattt

This comment has been minimized.

Show comment
Hide comment
@mattt

mattt Apr 26, 2013

Owner

A few other notes I hope you'll find helpful:

  • When developing a feature, you can create intermediary commits as you've done—in fact, this is probably a good idea. However, when it comes time to merge that in, you should squash these down into one or a few strongly-focused, functional commits.
  • I would recommend against using thread dictionaries to store static variables. Here's the best way to do that, for example:
static NSString * TTTISO8601TimestampFromDate(NSDate *date) {
    static NSDateFormatter *_iso8601DateFormatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _iso8601DateFormatter = [[NSDateFormatter alloc] initWithDateFormat:@"%Y-%m-%dT%H:%M:%S%z" allowNaturalLanguage:NO];
        [_iso8601DateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    });

    return [_iso8601DateFormatter stringFromDate:date];
}
Owner

mattt commented Apr 26, 2013

A few other notes I hope you'll find helpful:

  • When developing a feature, you can create intermediary commits as you've done—in fact, this is probably a good idea. However, when it comes time to merge that in, you should squash these down into one or a few strongly-focused, functional commits.
  • I would recommend against using thread dictionaries to store static variables. Here's the best way to do that, for example:
static NSString * TTTISO8601TimestampFromDate(NSDate *date) {
    static NSDateFormatter *_iso8601DateFormatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _iso8601DateFormatter = [[NSDateFormatter alloc] initWithDateFormat:@"%Y-%m-%dT%H:%M:%S%z" allowNaturalLanguage:NO];
        [_iso8601DateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    });

    return [_iso8601DateFormatter stringFromDate:date];
}
@kwylez

This comment has been minimized.

Show comment
Hide comment
@kwylez

kwylez Apr 29, 2013

@mattt Thank you very much for the feedback. You're points are spot on!

kwylez commented Apr 29, 2013

@mattt Thank you very much for the feedback. You're points are spot on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment