Please see SwiftDates for Swift date utilities, which have been greatly expanded and incorporate updated APIs.
erica / nsdate-extensions Goto Github PK
View Code? Open in Web Editor NEWPractical real-world dates
Practical real-world dates
Please see SwiftDates for Swift date utilities, which have been greatly expanded and incorporate updated APIs.
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.
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
.
In TimeFormatters line 96
NSString *LocStr(NSString *const key) {
I'm getting the below warning
Warning: No previous prototype for function 'LocStr'
(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)];
}
(line 130)
Somehow, that manages to work in some weird cases
Anyhow I think I fixed it, the right code should be
Hi Erica,
/*
Erica Sadun, http://ericasadun.com
iPhone Developer's Cookbook 3.x and beyond
BSD License, Use at your own risk
*/
Which BSD license (e.g. 4-clause, 3-clause, or 2-clause)?
thanks.
When using these extensions I found that adding the date returned was incorrect when the value returned was within a DST time change.
This can be fixed by using date components for all the add/subtract methods.
Here's a blog post I found that describes this issue.
It's pretty easy, but would be a handy addition
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]];
}
TimeFormatters.m, line 173
Warning: "Method possibly missing a [super dealloc] call"
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.
Hi Erica,
First of all, this category is awesome. Could you post a new podspec that points at the most recent commits?
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];
}
Using an NSUInteger for the
does not permit proper calculations when subtracting days, which is called by this method:
"source": {
"git": "https://github.com/erica/NSDate-Extensions.git",
"commit": "7c6730b8e5e4740a332d839f3597bcce11d83160"
},
Could you please update one version for CocoaPods
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!
Can you add a Package.swift and tag that commit with a version number so this can be easily used with SPM?
The pod 'NSDate-Extensions' version is not the latest one, could you please update it?
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];
}
Would be nice. Thanks
SDK IOS7.1 Above Versions
Should be replaced
Precautions:
componentFlags add "NSWeekOfYearCalendarUnit"
ios7 above can not be used
(NSInteger)week
{
NSDateComponents *components = [[NSDate currentCalendar] components:componentFlags fromDate:self];
return components.week;
}
They should both have return [self isSameWeekAsDate:newDate]; instead of return [self isSameYearAsDate:newDate];
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.
Hi Erica,
I'm sure you've heard about the CocoaPods project, maybe you're using it yourself already?! It would be really nice, if you could start creating git tags on your great NSDate-Exentions repository. The reason behind this request is the section "Unversioned libraries":
http://docs.cocoapods.org/guides/contributing_to_the_master_repo.html
Thanks,
plu
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.