- Add dateFromISO8601String
- Aggiornata libreria - Release 1.0.3
This commit is contained in:
parent
2d47db6a48
commit
d8ae763ffa
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
Pod::Spec.new do |s|
|
Pod::Spec.new do |s|
|
||||||
s.name = "NSDate_Utils"
|
s.name = "NSDate_Utils"
|
||||||
s.version = "1.0.2"
|
s.version = "1.0.3"
|
||||||
s.summary = "NSDate_Utils is a NSDate category that helps date managements. For example NSString to NSDate, NSDate to NSString,NSSdate comparisons"
|
s.summary = "NSDate_Utils is a NSDate category that helps date managements. For example NSString to NSDate, NSDate to NSString,NSSdate comparisons"
|
||||||
|
|
||||||
# This description is used to generate tags and improve search results.
|
# This description is used to generate tags and improve search results.
|
||||||
|
|||||||
@ -13,84 +13,108 @@
|
|||||||
#define kMoonRiseMinutes 17*60
|
#define kMoonRiseMinutes 17*60
|
||||||
#define kMoonSetMinutes 7*60
|
#define kMoonSetMinutes 7*60
|
||||||
|
|
||||||
static NSString *kNSDateHelperFormatFullDateWithTime = @"MMM d, yyyy h:mm a";
|
static NSString * _Nonnull kNSDateHelperFormatFullDateWithTime = @"MMM d, yyyy h:mm a";
|
||||||
static NSString *kNSDateHelperFormatFullDate = @"MMM d, yyyy";
|
static NSString * _Nonnull kNSDateHelperFormatFullDate = @"MMM d, yyyy";
|
||||||
static NSString *kNSDateHelperFormatShortDateWithTime = @"MMM d h:mm a";
|
static NSString * _Nonnull kNSDateHelperFormatShortDateWithTime = @"MMM d h:mm a";
|
||||||
static NSString *kNSDateHelperFormatShortDate = @"MMM d";
|
static NSString * _Nonnull kNSDateHelperFormatShortDate = @"MMM d";
|
||||||
static NSString *kNSDateHelperFormatWeekday = @"EEEE";
|
static NSString * _Nonnull kNSDateHelperFormatWeekday = @"EEEE";
|
||||||
static NSString *kNSDateHelperFormatWeekdayWithTime = @"EEEE h:mm a";
|
static NSString * _Nonnull kNSDateHelperFormatWeekdayWithTime = @"EEEE h:mm a";
|
||||||
static NSString *kNSDateHelperFormatTime = @"h:mm a";
|
static NSString * _Nonnull kNSDateHelperFormatTime = @"h:mm a";
|
||||||
static NSString *kNSDateHelperFormatTimeWithPrefix = @"'at' 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 * _Nonnull kNSDateHelperFormatSQLDate = @"yyyy-MM-dd";
|
||||||
static NSString *kNSDateHelperFormatSQLDate_shashSeparated = @"yyyy/MM/dd";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDate_shashSeparated = @"yyyy/MM/dd";
|
||||||
|
|
||||||
static NSString *kNSDateHelperFormatSQLDateIT = @"dd-MM-yyyy";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateIT = @"dd-MM-yyyy";
|
||||||
static NSString *kNSDateHelperFormatSQLDateIT_shashSeparated = @"dd/MM/yyyy";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateIT_shashSeparated = @"dd/MM/yyyy";
|
||||||
|
|
||||||
static NSString *kNSDateHelperFormatSQLDateEN = @"MM-dd-yyyy";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateEN = @"MM-dd-yyyy";
|
||||||
static NSString *kNSDateHelperFormatSQLDateEN_shashSeparated = @"MM/dd/yyyy";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateEN_shashSeparated = @"MM/dd/yyyy";
|
||||||
|
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTime = @"yyyy-MM-dd HH:mm:ss";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTime = @"yyyy-MM-dd HH:mm:ss";
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTime_shashSeparated = @"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 * _Nonnull kNSDateHelperFormatSQLDateWithTimeIT = @"dd-MM-yyyy HH:mm:ss";
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeIT_shashSeparated = @"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 * _Nonnull kNSDateHelperFormatSQLDateWithTimeEN = @"MM-dd-yyyy HH:mm:ss";
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeEN_shashSeparated = @"MM/dd/yyyy HH:mm:ss";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeEN_shashSeparated = @"MM/dd/yyyy HH:mm:ss";
|
||||||
|
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeZone = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZone = @"yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeZone_shashSeparated = @"yyyy/MM/dd'T'HH:mm:ss'Z'";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZone_shashSeparated = @"yyyy/MM/dd'T'HH:mm:ss'Z'";
|
||||||
|
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeZoneEN = @"MM-dd-yyyy'T'HH:mm:ss'Z'";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZoneEN = @"MM-dd-yyyy'T'HH:mm:ss'Z'";
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeZoneEN_shashSeparated = @"MM/dd/yyyy'T'HH:mm:ss'Z'";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZoneEN_shashSeparated = @"MM/dd/yyyy'T'HH:mm:ss'Z'";
|
||||||
|
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeZoneIT = @"dd-MM-yyyy'T'HH:mm:ss'Z'";
|
static NSString * _Nonnull kNSDateHelperFormatSQLDateWithTimeZoneIT = @"dd-MM-yyyy'T'HH:mm:ss'Z'";
|
||||||
static NSString *kNSDateHelperFormatSQLDateWithTimeZoneIT_shashSeparated = @"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)
|
@interface NSDate (NSDate_Util)
|
||||||
|
|
||||||
+ (void)initializeStatics;
|
+ (void)initializeStatics;
|
||||||
|
|
||||||
+ (NSCalendar *) sharedCalendar;
|
+ (NSCalendar * _Nonnull) sharedCalendar;
|
||||||
+ (NSDateFormatter *) sharedDateFormatter;
|
+ (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;
|
+ (NSString * _Nonnull) stringFromDate:(NSDate * _Nonnull) date withFormat:(NSString * _Nonnull) string;
|
||||||
- (NSDate *) dateByAddingHours:(NSInteger) hours;
|
+ (NSString * _Nonnull) stringFromDate:(NSDate * _Nonnull) date;
|
||||||
- (NSDate*) dateByAddingDays:(NSInteger) days;
|
+ (NSString * _Nonnull) stringForDisplayFromDate:(NSDate * _Nonnull) date;
|
||||||
- (NSDate*) dateByAddingYears:(NSInteger) years;
|
+ (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;
|
+ (NSString * _Nonnull) dateFormatString;
|
||||||
- (NSDate *) dateBySubtractingHours:(NSInteger) hours;
|
+ (NSString * _Nonnull) timeFormatString;
|
||||||
- (NSDate *) dateBySubtractingDays:(NSInteger) days;
|
+ (NSString * _Nonnull) timestampFormatString;
|
||||||
- (NSDate *) dateBySubtractingYears:(NSInteger) years;
|
+ (NSString * _Nonnull) dbFormatString;
|
||||||
|
|
||||||
- (NSDate *) dateAtStartOfDay;
|
+ (NSString * _Nonnull) parseDateFormatFromString:(NSString * _Nonnull) dateString;
|
||||||
- (NSDate *) dateAtEndOfDay;
|
|
||||||
|
|
||||||
- (NSInteger) minutesAfterDate:(NSDate *) aDate;
|
+ (NSString * _Nonnull) WCFStringFromDate:(NSDate * _Nonnull) date;
|
||||||
- (CGFloat) distanceInWeeksToDate:(NSDate *) anotherDate;
|
|
||||||
- (CGFloat) distanceInDaysToDate:(NSDate *) anotherDate;
|
|
||||||
- (CGFloat) distanceInHoursToDate:(NSDate *) anotherDate;
|
|
||||||
- (CGFloat) distanceInMinutesToDate:(NSDate *) anotherDate;
|
|
||||||
- (CGFloat) distanceInSeconsToDate:(NSDate *) anotherDate;
|
|
||||||
|
|
||||||
- (NSDate *) toLocalTime;
|
+ (NSArray * _Nullable) getSlotTimesFromDate:(NSDate * _Nonnull) date distanceMinutes:(NSInteger) minutes;
|
||||||
- (NSDate *) toGlobalTime;
|
|
||||||
|
|
||||||
- (BOOL) isSameYearAsDate:(NSDate *) aDate;
|
+ (NSString * _Nonnull) getUniversalHourFromDateString:(NSString * _Nonnull) string formatterString:(NSString * _Nonnull) formatterString andUppercaseString:(BOOL) uppercaseString;
|
||||||
- (BOOL) isEarlierThanDate:(NSDate *) aDate;
|
|
||||||
- (BOOL) isLaterThanDate:(NSDate *) aDate;
|
- (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) daysAgo;
|
||||||
- (NSUInteger) daysAgoAgainstMidnight;
|
- (NSUInteger) daysAgoAgainstMidnight;
|
||||||
- (NSString *) stringDaysAgo;
|
- (NSString * _Nonnull) stringDaysAgo;
|
||||||
- (NSString *) stringDaysAgoAgainstMidnight:(BOOL)flag;
|
- (NSString * _Nonnull) stringDaysAgoAgainstMidnight:(BOOL)flag;
|
||||||
- (NSUInteger) monthDay;
|
- (NSUInteger) monthDay;
|
||||||
- (NSUInteger) weekday;
|
- (NSUInteger) weekday;
|
||||||
- (NSUInteger) month;
|
- (NSUInteger) month;
|
||||||
@ -99,37 +123,19 @@ static NSString *kNSDateHelperFormatSQLDateWithTimeZoneIT_shashSeparated = @"dd/
|
|||||||
- (NSUInteger) minute;
|
- (NSUInteger) minute;
|
||||||
- (NSUInteger) year;
|
- (NSUInteger) year;
|
||||||
- (long int) utcTimeStamp; //full seconds since
|
- (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;
|
- (NSString * _Nonnull) getUniversalHourUppercaseString:(BOOL) uppercaseString;
|
||||||
|
- (NSString * _Nonnull) getNotificationUniversalHourUppercaseString:(BOOL) uppercaseString;
|
||||||
- (BOOL)isSameDayAsDate:(NSDate*)otherDate;
|
|
||||||
|
|
||||||
+ (NSString *) getUniversalHourFromDateString:(NSString *) string formatterString:(NSString*) formatterString andUppercaseString:(BOOL) uppercaseString;
|
|
||||||
- (NSString *) getUniversalHourUppercaseString:(BOOL) uppercaseString;
|
|
||||||
- (NSString *) getNotificationUniversalHourUppercaseString:(BOOL) uppercaseString;
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@ -12,9 +12,6 @@
|
|||||||
#define D_MINUTE 60
|
#define D_MINUTE 60
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@implementation NSDate (NSDate_Util)
|
@implementation NSDate (NSDate_Util)
|
||||||
|
|
||||||
static NSCalendar *_calendar = nil;
|
static NSCalendar *_calendar = nil;
|
||||||
@ -25,11 +22,7 @@ static NSDateFormatter *_displayFormatter = nil;
|
|||||||
dispatch_once(&onceToken, ^{
|
dispatch_once(&onceToken, ^{
|
||||||
@autoreleasepool {
|
@autoreleasepool {
|
||||||
if (_calendar == nil) {
|
if (_calendar == nil) {
|
||||||
#if __has_feature(objc_arc)
|
|
||||||
_calendar = [NSCalendar currentCalendar];
|
_calendar = [NSCalendar currentCalendar];
|
||||||
#else
|
|
||||||
_calendar = [[NSCalendar currentCalendar] retain];
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
if (_displayFormatter == nil) {
|
if (_displayFormatter == nil) {
|
||||||
_displayFormatter = [[NSDateFormatter alloc] init];
|
_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 {
|
+ (NSCalendar *)sharedCalendar {
|
||||||
[self initializeStatics];
|
[self initializeStatics];
|
||||||
return _calendar;
|
return _calendar;
|
||||||
@ -210,91 +41,6 @@ static NSDateFormatter *_displayFormatter = nil;
|
|||||||
return _displayFormatter;
|
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 {
|
+ (NSDate *)dateFromString:(NSString *)string {
|
||||||
return [NSDate dateFromString:string withFormat:[NSDate dbFormatString]];
|
return [NSDate dateFromString:string withFormat:[NSDate dbFormatString]];
|
||||||
}
|
}
|
||||||
@ -316,6 +62,13 @@ static NSDateFormatter *_displayFormatter = nil;
|
|||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+ (NSDate *) dateFromISO8601String:(NSString*) string {
|
||||||
|
|
||||||
|
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
|
||||||
|
|
||||||
|
return [formatter dateFromString:string];
|
||||||
|
}
|
||||||
|
|
||||||
+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format {
|
+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format {
|
||||||
return [date stringWithFormat:format];
|
return [date stringWithFormat:format];
|
||||||
}
|
}
|
||||||
@ -360,9 +113,7 @@ static NSDateFormatter *_displayFormatter = nil;
|
|||||||
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
|
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
|
||||||
[componentsToSubtract setDay:-7];
|
[componentsToSubtract setDay:-7];
|
||||||
NSDate *lastweek = [[self sharedCalendar] dateByAddingComponents:componentsToSubtract toDate:today options:0];
|
NSDate *lastweek = [[self sharedCalendar] dateByAddingComponents:componentsToSubtract toDate:today options:0];
|
||||||
#if !__has_feature(objc_arc)
|
|
||||||
[componentsToSubtract release];
|
|
||||||
#endif
|
|
||||||
NSComparisonResult lastweek_result = [date compare:lastweek];
|
NSComparisonResult lastweek_result = [date compare:lastweek];
|
||||||
if (lastweek_result == NSOrderedDescending) {
|
if (lastweek_result == NSOrderedDescending) {
|
||||||
if (displayTime) {
|
if (displayTime) {
|
||||||
@ -411,90 +162,6 @@ static NSDateFormatter *_displayFormatter = nil;
|
|||||||
return [self stringForDisplayFromDate:date prefixed:NO];
|
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 {
|
+ (NSString *)dateFormatString {
|
||||||
return kNSDateHelperFormatSQLDate;
|
return kNSDateHelperFormatSQLDate;
|
||||||
}
|
}
|
||||||
@ -572,20 +239,6 @@ static NSDateFormatter *_displayFormatter = nil;
|
|||||||
return slotTimes;
|
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 *)getUniversalHourFromDateString:(NSString *)string formatterString:(NSString*) formatterString andUppercaseString:(BOOL)uppercaseString {
|
||||||
|
|
||||||
NSString * dateString = [NSString stringWithString:string];
|
NSString * dateString = [NSString stringWithString:string];
|
||||||
@ -862,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 {
|
- (NSString *)getUniversalHourUppercaseString:(BOOL)uppercaseString {
|
||||||
|
|
||||||
NSDate *now = [NSDate date];
|
NSDate *now = [NSDate date];
|
||||||
@ -1204,4 +1195,4 @@ static NSDateFormatter *_displayFormatter = nil;
|
|||||||
}
|
}
|
||||||
return @"";
|
return @"";
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user