Coder Social home page Coder Social logo

mtdates's Introduction

MTDates

Build Status Version Platform

MTDates is a category on NSDate that makes common date calculations super easy.

Installation

In your Podfile, add this line:

pod "MTDates"

pod? => https://github.com/CocoaPods/CocoaPods/ NOTE: You may need to add -all_load to "Other Linker Flags" in your targets build settings if the pods library only contains categories.

Example Usage

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM/dd/yyyy hh:mma";
NSDate *date = [formatter dateFromString:@"07/11/1986 11:29am"];

[date mt_year];                                                     // => 1986

[date mt_weekdayOfWeek];                                            // => 6

[date mt_minuteOfHour];                                             // => 29

[[date mt_startOfPreviousYear] mt_year];                            // => 1985

[[date mt_startOfPreviousYear] mt_stringFromDateWithISODateTime];   // => @"1985-01-01T00:00:00Z"

[[date mt_startOfPreviousMonth] mt_stringFromDatesFullMonth];       // => @"June"

Creating Dates

[NSDate mt_dateFromYear:1986 week:28 weekday:6 hour:11 minute:29 second:0];

Dates From Other Dates

[date mt_dateByAddingYears:0 months:0 weeks:2 days:0 hours:1 minutes:24 seconds:0];

[date mt_dateYearsAfter:8];

[date mt_oneYearNext];

[date mt_dateMonthsBefore:4];

Comparing Dates

[date mt_isAfter:date2];

[date2 mt_isBefore:date];

[date3 mt_isOnOrBefore:date];

[date2 mt_isWithinSameWeek:date];

[date4 mt_isWithinSameDay:date];

Other Coolness

[date mt_weekdayStartOfCurrentMonth];
[date mt_daysInCurrentMonth];
[date mt_daysInPreviousMonth];
[date mt_daysInNextMonth];

International

[NSDate mt_setFirstDayOfWeek:1];
[NSDate mt_setFirstDayOfWeek:4];

[NSDate mt_setWeekNumberingSystem:MTDateWeekNumberingSystemISO];
[NSDate mt_setWeekNumberingSystem:MTDateWeekNumberingSystemUS];

All methods

+ (void)mt_setLocale:(NSLocale *)locale;
+ (void)mt_setTimeZone:(NSTimeZone *)timeZone;
+ (void)mt_setFirstDayOfWeek:(NSUInteger)firstDay; // Sunday: 1, Saturday: 7
+ (void)mt_setWeekNumberingSystem:(MTDateWeekNumberingSystem)system;

+ (NSDate *)mt_dateFromISOString:(NSString *)ISOString;
+ (NSDate *)mt_dateFromString:(NSString *)string usingFormat:(NSString *)format;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day hour:(NSUInteger)hour minute:(NSUInteger)minute;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year month:(NSUInteger)month day:(NSUInteger)day hour:(NSUInteger)hour minute:(NSUInteger)minute second:(NSUInteger)second;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year week:(NSUInteger)week weekday:(NSUInteger)weekday;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year week:(NSUInteger)week weekday:(NSUInteger)weekday hour:(NSUInteger)hour minute:(NSUInteger)minute;
+ (NSDate *)mt_dateFromYear:(NSUInteger)year week:(NSUInteger)week weekday:(NSUInteger)weekday hour:(NSUInteger)hour minute:(NSUInteger)minute second:(NSUInteger)second;
- (NSDate *)mt_dateByAddingYears:(NSInteger)years months:(NSInteger)months weeks:(NSInteger)weeks days:(NSInteger)days hours:(NSInteger)hours minutes:(NSInteger)minutes seconds:(NSInteger)seconds;
+ (NSDate *)mt_dateFromComponents:(NSDateComponents *)components;

+ (NSArray *)mt_shortWeekdaySymbols;
+ (NSArray *)mt_weekdaySymbols;
+ (NSArray *)mt_veryShortWeekdaySymbols;
+ (NSArray *)mt_shortMonthlySymbols;
+ (NSArray *)mt_monthlySymbols;
+ (NSArray *)mt_veryShortMonthlySymbols;

