-
Notifications
You must be signed in to change notification settings - Fork 2
/
CGFDateModifier.m
94 lines (80 loc) · 2.8 KB
/
CGFDateModifier.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// CGFDateModifier.m
//
// Created by Björn Kaiser on 17.07.13.
// Copyright (c) 2013 Björn Kaiser. All rights reserved.
//
#import "CGFDateModifier.h"
@implementation CGFDateModifier
+ (NSDate*) dateByModifiyingDate:(NSDate*)date withModifier:(NSString*)modifier
{
BOOL substract = NO;
__block int numOfKind = 0;
CGFHumanDateModifierType type;
NSDateComponents *components = [[NSDateComponents alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// What should we do (add vs. sub)?
if([modifier hasPrefix:@"-"]) substract = YES;
// How much?
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]{1,}" options:0 error:&error];
if (error == NULL) {
[regex enumerateMatchesInString:modifier options:0 range:NSMakeRange(0, [modifier length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSString *match = [modifier substringWithRange:[result range]];
if(substract) numOfKind -= [match intValue];
else numOfKind += [match intValue];
}];
}
// What kind? (days, months, years)
if ([modifier rangeOfString:@"day"].location != NSNotFound)
{
type = CGFHumanDateModifierTypeDays;
}
else if([modifier rangeOfString:@"week"].location != NSNotFound)
{
type = CGFHumanDateModifierTypeWeeks;
}
else if([modifier rangeOfString:@"month"].location != NSNotFound)
{
type = CGFHumanDateModifierTypeMonths;
}
else if([modifier rangeOfString:@"year"].location != NSNotFound)
{
type = CGFHumanDateModifierTypeYears;
}
else
{
// We default to adding 0 days
type = CGFHumanDateModifierTypeDays;
}
switch (type) {
case CGFHumanDateModifierTypeDays:
[components setDay:numOfKind];
break;
case CGFHumanDateModifierTypeWeeks:
[components setWeekOfYear:numOfKind];
break;
case CGFHumanDateModifierTypeMonths:
[components setMonth:numOfKind];
break;
case CGFHumanDateModifierTypeYears:
[components setYear:numOfKind];
break;
default:
[components setDay:0];
break;
}
return [gregorian dateByAddingComponents:components toDate:date options:0];
}
+ (NSDate*) dateByModifiyingDate:(NSDate*)date withModifiers:(NSArray*)modifiers
{
NSDate *resultDate = date;
if(modifiers != nil && modifiers.count > 0)
{
for (NSString *modifier in modifiers) {
resultDate = [CGFDateModifier dateByModifiyingDate:resultDate withModifier:modifier];
}
}
return resultDate;
}
@end