Giter Club home page Giter Club logo

nsdate-extensions's Introduction

README

Please see SwiftDates for Swift date utilities, which have been greatly expanded and incorporate updated APIs.

nsdate-extensions's People

Contributors

erica 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  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

nsdate-extensions's Issues

dateByAddingDays is not safe for DST switch.

dateByAddingDays is not safe for Daylight Saving Time switch.
I have an issue related this dateByAddingDays on Nov 3rd, and just realized this date is DST End Date.

What do you think if we using NSDateComponent for add days calculation?

Original :

- (NSDate *) dateByAddingDays: (NSInteger) dDays
{
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_DAY * dDays;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}

Suggestion :

- (NSDate *) dateByAddingDays: (NSInteger) dDays
{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:dDays];
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:self options:0];
    return newDate;
}

If you can give any suggestions that would be awesome.

isLastWeek doesn't seem to be working

At the time of writing, the date is 18th of July 2014, I am checking to see if the 17th of July is last week. It isn't last week, but isLastWeek is returning YES. I believe it might just be a missing ! before [self isSameWeekAsDate:newDate]; in isLastWeek.

Subtracting 1 Minute leads to Date in the far future...

(lldb) po [NSDate date]
(id) $2 = 0x1513c0c0 2012-08-23 22:00:00 +0000

(lldb) po [[NSDate date] dateBySubtractingMinutes:1]
(id) $1 = 0x0a19b6f0 2148-09-30 04:27:16 +0000

This is the method

- (NSDate *) dateByAddingMinutes: (NSUInteger) dMinutes
{
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;         
}

- (NSDate *) dateBySubtractingMinutes: (NSUInteger) dMinutes
{
    return [self dateByAddingMinutes: (dMinutes * -1)];
}

-isThisYear is broken

(line 130)

  • (BOOL) isThisYear
    {
    return [self isSameWeekAsDate:[NSDate date]];
    }

Somehow, that manages to work in some weird cases

Anyhow I think I fixed it, the right code should be

  • (BOOL) isThisYear
    {
    return [self isSameYearAsDate:[NSDate date]];
    }

isThisYear has the same body as isThisWeek

In the current implementation:

- (BOOL) isThisYear
{
        return [self isSameWeekAsDate:[NSDate date]];
}

This is wrong of course. Should be:

- (BOOL) isThisYear
{
    return [self isSameYearAsDate:[NSDate date]];
}

Don't do it like that!

It's really bad to add/subtract dates like you are you you don't account for any of the issues around dates. You should be using the calendrical operations instead of just adding 86400 seconds per day. Watch the WWDC video on this and he clearly explains why what you're doing is unsafe.

Update Podspec

Hi Erica,

First of all, this category is awesome. Could you post a new podspec that points at the most recent commits?

dateAtEndOfDay is implemented incorrectly

In the method - (NSDate *) dateAtEndOfDay it is assumed that there are 11 hours in a day which is wrong. Method implementation should be something like:

- (NSDate *) dateAtEndOfDay
{
  NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
  components.hour = 23;
  components.minute = 59;
  components.second = 59;
  return [[NSDate currentCalendar] dateFromComponents:components];
}

Timezone support

I've just found that all date calculations happens with timezone specified in current calendar e.g. [NSCalendar currentCalendar]. But when you are working with time in different timezones, it become more or less useless.

It would be great if you could set the timezone for calendar. Something like that:
[CURRENT_CALENDAR setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

Thanks!

SPM support

Can you add a Package.swift and tag that commit with a version number so this can be easily used with SPM?

Need the pod update

The pod 'NSDate-Extensions' version is not the latest one, could you please update it?

isSameWeekAsDate

I think if this method doesn't work properly or his name is wrong.
As a consequence, methods like isNextWeek too.

isSameMonthsAsDate and isSameYearAsDate compare months with years, but method isSameWeekAsDate dimensions interval between two days.

Maybe my solution does that we need. it compares if date has same week like other date.

- (BOOL) isSameWeekAsDate: (NSDate *) aDate
{
    NSDate *startWeek = [self dateAtStartOfWeek];

    NSInteger timeInterval = [aDate timeIntervalSinceDate:startWeek];
    return (timeInterval > 0 && timeInterval < D_WEEK);
}

- (NSDate *) dateAtStartOfWeek
{
    // Get the weekday component of the current date
    NSDateComponents *weekdayComponents = [[NSDate currentCalendar] components:NSWeekdayCalendarUnit
                                                                      fromDate:self];
    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
    // required having 7 days in one week
    [componentsToSubtract setDay: 0 - ((([weekdayComponents weekday] - [[NSDate currentCalendar] firstWeekday]) + 7 ) % 7)];

    NSDate *beginningOfWeek = [[NSDate currentCalendar] dateByAddingComponents:componentsToSubtract
                                                                        toDate:self options:0];
// optional
    NSDateComponents *components = [[NSDate currentCalendar] components: componentFlags
                                                               fromDate: beginningOfWeek];
    beginningOfWeek = [[NSDate currentCalendar] dateFromComponents: components];

    return [beginningOfWeek dateAtStartOfDay];
}

"week" Propertie Version Support

SDK IOS7.1 Above Versions

  • (NSInteger) week
    {
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    return components.week;
    }

Should be replaced

  • (NSInteger) week
    {
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
    return components.weekOfYear;
    }

Precautions:
componentFlags add "NSWeekOfYearCalendarUnit"

IOS7 week NS_CALENDAR_DEPRECATED

  • (NSInteger)week NS_CALENDAR_DEPRECATED(10_4, 10_9, 2_0, 7_0, "Use weekOfMonth or weekOfYear, depending on which you mean");

ios7 above can not be used

  • (NSInteger)week
    {
    NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];

    return components.week;
    }

dateBySubtractingDays returns incorrect results

dateByAddingDays takes a NSUInteger but gets called with a negative number from dateBySubtractingDays: this causes incorrect result. Haven't had time to look around but this bug may not be limited to just dateByAddingDays.

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.