- (NSUInteger)mt_year;
- (NSUInteger)mt_weekOfYear;
- (NSUInteger)mt_weekdayOfWeek;
- (NSUInteger)mt_monthOfYear;
- (NSUInteger)mt_dayOfMonth;
- (NSUInteger)mt_hourOfDay;
- (NSUInteger)mt_minuteOfHour;
- (NSUInteger)mt_secondOfMinute;
- (NSTimeInterval)mt_secondsIntoDay;
- (NSDateComponents *)mt_components;

- (NSDate *)mt_startOfPreviousYear;
- (NSDate *)mt_startOfCurrentYear;
- (NSDate *)mt_startOfNextYear;
- (NSDate *)mt_endOfPreviousYear;
- (NSDate *)mt_endOfCurrentYear;
- (NSDate *)mt_endOfNextYear;
- (NSDate *)mt_oneYearPrevious;
- (NSDate *)mt_oneYearNext;
- (NSDate *)mt_dateYearsBefore:(NSUInteger)years;
- (NSDate *)mt_dateYearsAfter:(NSUInteger)years;
- (NSInteger)mt_yearsSinceDate:(NSDate *)date;
- (NSInteger)mt_yearsUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousMonth;
- (NSDate *)mt_startOfCurrentMonth;
- (NSDate *)mt_startOfNextMonth;
- (NSDate *)mt_endOfPreviousMonth;
- (NSDate *)mt_endOfCurrentMonth;
- (NSDate *)mt_endOfNextMonth;
- (NSDate *)mt_oneMonthPrevious;
- (NSDate *)mt_oneMonthNext;
- (NSDate *)mt_dateMonthsBefore:(NSUInteger)months;
- (NSDate *)mt_dateMonthsAfter:(NSUInteger)months;
- (NSInteger)mt_monthsSinceDate:(NSDate *)date;
- (NSInteger)mt_monthsUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousWeek;
- (NSDate *)mt_startOfCurrentWeek;
- (NSDate *)mt_startOfNextWeek;
- (NSDate *)mt_endOfPreviousWeek;
- (NSDate *)mt_endOfCurrentWeek;
- (NSDate *)mt_endOfNextWeek;
- (NSDate *)mt_oneWeekPrevious;
- (NSDate *)mt_oneWeekNext;
- (NSDate *)mt_dateWeeksBefore:(NSUInteger)weeks;
- (NSDate *)mt_dateWeeksAfter:(NSUInteger)weeks;
- (NSInteger)mt_weeksSinceDate:(NSDate *)date;
- (NSInteger)mt_weeksUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousDay;
- (NSDate *)mt_startOfCurrentDay;
- (NSDate *)mt_startOfNextDay;
- (NSDate *)mt_endOfPreviousDay;
- (NSDate *)mt_endOfCurrentDay;
- (NSDate *)mt_endOfNextDay;
- (NSDate *)mt_oneDayPrevious;
- (NSDate *)mt_oneDayNext;
- (NSDate *)mt_dateDaysBefore:(NSUInteger)days;
- (NSDate *)mt_dateDaysAfter:(NSUInteger)days;
- (NSInteger)mt_daysSinceDate:(NSDate *)date;
- (NSInteger)mt_daysUntilDate:(NSDate *)date;

- (NSDate *)mt_startOfPreviousHour;
- (NSDate *)mt_startOfCurrentHour;
- (NSDate *)mt_startOfNextHour;
- (NSDate *)mt_endOfPreviousHour;
- (NSDate *)mt_endOfCurrentHour;
- (NSDate *)mt_endOfNextHour;
- (NSDate *)mt_oneHourPrevious;
- (NSDate *)mt_oneHourNext;
- (NSDate *)mt_dateHoursBefore:(NSUInteger)hours;
- (NSDate *)mt_dateHoursAfter:(NSUInteger)hours;
- (NSInteger)mt_hoursSinceDate:(NSDate *)date;
- (NSInteger)mt_hoursUntilDate:(NSDate *)date;

