- Add support for parsing date ISO8601
This commit is contained in:
parent
13801d0aa3
commit
3bac17f9df
@ -33,7 +33,7 @@ PODS:
|
||||
- FBSDKShareKit (4.18.0):
|
||||
- FBSDKCoreKit
|
||||
- NSDataAES (0.2.2)
|
||||
- NSDate_Utils (1.0.1)
|
||||
- NSDate_Utils (1.0.3)
|
||||
- NSString-Helper (1.0.5)
|
||||
- nv-ios-http-status (0.0.1)
|
||||
- PEAR-FileManager-iOS (1.3.1)
|
||||
@ -80,7 +80,7 @@ SPEC CHECKSUMS:
|
||||
FBSDKLoginKit: 6773073e970b2b15fb12e451ce7f11da0532b880
|
||||
FBSDKShareKit: 0b8d6cc3f103c75297eb3c62caec284a2ccf1b9e
|
||||
NSDataAES: 967ea3337476a80e9838a533c25d570a06855ed0
|
||||
NSDate_Utils: b451375dcbf9099a444c32de029c4498a5e7d58e
|
||||
NSDate_Utils: 2fb4071323341507c610821bc99b98ef8c46d4ac
|
||||
NSString-Helper: 459e1b6a62b3bf7db10f01b0d102548608e945c4
|
||||
nv-ios-http-status: b6c2b5fc8656cc19e0d3000dadce2080b99d0e2f
|
||||
PEAR-FileManager-iOS: 3bc403f68a53483f5629aa822f4649e40275c4d3
|
||||
|
||||
4
Example/Pods/Manifest.lock
generated
4
Example/Pods/Manifest.lock
generated
@ -33,7 +33,7 @@ PODS:
|
||||
- FBSDKShareKit (4.18.0):
|
||||
- FBSDKCoreKit
|
||||
- NSDataAES (0.2.2)
|
||||
- NSDate_Utils (1.0.1)
|
||||
- NSDate_Utils (1.0.3)
|
||||
- NSString-Helper (1.0.5)
|
||||
- nv-ios-http-status (0.0.1)
|
||||
- PEAR-FileManager-iOS (1.3.1)
|
||||
@ -80,7 +80,7 @@ SPEC CHECKSUMS:
|
||||
FBSDKLoginKit: 6773073e970b2b15fb12e451ce7f11da0532b880
|
||||
FBSDKShareKit: 0b8d6cc3f103c75297eb3c62caec284a2ccf1b9e
|
||||
NSDataAES: 967ea3337476a80e9838a533c25d570a06855ed0
|
||||
NSDate_Utils: b451375dcbf9099a444c32de029c4498a5e7d58e
|
||||
NSDate_Utils: 2fb4071323341507c610821bc99b98ef8c46d4ac
|
||||
NSString-Helper: 459e1b6a62b3bf7db10f01b0d102548608e945c4
|
||||
nv-ios-http-status: b6c2b5fc8656cc19e0d3000dadce2080b99d0e2f
|
||||
PEAR-FileManager-iOS: 3bc403f68a53483f5629aa822f4649e40275c4d3
|
||||
|
||||
@ -13,76 +13,108 @@
|
||||
#define kMoonRiseMinutes 17*60
|
||||
#define kMoonSetMinutes 7*60
|
||||
|
||||
static NSString *kNSDateHelperFormatFullDateWithTime = @"MMM d, yyyy h:mm a";
|
||||
static NSString *kNSDateHelperFormatFullDate = @"MMM d, yyyy";
|
||||
static NSString *kNSDateHelperFormatShortDateWithTime = @"MMM d h:mm a";
|
||||
static NSString *kNSDateHelperFormatShortDate = @"MMM d";
|
||||
static NSString *kNSDateHelperFormatWeekday = @"EEEE";
|
||||
static NSString *kNSDateHelperFormatWeekdayWithTime = @"EEEE h:mm a";
|
||||
static NSString *kNSDateHelperFormatTime = @"h:mm a";
|
||||
static NSString *kNSDateHelperFormatTimeWithPrefix = @"'at' h:mm a";
|
||||
static NSString * _Nonnull kNSDateHelperFormatFullDateWithTime = @"MMM d, yyyy h:mm a";
|
||||
static NSString * _Nonnull kNSDateHelperFormatFullDate = @"MMM d, yyyy";
|
||||
static NSString * _Nonnull kNSDateHelperFormatShortDateWithTime = @"MMM d h:mm a";
|
||||
static NSString * _Nonnull kNSDateHelperFormatShortDate = @"MMM d";
|
||||
static NSString * _Nonnull kNSDateHelperFormatWeekday = @"EEEE";
|
||||
static NSString * _Nonnull kNSDateHelperFormatWeekdayWithTime = @"EEEE h:mm a";
|
||||
static NSString * _Nonnull kNSDateHelperFormatTime = @"h:mm a";
|
||||
static NSString * _Nonnull kNSDateHelperFormatTimeWithPrefix = @"'at' h:mm a";
|
||||
|
||||
static NSString *kNSDateHelperFormatSQLTime = @"HH:mm:ss";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLTime = @"HH:mm:ss";
|
||||
|
||||
static NSString *kNSDateHelperFormatSQLDate = @"yyyy-MM-dd";
|
||||
static NSString *kNSDateHelperFormatSQLDate_shashSeparated = @"yyyy/MM/dd";
|
||||
static NSString *kNSDateHelperFormatSQLDate_notSlashSeparated = @"yyyy-MM-dd";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDate = @"yyyy-MM-dd";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDate_shashSeparated = @"yyyy/MM/dd";
|
||||
|
||||
static NSString *kNSDateHelperFormatSQLDateIT = @"dd-MM-yyyy";
|
||||
static NSString *kNSDateHelperFormatSQLDateIT_shashSeparated = @"dd/MM/yyyy";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateIT = @"dd-MM-yyyy";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateIT_shashSeparated = @"dd/MM/yyyy";
|
||||
|
||||
static NSString *kNSDateHelperFormatSQLDateEN = @"MM-dd-yyyy";
|
||||
static NSString *kNSDateHelperFormatSQLDateEN_shashSeparated = @"MM/dd/yyyy";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateEN = @"MM-dd-yyyy";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateEN_shashSeparated = @"MM/dd/yyyy";
|
||||
|
||||
static NSString *kNSDateHelperFormatSQLDateWithTime = @"yyyy-MM-dd HH:mm:ss";
|
||||
static NSString *kNSDateHelperFormatSQLDateWithTime_shashSeparated = @"yyyy/MM/dd HH:mm:ss";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTime = @"yyyy-MM-dd HH:mm:ss";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTime_shashSeparated = @"yyyy/MM/dd HH:mm:ss";
|
||||
|
||||
static NSString *kNSDateHelperFormatSQLDateWithTimeIT = @"dd-MM-yyyy HH:mm:ss";
|
||||
static NSString *kNSDateHelperFormatSQLDateWithTimeIT_shashSeparated = @"dd/MM/yyyy HH:mm:ss";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeIT = @"dd-MM-yyyy HH:mm:ss";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeIT_shashSeparated = @"dd/MM/yyyy HH:mm:ss";
|
||||
|
||||
static NSString *kNSDateHelperFormatSQLDateWithTimeEN = @"MM-dd-yyyy HH:mm:ss";
|
||||
static NSString *kNSDateHelperFormatSQLDateWithTimeEN_shashSeparated = @"MM/dd/yyyy HH:mm:ss";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeEN = @"MM-dd-yyyy HH:mm:ss";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeEN_shashSeparated = @"MM/dd/yyyy HH:mm:ss";
|
||||
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZone = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZone_shashSeparated = @"yyyy/MM/dd'T'HH:mm:ss'Z'";
|
||||
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZoneEN = @"MM-dd-yyyy'T'HH:mm:ss'Z'";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZoneEN_shashSeparated = @"MM/dd/yyyy'T'HH:mm:ss'Z'";
|
||||
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZoneIT = @"dd-MM-yyyy'T'HH:mm:ss'Z'";
|
||||
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZoneIT_shashSeparated = @"dd/MM/yyyy'T'HH:mm:ss'Z'";
|
||||
|
||||
@interface NSDate (NSDate_Util)
|
||||
|
||||
+ (void)initializeStatics;
|
||||
|
||||
+ (NSCalendar *) sharedCalendar;
|
||||
+ (NSDateFormatter *) sharedDateFormatter;
|
||||
+ (NSCalendar * _Nonnull) sharedCalendar;
|
||||
+ (NSDateFormatter * _Nonnull) sharedDateFormatter;
|
||||
|
||||
- (NSDate *) dateToNearestMinutes:(NSInteger)minutes;
|
||||
+ (NSDate * _Nullable) dateFromString:(NSString * _Nonnull) string;
|
||||
+ (NSDate * _Nullable) dateFromString:(NSString * _Nonnull) string withFormat:(NSString * _Nonnull) format;
|
||||
+ (NSDate * _Nullable) dateFromString:(NSString * _Nonnull) string withFormat:(NSString * _Nonnull) format withTimeZone:(NSTimeZone * _Nonnull) timeZone;
|
||||
+ (NSDate * _Nullable) dateFromISO8601String:(NSString * _Nonnull) string;
|
||||
|
||||
- (NSDate *) dateByAddingMinutes:(NSInteger) dMinutes;
|
||||
- (NSDate *) dateByAddingHours:(NSInteger) hours;
|
||||
- (NSDate*) dateByAddingDays:(NSInteger) days;
|
||||
- (NSDate*) dateByAddingYears:(NSInteger) years;
|
||||
+ (NSString * _Nonnull) stringFromDate:(NSDate * _Nonnull) date withFormat:(NSString * _Nonnull) string;
|
||||
+ (NSString * _Nonnull) stringFromDate:(NSDate * _Nonnull) date;
|
||||
+ (NSString * _Nonnull) stringForDisplayFromDate:(NSDate * _Nonnull) date;
|
||||
+ (NSString * _Nonnull) stringForDisplayFromDate:(NSDate * _Nonnull) date prefixed:(BOOL) prefixed;
|
||||
+ (NSString * _Nonnull) stringForDisplayFromDate:(NSDate * _Nonnull) date prefixed:(BOOL) prefixed alwaysDisplayTime:(BOOL)displayTime;
|
||||
|
||||
- (NSDate *) dateBySubtractingMinutes:(NSInteger) dMinutes;
|
||||
- (NSDate *) dateBySubtractingHours:(NSInteger) hours;
|
||||
- (NSDate *) dateBySubtractingDays:(NSInteger) days;
|
||||
- (NSDate *) dateBySubtractingYears:(NSInteger) years;
|
||||
+ (NSString * _Nonnull) dateFormatString;
|
||||
+ (NSString * _Nonnull) timeFormatString;
|
||||
+ (NSString * _Nonnull) timestampFormatString;
|
||||
+ (NSString * _Nonnull) dbFormatString;
|
||||
|
||||
- (NSDate *) dateAtStartOfDay;
|
||||
- (NSDate *) dateAtEndOfDay;
|
||||
+ (NSString * _Nonnull) parseDateFormatFromString:(NSString * _Nonnull) dateString;
|
||||
|
||||
- (NSInteger) minutesAfterDate:(NSDate *) aDate;
|
||||
- (CGFloat) distanceInWeeksToDate:(NSDate *) anotherDate;
|
||||
- (CGFloat) distanceInDaysToDate:(NSDate *) anotherDate;
|
||||
- (CGFloat) distanceInHoursToDate:(NSDate *) anotherDate;
|
||||
- (CGFloat) distanceInMinutesToDate:(NSDate *) anotherDate;
|
||||
- (CGFloat) distanceInSeconsToDate:(NSDate *) anotherDate;
|
||||
+ (NSString * _Nonnull) WCFStringFromDate:(NSDate * _Nonnull) date;
|
||||
|
||||
- (NSDate *) toLocalTime;
|
||||
- (NSDate *) toGlobalTime;
|
||||
+ (NSArray * _Nullable) getSlotTimesFromDate:(NSDate * _Nonnull) date distanceMinutes:(NSInteger) minutes;
|
||||
|
||||
- (BOOL) isSameYearAsDate:(NSDate *) aDate;
|
||||
- (BOOL) isEarlierThanDate:(NSDate *) aDate;
|
||||
- (BOOL) isLaterThanDate:(NSDate *) aDate;
|
||||
+ (NSString * _Nonnull) getUniversalHourFromDateString:(NSString * _Nonnull) string formatterString:(NSString * _Nonnull) formatterString andUppercaseString:(BOOL) uppercaseString;
|
||||
|
||||
- (NSDate * _Nonnull) dateToNearestMinutes:(NSInteger)minutes;
|
||||
|
||||
- (NSDate * _Nonnull) dateByAddingMinutes:(NSInteger) dMinutes;
|
||||
- (NSDate * _Nonnull) dateByAddingHours:(NSInteger) hours;
|
||||
- (NSDate * _Nonnull) dateByAddingDays:(NSInteger) days;
|
||||
- (NSDate * _Nonnull) dateByAddingYears:(NSInteger) years;
|
||||
|
||||
- (NSDate * _Nonnull) dateBySubtractingMinutes:(NSInteger) dMinutes;
|
||||
- (NSDate * _Nonnull) dateBySubtractingHours:(NSInteger) hours;
|
||||
- (NSDate * _Nonnull) dateBySubtractingDays:(NSInteger) days;
|
||||
- (NSDate * _Nonnull) dateBySubtractingYears:(NSInteger) years;
|
||||
|
||||
- (NSDate * _Nonnull) dateAtStartOfDay;
|
||||
- (NSDate * _Nonnull) dateAtEndOfDay;
|
||||
|
||||
- (NSInteger) minutesAfterDate:(NSDate * _Nonnull) aDate;
|
||||
- (CGFloat) distanceInWeeksToDate:(NSDate * _Nonnull) anotherDate;
|
||||
- (CGFloat) distanceInDaysToDate:(NSDate * _Nonnull) anotherDate;
|
||||
- (CGFloat) distanceInHoursToDate:(NSDate * _Nonnull) anotherDate;
|
||||
- (CGFloat) distanceInMinutesToDate:(NSDate * _Nonnull) anotherDate;
|
||||
- (CGFloat) distanceInSeconsToDate:(NSDate * _Nonnull) anotherDate;
|
||||
|
||||
- (NSDate * _Nonnull) toLocalTime;
|
||||
- (NSDate * _Nonnull) toGlobalTime;
|
||||
|
||||
- (BOOL) isSameYearAsDate:(NSDate * _Nonnull) aDate;
|
||||
- (BOOL) isEarlierThanDate:(NSDate * _Nonnull) aDate;
|
||||
- (BOOL) isLaterThanDate:(NSDate * _Nonnull) aDate;
|
||||
|
||||
- (NSUInteger) daysAgo;
|
||||
- (NSUInteger) daysAgoAgainstMidnight;
|
||||
- (NSString *) stringDaysAgo;
|
||||
- (NSString *) stringDaysAgoAgainstMidnight:(BOOL)flag;
|
||||
- (NSString * _Nonnull) stringDaysAgo;
|
||||
- (NSString * _Nonnull) stringDaysAgoAgainstMidnight:(BOOL)flag;
|
||||
- (NSUInteger) monthDay;
|
||||
- (NSUInteger) weekday;
|
||||
- (NSUInteger) month;
|
||||
@ -91,37 +123,19 @@ static NSString *kNSDateHelperFormatSQLDateWithTimeEN_shashSeparated = @"
|
||||
- (NSUInteger) minute;
|
||||
- (NSUInteger) year;
|
||||
- (long int) utcTimeStamp; //full seconds since
|
||||
+ (NSDate *) dateFromString:(NSString *) string;
|
||||
+ (NSDate *) dateFromString:(NSString *) string withFormat:(NSString *) format;
|
||||
+ (NSDate *) dateFromString:(NSString *) string withFormat:(NSString *) format withTimeZone:(NSTimeZone*) timeZone;
|
||||
+ (NSString *) stringFromDate:(NSDate *) date withFormat:(NSString *) string;
|
||||
+ (NSString *) stringFromDate:(NSDate *) date;
|
||||
+ (NSString *) stringForDisplayFromDate:(NSDate *) date;
|
||||
+ (NSString *) stringForDisplayFromDate:(NSDate *) date prefixed:(BOOL) prefixed;
|
||||
+ (NSString *) stringForDisplayFromDate:(NSDate *) date prefixed:(BOOL) prefixed alwaysDisplayTime:(BOOL)displayTime;
|
||||
- (NSString *) monthSymbol;
|
||||
- (NSString *) string;
|
||||
- (NSString *) stringWithFormat:(NSString *) format;
|
||||
- (NSString *) stringWithFormat:(NSString *) format timeZone:(NSTimeZone*) timezone;
|
||||
- (NSString *) stringWithDateStyle:(NSDateFormatterStyle) dateStyle timeStyle:(NSDateFormatterStyle) timeStyle;
|
||||
- (NSDate *) beginningOfWeek;
|
||||
- (NSDate *) beginningOfDay;
|
||||
- (NSDate *) endOfWeek;
|
||||
+ (NSString *) dateFormatString;
|
||||
+ (NSString *) timeFormatString;
|
||||
+ (NSString *) timestampFormatString;
|
||||
+ (NSString *) dbFormatString;
|
||||
|
||||
+ (NSString *) parseDateFormatFromString:(NSString *) dateString;
|
||||
- (NSString * _Nonnull) monthSymbol;
|
||||
- (NSString * _Nonnull) string;
|
||||
- (NSString * _Nullable) stringWithFormat:(NSString * _Nonnull) format;
|
||||
- (NSString * _Nullable) stringWithFormat:(NSString * _Nonnull) format timeZone:(NSTimeZone * _Nonnull) timezone;
|
||||
- (NSString * _Nonnull) stringWithDateStyle:(NSDateFormatterStyle) dateStyle timeStyle:(NSDateFormatterStyle) timeStyle;
|
||||
- (NSDate * _Nonnull) beginningOfWeek;
|
||||
- (NSDate * _Nonnull) beginningOfDay;
|
||||
- (NSDate * _Nonnull) endOfWeek;
|
||||
|
||||
+ (NSString *) WCFStringFromDate:(NSDate*) date;
|
||||
- (BOOL)isSameDayAsDate:(NSDate * _Nonnull)otherDate;
|
||||
|
||||
+ (NSArray*) getSlotTimesFromDate:(NSDate*) date distanceMinutes:(NSInteger) minutes;
|
||||
|
||||
- (BOOL)isSameDayAsDate:(NSDate*)otherDate;
|
||||
|
||||
+ (NSString *) getUniversalHourFromDateString:(NSString *) string formatterString:(NSString*) formatterString andUppercaseString:(BOOL) uppercaseString;
|
||||
- (NSString *) getUniversalHourUppercaseString:(BOOL) uppercaseString;
|
||||
- (NSString *) getNotificationUniversalHourUppercaseString:(BOOL) uppercaseString;
|
||||
- (NSString * _Nonnull) getUniversalHourUppercaseString:(BOOL) uppercaseString;
|
||||
- (NSString * _Nonnull) getNotificationUniversalHourUppercaseString:(BOOL) uppercaseString;
|
||||
|
||||
@end
|
||||
|
||||
@ -12,9 +12,6 @@
|
||||
#define D_MINUTE 60
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@implementation NSDate (NSDate_Util)
|
||||
|
||||
static NSCalendar *_calendar = nil;
|
||||
@ -25,11 +22,7 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
dispatch_once(&onceToken, ^{
|
||||
@autoreleasepool {
|
||||
if (_calendar == nil) {
|
||||
#if __has_feature(objc_arc)
|
||||
_calendar = [NSCalendar currentCalendar];
|
||||
#else
|
||||
_calendar = [[NSCalendar currentCalendar] retain];
|
||||
#endif
|
||||
}
|
||||
if (_displayFormatter == nil) {
|
||||
_displayFormatter = [[NSDateFormatter alloc] init];
|
||||
@ -38,168 +31,6 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
});
|
||||
}
|
||||
|
||||
- (NSDate *) dateAtStartOfDay
|
||||
{
|
||||
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
|
||||
components.hour = 0;
|
||||
components.minute = 0;
|
||||
components.second = 0;
|
||||
return [CURRENT_CALENDAR dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSDate *) dateAtEndOfDay
|
||||
{
|
||||
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
|
||||
components.hour = 23;
|
||||
components.minute = 59;
|
||||
components.second = 59;
|
||||
return [CURRENT_CALENDAR dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSInteger) minutesAfterDate: (NSDate *) aDate
|
||||
{
|
||||
NSTimeInterval ti = [self timeIntervalSinceDate:aDate];
|
||||
return (NSInteger) (ti / D_MINUTE);
|
||||
}
|
||||
|
||||
|
||||
- (NSDate *) dateBySubtractingMinutes: (NSInteger) dMinutes
|
||||
{
|
||||
return [self dateByAddingMinutes: (dMinutes * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *) dateBySubtractingHours: (NSInteger) hours {
|
||||
|
||||
return [self dateByAddingHours: (hours * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *) dateBySubtractingDays: (NSInteger) days
|
||||
{
|
||||
return [self dateByAddingDays: (days * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *) dateBySubtractingYears: (NSInteger) years {
|
||||
|
||||
|
||||
return [self dateByAddingYears: (years * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *)dateToNearestMinutes:(NSInteger)minutes {
|
||||
unsigned unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal;
|
||||
// Extract components.
|
||||
NSDateComponents *time = [[NSCalendar currentCalendar] components:unitFlags fromDate:self];
|
||||
NSInteger thisMin = [time minute];
|
||||
NSDate *newDate;
|
||||
long remain = thisMin % minutes;
|
||||
// if less then 3 then round down
|
||||
NSInteger dividor = ceil(minutes/2);
|
||||
if (remain<dividor){
|
||||
// Subtract the remainder of time to the date to round it down evenly
|
||||
newDate = [self dateByAddingTimeInterval:-60*(remain)];
|
||||
}else{
|
||||
// Add the remainder of time to the date to round it up evenly
|
||||
newDate = [self dateByAddingTimeInterval:60*(minutes-remain)];
|
||||
}
|
||||
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:newDate];
|
||||
[comps setSecond:0];
|
||||
return [[NSCalendar currentCalendar] dateFromComponents:comps];
|
||||
}
|
||||
- (BOOL) isEarlierThanDate: (NSDate *) aDate
|
||||
{
|
||||
return ([self compare:aDate] == NSOrderedAscending);
|
||||
}
|
||||
|
||||
- (BOOL) isLaterThanDate: (NSDate *) aDate
|
||||
{
|
||||
return ([self compare:aDate] == NSOrderedDescending);
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInWeeksToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInDaysToDate:anotherDate]/7;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInDaysToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInHoursToDate:anotherDate]/24;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInHoursToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInMinutesToDate:anotherDate]/60;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInMinutesToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInSeconsToDate:anotherDate]/60;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInSeconsToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return -[self timeIntervalSinceDate:anotherDate];
|
||||
}
|
||||
|
||||
- (NSString *) monthSymbol {
|
||||
|
||||
return [[[NSDate sharedDateFormatter] monthSymbols] objectAtIndex:([self month]-1)];
|
||||
}
|
||||
|
||||
-(NSDate *) toLocalTime
|
||||
{
|
||||
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
|
||||
NSInteger seconds = [tz secondsFromGMTForDate: self];
|
||||
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
|
||||
}
|
||||
|
||||
-(NSDate *) toGlobalTime
|
||||
{
|
||||
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
|
||||
NSInteger seconds = -[tz secondsFromGMTForDate: self];
|
||||
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
|
||||
}
|
||||
|
||||
- (BOOL) isSameYearAsDate: (NSDate *) aDate
|
||||
{
|
||||
NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
|
||||
NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:aDate];
|
||||
return (components1.year == components2.year);
|
||||
}
|
||||
|
||||
- (NSDate *) dateByAddingMinutes: (NSInteger) dMinutes
|
||||
{
|
||||
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
|
||||
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
|
||||
return newDate;
|
||||
}
|
||||
|
||||
- (NSDate*) dateByAddingHours:(NSInteger) hours {
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
|
||||
|
||||
NSCalendar *gregorian = [NSCalendar currentCalendar];
|
||||
NSDateComponents *comps = [gregorian components:unitFlags fromDate:self];
|
||||
[comps setHour:[comps hour] + hours];
|
||||
return [gregorian dateFromComponents:comps];
|
||||
}
|
||||
|
||||
- (NSDate*) dateByAddingDays:(NSInteger) days {
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
|
||||
NSCalendar *gregorian = [NSCalendar currentCalendar];
|
||||
NSDateComponents *comps = [gregorian components:unitFlags fromDate:self];
|
||||
[comps setDay:[comps day] + days];
|
||||
return [gregorian dateFromComponents:comps];
|
||||
}
|
||||
|
||||
- (NSDate*) dateByAddingYears:(NSInteger) years {
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
|
||||
NSCalendar *gregorian = [NSCalendar currentCalendar];
|
||||
NSDateComponents *comps = [gregorian components:unitFlags fromDate:self];
|
||||
[comps setYear:[comps year] + years];
|
||||
return [gregorian dateFromComponents:comps];
|
||||
}
|
||||
|
||||
+ (NSCalendar *)sharedCalendar {
|
||||
[self initializeStatics];
|
||||
return _calendar;
|
||||
@ -210,91 +41,6 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
return _displayFormatter;
|
||||
}
|
||||
|
||||
/*
|
||||
* This guy can be a little unreliable and produce unexpected results,
|
||||
* you're better off using daysAgoAgainstMidnight
|
||||
*/
|
||||
- (NSUInteger)daysAgo {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *components = [calendar components:(NSCalendarUnitDay)
|
||||
fromDate:self
|
||||
toDate:[NSDate date]
|
||||
options:0];
|
||||
return [components day];
|
||||
}
|
||||
|
||||
- (NSUInteger)daysAgoAgainstMidnight {
|
||||
// get a midnight version of ourself:
|
||||
NSDateFormatter *mdf = [[NSDateFormatter alloc] init];
|
||||
[mdf setDateFormat:@"yyyy-MM-dd"];
|
||||
NSDate *midnight = [mdf dateFromString:[mdf stringFromDate:self]];
|
||||
return (int)[midnight timeIntervalSinceNow] / (60*60*24) *-1;
|
||||
}
|
||||
|
||||
- (NSString *)stringDaysAgo {
|
||||
return [self stringDaysAgoAgainstMidnight:YES];
|
||||
}
|
||||
|
||||
- (NSString *)stringDaysAgoAgainstMidnight:(BOOL)flag {
|
||||
NSUInteger daysAgo = (flag) ? [self daysAgoAgainstMidnight] : [self daysAgo];
|
||||
NSString *text = nil;
|
||||
switch (daysAgo) {
|
||||
case 0:
|
||||
text = NSLocalizedString(@"Today", nil);
|
||||
break;
|
||||
case 1:
|
||||
text = NSLocalizedString(@"Yesterday", nil);
|
||||
break;
|
||||
default:
|
||||
text = [NSString stringWithFormat:@"%ld days ago", (long)daysAgo];
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
- (NSUInteger)hour {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *weekdayComponents = [calendar components:(NSCalendarUnitHour) fromDate:self];
|
||||
return [weekdayComponents hour];
|
||||
}
|
||||
|
||||
- (NSUInteger)minute {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *weekdayComponents = [calendar components:(NSCalendarUnitMinute) fromDate:self];
|
||||
return [weekdayComponents minute];
|
||||
}
|
||||
|
||||
- (NSUInteger)year {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *weekdayComponents = [calendar components:(NSCalendarUnitYear) fromDate:self];
|
||||
return [weekdayComponents year];
|
||||
}
|
||||
|
||||
- (long int)utcTimeStamp{
|
||||
return lround(floor([self timeIntervalSince1970]));
|
||||
}
|
||||
|
||||
- (NSUInteger)monthDay {
|
||||
|
||||
NSDateComponents *weekdayComponents = [[[self class] sharedCalendar] components:(NSCalendarUnitDay) fromDate:self];
|
||||
return [weekdayComponents day];
|
||||
}
|
||||
|
||||
- (NSUInteger)weekday {
|
||||
NSDateComponents *weekdayComponents = [[[self class] sharedCalendar] components:(NSCalendarUnitWeekday) fromDate:self];
|
||||
return [weekdayComponents weekday];
|
||||
}
|
||||
|
||||
- (NSUInteger)month {
|
||||
NSDateComponents *weekdayComponents = [[[self class] sharedCalendar] components:(NSCalendarUnitMonth) fromDate:self];
|
||||
return [weekdayComponents month];
|
||||
}
|
||||
|
||||
- (NSUInteger)weekNumber {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitWeekOfMonth) fromDate:self];
|
||||
return [dateComponents weekOfYear];
|
||||
}
|
||||
|
||||
+ (NSDate *)dateFromString:(NSString *)string {
|
||||
return [NSDate dateFromString:string withFormat:[NSDate dbFormatString]];
|
||||
}
|
||||
@ -316,6 +62,13 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
return date;
|
||||
}
|
||||
|
||||
+ (NSDate *) dateFromISO8601String:(NSString*) string {
|
||||
|
||||
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
|
||||
|
||||
return [formatter dateFromString:string];
|
||||
}
|
||||
|
||||
+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format {
|
||||
return [date stringWithFormat:format];
|
||||
}
|
||||
@ -360,9 +113,7 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
|
||||
[componentsToSubtract setDay:-7];
|
||||
NSDate *lastweek = [[self sharedCalendar] dateByAddingComponents:componentsToSubtract toDate:today options:0];
|
||||
#if !__has_feature(objc_arc)
|
||||
[componentsToSubtract release];
|
||||
#endif
|
||||
|
||||
NSComparisonResult lastweek_result = [date compare:lastweek];
|
||||
if (lastweek_result == NSOrderedDescending) {
|
||||
if (displayTime) {
|
||||
@ -411,90 +162,6 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
return [self stringForDisplayFromDate:date prefixed:NO];
|
||||
}
|
||||
|
||||
- (NSString *)stringWithFormat:(NSString *)format {
|
||||
|
||||
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
|
||||
[outputFormatter setDateFormat:format];
|
||||
NSString *timestamp_str = [outputFormatter stringFromDate:self];
|
||||
|
||||
return timestamp_str;
|
||||
}
|
||||
|
||||
- (NSString *)stringWithFormat:(NSString *)format timeZone:(NSTimeZone*) timezone {
|
||||
|
||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||
[formatter setDateFormat:format];
|
||||
[formatter setTimeZone:timezone];
|
||||
|
||||
NSString *timestamp_str = [formatter stringFromDate:self];
|
||||
return timestamp_str;
|
||||
}
|
||||
|
||||
- (NSString *)string {
|
||||
return [self stringWithFormat:[NSDate dbFormatString]];
|
||||
}
|
||||
|
||||
- (NSString *)stringWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle {
|
||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||
|
||||
[formatter setDateStyle:dateStyle];
|
||||
[formatter setTimeStyle:timeStyle];
|
||||
NSString *outputString = [formatter stringFromDate:self];
|
||||
return outputString;
|
||||
}
|
||||
|
||||
- (NSDate *)beginningOfWeek {
|
||||
// largely borrowed from "Date and Time Programming Guide for Cocoa"
|
||||
// we'll use the default calendar and hope for the best
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDate *beginningOfWeek = nil;
|
||||
BOOL ok = [calendar rangeOfUnit:NSCalendarUnitWeekOfMonth startDate:&beginningOfWeek
|
||||
interval:NULL forDate:self];
|
||||
if (ok) {
|
||||
return beginningOfWeek;
|
||||
}
|
||||
// couldn't calc via range, so try to grab Sunday, assuming gregorian style
|
||||
// Get the weekday component of the current date
|
||||
NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:self];
|
||||
/*
|
||||
Create a date components to represent the number of days to subtract from the current date.
|
||||
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question. (If today's Sunday, subtract 0 days.)
|
||||
*/
|
||||
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
|
||||
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];
|
||||
beginningOfWeek = nil;
|
||||
beginningOfWeek = [calendar dateByAddingComponents:componentsToSubtract toDate:self options:0];
|
||||
#if !__has_feature(objc_arc)
|
||||
[componentsToSubtract release];
|
||||
#endif
|
||||
//normalize to midnight, extract the year, month, and day components and create a new date from those components.
|
||||
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
|
||||
fromDate:beginningOfWeek];
|
||||
return [calendar dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSDate *)beginningOfDay {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
// Get the weekday component of the current date
|
||||
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
|
||||
fromDate:self];
|
||||
return [calendar dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSDate *)endOfWeek {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
// Get the weekday component of the current date
|
||||
NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:self];
|
||||
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
|
||||
// to get the end of week for a particular date, add (7 - weekday) days
|
||||
[componentsToAdd setDay:(7 - [weekdayComponents weekday])];
|
||||
NSDate *endOfWeek = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
|
||||
#if !__has_feature(objc_arc)
|
||||
[componentsToAdd release];
|
||||
#endif
|
||||
return endOfWeek;
|
||||
}
|
||||
|
||||
+ (NSString *)dateFormatString {
|
||||
return kNSDateHelperFormatSQLDate;
|
||||
}
|
||||
@ -516,7 +183,18 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
|
||||
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
||||
|
||||
NSArray *dateFormatsArray = [[NSArray alloc] initWithObjects:kNSDateHelperFormatSQLDate,kNSDateHelperFormatSQLDateIT,kNSDateHelperFormatSQLDate_notSlashSeparated,kNSDateHelperFormatSQLDateEN,kNSDateHelperFormatSQLDateIT_shashSeparated,kNSDateHelperFormatSQLDateEN_shashSeparated,kNSDateHelperFormatSQLTime,kNSDateHelperFormatSQLDateWithTime,kNSDateHelperFormatSQLDate_shashSeparated,kNSDateHelperFormatSQLDateWithTime_shashSeparated,kNSDateHelperFormatFullDateWithTime,kNSDateHelperFormatFullDate,kNSDateHelperFormatShortDateWithTime,kNSDateHelperFormatShortDate,kNSDateHelperFormatWeekday,kNSDateHelperFormatWeekdayWithTime,kNSDateHelperFormatTime,kNSDateHelperFormatTimeWithPrefix, nil];
|
||||
NSArray *dateFormatsArray = [[NSArray alloc] initWithObjects:kNSDateHelperFormatFullDateWithTime,kNSDateHelperFormatFullDate,
|
||||
kNSDateHelperFormatShortDateWithTime,kNSDateHelperFormatShortDate,kNSDateHelperFormatWeekday,
|
||||
kNSDateHelperFormatWeekdayWithTime,kNSDateHelperFormatTime,kNSDateHelperFormatTimeWithPrefix,
|
||||
kNSDateHelperFormatSQLTime,kNSDateHelperFormatSQLDate,kNSDateHelperFormatSQLDate_shashSeparated,
|
||||
kNSDateHelperFormatSQLDateIT,kNSDateHelperFormatSQLDateIT_shashSeparated,kNSDateHelperFormatSQLDateEN,
|
||||
kNSDateHelperFormatSQLDateEN_shashSeparated,kNSDateHelperFormatSQLDateWithTime,
|
||||
kNSDateHelperFormatSQLDateWithTime_shashSeparated,kNSDateHelperFormatSQLDateWithTimeIT,
|
||||
kNSDateHelperFormatSQLDateWithTimeIT_shashSeparated,kNSDateHelperFormatSQLDateWithTimeEN,
|
||||
kNSDateHelperFormatSQLDateWithTimeEN_shashSeparated,kNSDateHelperFormatSQLDateWithTimeZone,
|
||||
kNSDateHelperFormatSQLDateWithTimeZone_shashSeparated,kNSDateHelperFormatSQLDateWithTimeZoneEN,
|
||||
kNSDateHelperFormatSQLDateWithTimeZoneEN_shashSeparated,kNSDateHelperFormatSQLDateWithTimeZoneIT,
|
||||
kNSDateHelperFormatSQLDateWithTimeZoneIT_shashSeparated, nil];
|
||||
|
||||
|
||||
for (NSString *dateFormat in dateFormatsArray) {
|
||||
@ -561,20 +239,6 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
return slotTimes;
|
||||
}
|
||||
|
||||
- (BOOL)isSameDayAsDate:(NSDate*)otherDate {
|
||||
|
||||
// From progrmr's answer...
|
||||
NSCalendar* calendar = [NSCalendar currentCalendar];
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
|
||||
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:self];
|
||||
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:otherDate];
|
||||
|
||||
return [comp1 day] == [comp2 day] &&
|
||||
[comp1 month] == [comp2 month] &&
|
||||
[comp1 year] == [comp2 year];
|
||||
}
|
||||
|
||||
+ (NSString *)getUniversalHourFromDateString:(NSString *)string formatterString:(NSString*) formatterString andUppercaseString:(BOOL)uppercaseString {
|
||||
|
||||
NSString * dateString = [NSString stringWithString:string];
|
||||
@ -851,6 +515,344 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (NSDate *) dateAtStartOfDay
|
||||
{
|
||||
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
|
||||
components.hour = 0;
|
||||
components.minute = 0;
|
||||
components.second = 0;
|
||||
return [CURRENT_CALENDAR dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSDate *) dateAtEndOfDay
|
||||
{
|
||||
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
|
||||
components.hour = 23;
|
||||
components.minute = 59;
|
||||
components.second = 59;
|
||||
return [CURRENT_CALENDAR dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSInteger) minutesAfterDate: (NSDate *) aDate
|
||||
{
|
||||
NSTimeInterval ti = [self timeIntervalSinceDate:aDate];
|
||||
return (NSInteger) (ti / D_MINUTE);
|
||||
}
|
||||
|
||||
- (NSDate *) dateBySubtractingMinutes: (NSInteger) dMinutes
|
||||
{
|
||||
return [self dateByAddingMinutes: (dMinutes * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *) dateBySubtractingHours: (NSInteger) hours {
|
||||
|
||||
return [self dateByAddingHours: (hours * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *) dateBySubtractingDays: (NSInteger) days
|
||||
{
|
||||
return [self dateByAddingDays: (days * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *) dateBySubtractingYears: (NSInteger) years {
|
||||
|
||||
|
||||
return [self dateByAddingYears: (years * -1)];
|
||||
}
|
||||
|
||||
- (NSDate *)dateToNearestMinutes:(NSInteger)minutes {
|
||||
unsigned unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal;
|
||||
// Extract components.
|
||||
NSDateComponents *time = [[NSCalendar currentCalendar] components:unitFlags fromDate:self];
|
||||
NSInteger thisMin = [time minute];
|
||||
NSDate *newDate;
|
||||
long remain = thisMin % minutes;
|
||||
// if less then 3 then round down
|
||||
NSInteger dividor = ceil(minutes/2);
|
||||
if (remain<dividor){
|
||||
// Subtract the remainder of time to the date to round it down evenly
|
||||
newDate = [self dateByAddingTimeInterval:-60*(remain)];
|
||||
}else{
|
||||
// Add the remainder of time to the date to round it up evenly
|
||||
newDate = [self dateByAddingTimeInterval:60*(minutes-remain)];
|
||||
}
|
||||
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:newDate];
|
||||
[comps setSecond:0];
|
||||
return [[NSCalendar currentCalendar] dateFromComponents:comps];
|
||||
}
|
||||
- (BOOL) isEarlierThanDate: (NSDate *) aDate
|
||||
{
|
||||
return ([self compare:aDate] == NSOrderedAscending);
|
||||
}
|
||||
|
||||
- (BOOL) isLaterThanDate: (NSDate *) aDate
|
||||
{
|
||||
return ([self compare:aDate] == NSOrderedDescending);
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInWeeksToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInDaysToDate:anotherDate]/7;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInDaysToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInHoursToDate:anotherDate]/24;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInHoursToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInMinutesToDate:anotherDate]/60;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInMinutesToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return [self distanceInSeconsToDate:anotherDate]/60;
|
||||
}
|
||||
|
||||
- (CGFloat) distanceInSeconsToDate:(NSDate *)anotherDate
|
||||
{
|
||||
return -[self timeIntervalSinceDate:anotherDate];
|
||||
}
|
||||
|
||||
- (NSString *) monthSymbol {
|
||||
|
||||
return [[[NSDate sharedDateFormatter] monthSymbols] objectAtIndex:([self month]-1)];
|
||||
}
|
||||
|
||||
-(NSDate *) toLocalTime
|
||||
{
|
||||
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
|
||||
NSInteger seconds = [tz secondsFromGMTForDate: self];
|
||||
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
|
||||
}
|
||||
|
||||
-(NSDate *) toGlobalTime
|
||||
{
|
||||
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
|
||||
NSInteger seconds = -[tz secondsFromGMTForDate: self];
|
||||
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
|
||||
}
|
||||
|
||||
- (BOOL) isSameYearAsDate: (NSDate *) aDate
|
||||
{
|
||||
NSDateComponents *components1 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:self];
|
||||
NSDateComponents *components2 = [CURRENT_CALENDAR components:NSCalendarUnitYear fromDate:aDate];
|
||||
return (components1.year == components2.year);
|
||||
}
|
||||
|
||||
- (NSDate *) dateByAddingMinutes: (NSInteger) dMinutes
|
||||
{
|
||||
NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + D_MINUTE * dMinutes;
|
||||
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
|
||||
return newDate;
|
||||
}
|
||||
|
||||
- (NSDate*) dateByAddingHours:(NSInteger) hours {
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
|
||||
|
||||
NSCalendar *gregorian = [NSCalendar currentCalendar];
|
||||
NSDateComponents *comps = [gregorian components:unitFlags fromDate:self];
|
||||
[comps setHour:[comps hour] + hours];
|
||||
return [gregorian dateFromComponents:comps];
|
||||
}
|
||||
|
||||
- (NSDate*) dateByAddingDays:(NSInteger) days {
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
|
||||
NSCalendar *gregorian = [NSCalendar currentCalendar];
|
||||
NSDateComponents *comps = [gregorian components:unitFlags fromDate:self];
|
||||
[comps setDay:[comps day] + days];
|
||||
return [gregorian dateFromComponents:comps];
|
||||
}
|
||||
|
||||
- (NSDate*) dateByAddingYears:(NSInteger) years {
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
|
||||
NSCalendar *gregorian = [NSCalendar currentCalendar];
|
||||
NSDateComponents *comps = [gregorian components:unitFlags fromDate:self];
|
||||
[comps setYear:[comps year] + years];
|
||||
return [gregorian dateFromComponents:comps];
|
||||
}
|
||||
|
||||
- (NSUInteger)daysAgo {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *components = [calendar components:(NSCalendarUnitDay)
|
||||
fromDate:self
|
||||
toDate:[NSDate date]
|
||||
options:0];
|
||||
return [components day];
|
||||
}
|
||||
|
||||
- (NSUInteger)daysAgoAgainstMidnight {
|
||||
// get a midnight version of ourself:
|
||||
NSDateFormatter *mdf = [[NSDateFormatter alloc] init];
|
||||
[mdf setDateFormat:@"yyyy-MM-dd"];
|
||||
NSDate *midnight = [mdf dateFromString:[mdf stringFromDate:self]];
|
||||
return (int)[midnight timeIntervalSinceNow] / (60*60*24) *-1;
|
||||
}
|
||||
|
||||
- (NSString *)stringDaysAgo {
|
||||
return [self stringDaysAgoAgainstMidnight:YES];
|
||||
}
|
||||
|
||||
- (NSString *)stringDaysAgoAgainstMidnight:(BOOL)flag {
|
||||
NSUInteger daysAgo = (flag) ? [self daysAgoAgainstMidnight] : [self daysAgo];
|
||||
NSString *text = nil;
|
||||
switch (daysAgo) {
|
||||
case 0:
|
||||
text = NSLocalizedString(@"Today", nil);
|
||||
break;
|
||||
case 1:
|
||||
text = NSLocalizedString(@"Yesterday", nil);
|
||||
break;
|
||||
default:
|
||||
text = [NSString stringWithFormat:@"%ld days ago", (long)daysAgo];
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
- (NSUInteger)hour {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *weekdayComponents = [calendar components:(NSCalendarUnitHour) fromDate:self];
|
||||
return [weekdayComponents hour];
|
||||
}
|
||||
|
||||
- (NSUInteger)minute {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *weekdayComponents = [calendar components:(NSCalendarUnitMinute) fromDate:self];
|
||||
return [weekdayComponents minute];
|
||||
}
|
||||
|
||||
- (NSUInteger)year {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *weekdayComponents = [calendar components:(NSCalendarUnitYear) fromDate:self];
|
||||
return [weekdayComponents year];
|
||||
}
|
||||
|
||||
- (long int)utcTimeStamp{
|
||||
return lround(floor([self timeIntervalSince1970]));
|
||||
}
|
||||
|
||||
- (NSUInteger)monthDay {
|
||||
|
||||
NSDateComponents *weekdayComponents = [[[self class] sharedCalendar] components:(NSCalendarUnitDay) fromDate:self];
|
||||
return [weekdayComponents day];
|
||||
}
|
||||
|
||||
- (NSUInteger)weekday {
|
||||
NSDateComponents *weekdayComponents = [[[self class] sharedCalendar] components:(NSCalendarUnitWeekday) fromDate:self];
|
||||
return [weekdayComponents weekday];
|
||||
}
|
||||
|
||||
- (NSUInteger)month {
|
||||
NSDateComponents *weekdayComponents = [[[self class] sharedCalendar] components:(NSCalendarUnitMonth) fromDate:self];
|
||||
return [weekdayComponents month];
|
||||
}
|
||||
|
||||
- (NSUInteger)weekNumber {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitWeekOfMonth) fromDate:self];
|
||||
return [dateComponents weekOfYear];
|
||||
}
|
||||
|
||||
- (NSString *)stringWithFormat:(NSString *)format {
|
||||
|
||||
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
|
||||
[outputFormatter setDateFormat:format];
|
||||
NSString *timestamp_str = [outputFormatter stringFromDate:self];
|
||||
|
||||
return timestamp_str;
|
||||
}
|
||||
|
||||
- (NSString *)stringWithFormat:(NSString *)format timeZone:(NSTimeZone*) timezone {
|
||||
|
||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||
[formatter setDateFormat:format];
|
||||
[formatter setTimeZone:timezone];
|
||||
|
||||
NSString *timestamp_str = [formatter stringFromDate:self];
|
||||
return timestamp_str;
|
||||
}
|
||||
|
||||
- (NSString *)string {
|
||||
return [self stringWithFormat:[NSDate dbFormatString]];
|
||||
}
|
||||
|
||||
- (NSString *)stringWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle {
|
||||
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
||||
|
||||
[formatter setDateStyle:dateStyle];
|
||||
[formatter setTimeStyle:timeStyle];
|
||||
NSString *outputString = [formatter stringFromDate:self];
|
||||
return outputString;
|
||||
}
|
||||
|
||||
- (NSDate *)beginningOfWeek {
|
||||
// largely borrowed from "Date and Time Programming Guide for Cocoa"
|
||||
// we'll use the default calendar and hope for the best
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
NSDate *beginningOfWeek = nil;
|
||||
BOOL ok = [calendar rangeOfUnit:NSCalendarUnitWeekOfMonth startDate:&beginningOfWeek
|
||||
interval:NULL forDate:self];
|
||||
if (ok) {
|
||||
return beginningOfWeek;
|
||||
}
|
||||
// couldn't calc via range, so try to grab Sunday, assuming gregorian style
|
||||
// Get the weekday component of the current date
|
||||
NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:self];
|
||||
/*
|
||||
Create a date components to represent the number of days to subtract from the current date.
|
||||
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question. (If today's Sunday, subtract 0 days.)
|
||||
*/
|
||||
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
|
||||
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];
|
||||
beginningOfWeek = nil;
|
||||
beginningOfWeek = [calendar dateByAddingComponents:componentsToSubtract toDate:self options:0];
|
||||
|
||||
//normalize to midnight, extract the year, month, and day components and create a new date from those components.
|
||||
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
|
||||
fromDate:beginningOfWeek];
|
||||
return [calendar dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSDate *)beginningOfDay {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
// Get the weekday component of the current date
|
||||
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
|
||||
fromDate:self];
|
||||
return [calendar dateFromComponents:components];
|
||||
}
|
||||
|
||||
- (NSDate *)endOfWeek {
|
||||
NSCalendar *calendar = [[self class] sharedCalendar];
|
||||
// Get the weekday component of the current date
|
||||
NSDateComponents *weekdayComponents = [calendar components:NSCalendarUnitWeekday fromDate:self];
|
||||
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
|
||||
// to get the end of week for a particular date, add (7 - weekday) days
|
||||
[componentsToAdd setDay:(7 - [weekdayComponents weekday])];
|
||||
NSDate *endOfWeek = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
|
||||
|
||||
return endOfWeek;
|
||||
}
|
||||
|
||||
- (BOOL)isSameDayAsDate:(NSDate*)otherDate {
|
||||
|
||||
// From progrmr's answer...
|
||||
NSCalendar* calendar = [NSCalendar currentCalendar];
|
||||
|
||||
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
|
||||
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:self];
|
||||
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:otherDate];
|
||||
|
||||
return [comp1 day] == [comp2 day] &&
|
||||
[comp1 month] == [comp2 month] &&
|
||||
[comp1 year] == [comp2 year];
|
||||
}
|
||||
|
||||
|
||||
- (NSString *)getUniversalHourUppercaseString:(BOOL)uppercaseString {
|
||||
|
||||
NSDate *now = [NSDate date];
|
||||
@ -1193,4 +1195,4 @@ static NSDateFormatter *_displayFormatter = nil;
|
||||
}
|
||||
return @"";
|
||||
}
|
||||
@end
|
||||
@end
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'PNObject'
|
||||
s.version = '1.1.1'
|
||||
s.version = '1.1.2'
|
||||
s.summary = 'PNObject is a simple replica of the more complex ParseObject'
|
||||
|
||||
|
||||
|
||||
@ -125,6 +125,10 @@
|
||||
}
|
||||
|
||||
NSDate *val = [[NSDate dateFromString:str withFormat:dateFormat] toLocalTime];
|
||||
|
||||
if (!val) {
|
||||
val = [NSDate dateFromISO8601String:str];
|
||||
}
|
||||
if (![self isObjNull:val]) {
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user