Coder Social home page Coder Social logo

timerange's Introduction

TimeRange

CI Status Version License Platform Dependency Status

TimeRange provide check if an NSDate occurs between two other NSDates.

  • Check if an NSDate is contained TimeRange.( e.g. 12:00 is contained 10:00 ~ 20:00 )
  • Check if an TimeRange overlaps the other TimeRange.

Installation

TimeRange is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "TimeRange"

Usage

You can try to:

pod try "TimeRange"

img

API

@interface TimeRange : NSObject
// CAUTION : You should not access directory these property.
@property(nonatomic, readonly) NSDate *aDate;
@property(nonatomic, readonly) NSDate *bDate;

+ (instancetype)rangeWithStartDate:(NSDate *) startDate endDate:(NSDate *) endDate;

- (BOOL)contains:(NSDate *) targetData;

- (BOOL)overlaps:(TimeRange *) anotherTimeRange;

- (NSString *)description;
@end

contains

Check if an NSDate is contained TimeRange

/*
    20:00 ~ 04:00
    
    target date is 01:00
    
    It's contained the time range.
*/
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
[formatter setDateFormat:@"HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:@"20:00"];
NSDate *bDate = [formatter dateFromString:@"04:00"];
TimeRange *timeRange = [TimeRange rangeWithStartDate:aDate bDate:bDate];
NSDate *targetDate = [formatter dateFromString:@"01:00"];
[timeRange contains:targetDate];// => YES

overlaps

Compare TimeRanges.

/*
    04:00 ~ 10:00
    17:00 ~ 23:00
    
    are doesn't overlap.
*/

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
[formatter setDateFormat:@"HH:mm:ss"];
NSArray *aRangeData = @[@"04:00:00", @"10:00:00"];
NSArray *bRangeData = @[@"17:00:00", @"23:00:00"];
TimeRange *aRange = [TimeRange rangeWithStartDate:[formatter dateFromString:aRangeData[0]] bDate:[formatter dateFromString:aRangeData[1]]];
TimeRange *bRange = [TimeRange rangeWithStartDate:[formatter dateFromString:bRangeData[0]] bDate:[formatter dateFromString:bRangeData[1]]];
[aRange overlaps:bRange];// => NO

overlap

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

Acknowledgment

timerange's People

Contributors

azu avatar reiz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.