- (BOOL)mt_isAfter:(NSDate *)date;
- (BOOL)mt_isBefore:(NSDate *)date;
- (BOOL)mt_isOnOrAfter:(NSDate *)date;
- (BOOL)mt_isOnOrBefore:(NSDate *)date;
- (BOOL)mt_isWithinSameYear:(NSDate *)date;
- (BOOL)mt_isWithinSameMonth:(NSDate *)date;
- (BOOL)mt_isWithinSameWeek:(NSDate *)date;
- (BOOL)mt_isWithinSameDay:(NSDate *)date;
- (BOOL)mt_isWithinSameHour:(NSDate *)date;
- (BOOL)mt_isBetweenDate:(NSDate *)date1 andDate:(NSDate *)date2;

- (NSString *)mt_stringFromDateWithHourAndMinuteFormat:(MTDateHourFormat)format;
- (NSString *)mt_stringFromDateWithShortMonth;
- (NSString *)mt_stringFromDateWithFullMonth;
- (NSString *)mt_stringFromDateWithAMPMSymbol;
- (NSString *)mt_stringFromDateWithShortWeekdayTitle;
- (NSString *)mt_stringFromDateWithFullWeekdayTitle;
- (NSString *)mt_stringFromDateWithFormat:(NSString *)format localized:(BOOL)localized // http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
- (NSString *)mt_stringFromDateWithISODateTime;
- (NSString *)mt_stringFromDateWithGreatestComponentsForSecondsPassed:(NSTimeInterval)interval;

+ (NSArray *)mt_datesCollectionFromDate:(NSDate *)startDate untilDate:(NSDate *)endDate;
- (NSArray *)mt_hoursInCurrentDayAsDatesCollection;
- (BOOL)mt_isInAM;
- (BOOL)mt_isStartOfAnHour;
- (NSUInteger)mt_weekdayStartOfCurrentMonth;
- (NSUInteger)mt_daysInCurrentMonth;
- (NSUInteger)mt_daysInPreviousMonth;
- (NSUInteger)mt_daysInNextMonth;
- (NSDate *)mt_inTimeZone:(NSTimeZone *)timezone;

NSDateComponents Additions

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10 October 2009"];
[comps mt_stringValue]   // => @"10 October 2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"October 2009"];
[comps mt_stringValue]   // => @"October 2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"2009"];
[comps mt_stringValue]   // => @"2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10 2009"];
[comps mt_stringValue]   // => @"October 2009"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10 July"];
[comps mt_stringValue]   // => @"10 July"

NSDateComponents *comps = [NSDateComponents componentsFromString:@"10"];
[comps mt_stringValue]   // => @"October"

Contributors

mtdates's People

Contributors

atomkirk avatar dsparling avatar dzindra avatar foscomputerservices avatar haemi avatar jeddison avatar jeffaburt avatar jtrobman avatar m4c1ek avatar modocache avatar tylerc230 avatar wiruzx avatar xhacker avatar yas375 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mtdates's Issues

Time zones are sticky

I've got some code which uses the mt_startOfToday category to figure out when the start of the current day is. I was testing behaviour when changing time-zones and noticed that the time zone which was in effect when the library was first called is sticky.

Digging in, it looks like the local time zone is grabbed in mt_prepareDefaults and then copied into the __calendar and __formatter ... which persist in their statics for the lifetime of the app.

I guess it needs a hook into the NSSystemTimeZoneDidChangeNotification to null the statics out... But where? Your thoughts?

How to parse 2013-09-27T14:12:14+01:00

We are dealing with dates in the format 2013-09-27T14:12:14+01:00

I've tried to parse it using

[NSDate mt_dateFromString:postedAt usingFormat: @"yyyy-MM-ddTHH:mm:ss+z"]

...but no cigar.

Any help appreciated :)

iOS 6 compatibility

HI

You have removed the iOS 6 compatibility. Is it possible to make it working again on older os?

UPDATE: I have read the closed discussion. :-)

What is the deployment target for iOS?

The deployment target in the project for iOS is set to 5.1, but I recently saw that one merged pull request fixed some warning for deprecated APIs in iOS 7.
If the deployment target is actually moved to iOS 7 I would like to submit another pull request to remove all the other deprecation warnings.

on iOS8 with RegionFormat set on UK and Date&Time 24-Hour Time disabled, mt_dateFromISOString returns null

On iOS8 (did not test other version, but it s probably also the case), if RegionFormat is set to UK and Date&Time 24-Hour disabled, mt_dateFromISOString returns null:

NSString *dateString = @"2017-12-31 23:00:00 GMT";
NSDate *premiumUntil = [NSDate mt_dateFromISOString:dateString];
NSLog(@"Date: %@", premiumUntil); // Date: (null)

By setting the locale in (NSDate *)mt_dateFromISOString:(NSString *)ISOString:

[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];

it's working fine:

NSString *dateString = @"2017-12-31 23:00:00 GMT";
NSDate *premiumUntil = [NSDate mt_dateFromISOString:dateString];
NSLog(@"Date: %@", premiumUntil); // Date 2017-12-31 11:00:00 pm +0000

Note: as there is no Date&Time, this cannot be tested in the simulator, only on device

Make TravisCI to run tests on iOS as well

Running tests on iOS7 and iOS8 on TravisCI will ensure that we don't use an API added only in iOS8. For example like usage of NSCalendarIdentifierGregorian.

If @atomkirk doesn't have objections against it, I would be glad to do this myself. But probably a little later when I have more time. So if anyone wants to join this - you are welcome ;)

Using MTDates with GCD throws NSInvalidArgumentException

Executing following code:

- (void)test_MTDatesBlockInvoke {
    dispatch_queue_t queue = dispatch_queue_create("mtdates-test", NULL);
    dispatch_async(queue, ^{
        NSDate * date = [NSDate dateFromYear:2013 month:1 day:1];

    });
    dispatch_release(queue);
}

This snippet will always throw NSInvalidArgumentException with message setObjectForKey: key cannot be nil.

This is caused by [NSOperationQueue currentQueue] returning nil and then trying to use this return value as a key for thread local dictionary.

I am not quite sure how to solve this, maybe with using some kind of default calendar or extending current thread detection.

Preprocessor option to enable/disable prefix

I like the pfx_ convention on categories, and i'm all for it. However it's nice to also have a preprocessor option which can enable/disable the prefix. This would please everyone, and also make it easier for people upgrading from older version.
MagicalRecord does this with MR_SHORTHAND = 1 or similar.

Pod install error

When i run "Pod install" I got this error : "The dependency MTDates is not used in any concrete target"

Tests may fail if iOS simulator isn't using en_US locale

Changing the locale of the simulator from en_US isn't provided for within XCode, but is possible using tools like ios-sim-locale and can be very useful for developing and testing projects.

Running the tests in the simulator with the locale set even to 'en_GB' causes 12 failures in the existing tests using date string formats.

As the locale can't be set programatically I'm not sure on the solution - or importance - of this.

Weekday calculation doing something wrong?

Hi,
I have a problem when doing calculation with weeks and weekdays when you you library and I am not able to track down why.
I did a test:

NSDate *now = [NSDate date];
expectedDate = [NSDate mt_dateFromYear:[now mt_year]
                                  week:[now mt_weekOfYear]
                               weekday:[now mt_weekdayOfWeek]
                                  hour:[now mt_hourOfDay]
                                minute:[now mt_minuteOfHour]];

From what I feel expectedDate should be the same as today but it is yesterday.
Maybe I have to add that I am in Europe and when I debug I can see the error:
[now mt_weekdayOfWeek] returns 3 for wednesday as if 1 is monday but 3 is tuesday when sunday is 1.
So the function returns one day less and recreating it with the value seems to use sunday as 1 so I get tuesday in the result, not wednesday.
I dunno how to fix this or why it is this way or what I should have to set to get the correct result.

Regards,
Alexander

mt_dateFromYear problem when using 13 hours.

I'm trying to create a date as this:

[_picker setDate:[NSDate mt_dateFromYear:2014 month:8 day:6 hour:13 minute:45]];

and i'm getting this date:

2014-08-07 11:45:00 +0000

I'm doing something wrong?

How about 0.9.3 release?

Looks like there a lot of changes made in MTDates since 0.9.2 release. I think would be good to release 0.9.3 version so Cocoapods users can upgrade with ease ;)
Thanks in advance!

Tests crash at different places

I've just cloned the repo to my machine. When I'm running tests I see crashes in different places. See the screencast. Am I the only one who has such problems?

Xcode 5.0.2

Builds on Travis CI are failing since arm64 support...

2013-12-30_1707

@atomkirk does it work locally for you?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.