| 1 |
// |
| 2 |
// XspfMRule_NSExpressionFunctions.m |
| 3 |
// XspfManager |
| 4 |
// |
| 5 |
// Created by Hori,Masaki on 09/12/20. |
| 6 |
// |
| 7 |
|
| 8 |
/* |
| 9 |
This source code is release under the New BSD License. |
| 10 |
Copyright (c) 2009-2010, masakih |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に |
| 14 |
限り、再頒布および使用が許可されます。 |
| 15 |
|
| 16 |
1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含 |
| 17 |
めること。 |
| 18 |
2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表 |
| 19 |
示、本条件一覧、および下記免責条項を含めること。 |
| 20 |
3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、 |
| 21 |
コントリビューターの名前を使用してはならない。 |
| 22 |
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、 |
| 23 |
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証 |
| 24 |
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター |
| 25 |
も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか |
| 26 |
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する |
| 27 |
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用 |
| 28 |
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定 |
| 29 |
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい |
| 30 |
て、一切責任を負わないものとします。 |
| 31 |
------------------------------------------------------------------- |
| 32 |
Copyright (c) 2009-2010, masakih |
| 33 |
All rights reserved. |
| 34 |
|
| 35 |
Redistribution and use in source and binary forms, with or without |
| 36 |
modification, are permitted provided that the following conditions |
| 37 |
are met: |
| 38 |
|
| 39 |
1, Redistributions of source code must retain the above copyright |
| 40 |
notice, this list of conditions and the following disclaimer. |
| 41 |
2, Redistributions in binary form must reproduce the above copyright |
| 42 |
notice, this list of conditions and the following disclaimer in |
| 43 |
the documentation and/or other materials provided with the |
| 44 |
distribution. |
| 45 |
3, The names of its contributors may be used to endorse or promote |
| 46 |
products derived from this software without specific prior |
| 47 |
written permission. |
| 48 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 49 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 50 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 51 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 52 |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 53 |
INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 54 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 55 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 56 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 58 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 59 |
POSSIBILITY OF SUCH DAMAGE. |
| 60 |
*/ |
| 61 |
|
| 62 |
|
| 63 |
#import "XspfMRule.h" |
| 64 |
|
| 65 |
@implementation XspfMRule (XspfMNSExpressionFunctions) |
| 66 |
|
| 67 |
|
| 68 |
- (NSString *)notContainsRegularExpression:(NSArray *)displayValues |
| 69 |
{ |
| 70 |
NSString *reg = [NSString stringWithFormat:@"(?:(?!.*%@).)*", [[displayValues objectAtIndex:2] stringValue]]; |
| 71 |
id result = [NSExpression expressionForConstantValue:reg]; |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
- (NSArray *)rangeOfToday |
| 76 |
{ |
| 77 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 78 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 79 |
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
| 80 |
fromDate:now]; |
| 81 |
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 82 |
|
| 83 |
id result = [NSArray arrayWithObjects:startOfToday, now, nil]; |
| 84 |
return result; |
| 85 |
} |
| 86 |
- (NSArray *)rangeOfYesterday |
| 87 |
{ |
| 88 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 89 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 90 |
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
| 91 |
fromDate:now]; |
| 92 |
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 93 |
|
| 94 |
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease]; |
| 95 |
[comp setDay:-1]; |
| 96 |
NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 97 |
|
| 98 |
id result = [NSArray arrayWithObjects:startOfYesterday, startOfToday, nil]; |
| 99 |
return result; |
| 100 |
} |
| 101 |
- (NSArray *)rangeOfThisWeek |
| 102 |
{ |
| 103 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 104 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 105 |
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 106 |
fromDate:now]; |
| 107 |
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 108 |
|
| 109 |
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease]; |
| 110 |
|
| 111 |
[comp setWeekday:-[nowComp weekday]+1]; |
| 112 |
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 113 |
|
| 114 |
id result = [NSArray arrayWithObjects:startOfThisWeek, now, nil]; |
| 115 |
return result; |
| 116 |
} |
| 117 |
- (NSArray *)rangeOfLastWeek |
| 118 |
{ |
| 119 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 120 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 121 |
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 122 |
fromDate:now]; |
| 123 |
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 124 |
|
| 125 |
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease]; |
| 126 |
[comp setWeekday:-[nowComp weekday]+1]; |
| 127 |
NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 128 |
|
| 129 |
[comp setWeekday:-[nowComp weekday]+1]; |
| 130 |
[comp setWeek:-1]; |
| 131 |
NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 132 |
|
| 133 |
id result = [NSArray arrayWithObjects:startOfLastWeek, startOfThisWeek, nil]; |
| 134 |
return result; |
| 135 |
} |
| 136 |
|
| 137 |
- (NSArray *)dateRangeByNumber:(NSNumber *)numberValue unit:(NSNumber *)unitValue |
| 138 |
{ |
| 139 |
NSInteger number = [numberValue integerValue]; |
| 140 |
NSInteger unit = [unitValue integerValue]; |
| 141 |
|
| 142 |
NSDateComponents *comp01 = [[[NSDateComponents alloc] init] autorelease]; |
| 143 |
NSDateComponents *comp02 = [[[NSDateComponents alloc] init] autorelease]; |
| 144 |
NSUInteger unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | kCFCalendarUnitSecond; |
| 145 |
switch(unit) { |
| 146 |
case XspfMHoursUnitType: |
| 147 |
[comp01 setHour:-number]; |
| 148 |
[comp02 setHour:-number+1]; |
| 149 |
break; |
| 150 |
case XspfMDaysUnitType: |
| 151 |
[comp01 setDay:-number]; |
| 152 |
[comp02 setDay:-number+1]; |
| 153 |
break; |
| 154 |
case XpsfMWeeksUnitType: |
| 155 |
[comp01 setWeek:-number]; |
| 156 |
[comp02 setDay:-1]; |
| 157 |
break; |
| 158 |
case XspfMMonthsUnitType: |
| 159 |
[comp01 setMonth:-number]; |
| 160 |
[comp02 setDay:-1]; |
| 161 |
break; |
| 162 |
case XspfMYearsUnitType: |
| 163 |
[comp01 setYear:-number]; |
| 164 |
[comp02 setDay:-1]; |
| 165 |
break; |
| 166 |
default: |
| 167 |
[NSException raise:@"XspfMRuleUnknownUnitType" format:@"unknown unit type."]; |
| 168 |
break; |
| 169 |
} |
| 170 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 171 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 172 |
NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now]; |
| 173 |
NSDate *aDay = [aCalendar dateFromComponents:nowComp]; |
| 174 |
|
| 175 |
NSDate *pastDay01 = [aCalendar dateByAddingComponents:comp01 toDate:aDay options:0]; |
| 176 |
NSDate *pastDay02 = [aCalendar dateByAddingComponents:comp02 toDate:aDay options:0]; |
| 177 |
|
| 178 |
id result = [NSArray arrayWithObjects:pastDay01, pastDay02, nil]; |
| 179 |
return result; |
| 180 |
} |
| 181 |
- (NSDate *)dateByNumber:(NSNumber *)numberValue unit:(NSNumber *)unitValue |
| 182 |
{ |
| 183 |
NSInteger number = [numberValue integerValue]; |
| 184 |
NSInteger unit = [unitValue integerValue]; |
| 185 |
|
| 186 |
NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease]; |
| 187 |
NSUInteger unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | kCFCalendarUnitSecond; |
| 188 |
switch(unit) { |
| 189 |
case XspfMHoursUnitType: |
| 190 |
[comp setHour:-number]; |
| 191 |
break; |
| 192 |
case XspfMDaysUnitType: |
| 193 |
[comp setDay:-number]; |
| 194 |
break; |
| 195 |
case XpsfMWeeksUnitType: |
| 196 |
[comp setWeek:-number]; |
| 197 |
break; |
| 198 |
case XspfMMonthsUnitType: |
| 199 |
[comp setMonth:-number]; |
| 200 |
break; |
| 201 |
case XspfMYearsUnitType: |
| 202 |
[comp setYear:-number]; |
| 203 |
break; |
| 204 |
default: |
| 205 |
[NSException raise:@"XspfMRuleUnknownUnitType" format:@"unknown unit type."]; |
| 206 |
break; |
| 207 |
} |
| 208 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 209 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 210 |
NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now]; |
| 211 |
NSDate *aDay = [aCalendar dateFromComponents:nowComp]; |
| 212 |
|
| 213 |
NSDate *pastDay = [aCalendar dateByAddingComponents:comp toDate:aDay options:0]; |
| 214 |
|
| 215 |
return pastDay; |
| 216 |
} |
| 217 |
- (NSArray *)rangeDateByNumber:(NSNumber *)numberValue toNumber:(NSNumber *)number02Value unit:(NSNumber *)unitValue |
| 218 |
{ |
| 219 |
NSInteger number = [numberValue integerValue]; |
| 220 |
NSInteger number02 = [number02Value integerValue]; |
| 221 |
NSInteger unit = [unitValue integerValue]; |
| 222 |
|
| 223 |
NSDateComponents *comp01 = [[[NSDateComponents alloc] init] autorelease]; |
| 224 |
NSDateComponents *comp02 = [[[NSDateComponents alloc] init] autorelease]; |
| 225 |
NSUInteger unitFlag = 0;//NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | kCFCalendarUnitSecond; |
| 226 |
switch(unit) { |
| 227 |
case XspfMHoursUnitType: |
| 228 |
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit; |
| 229 |
[comp01 setHour:-number]; |
| 230 |
[comp02 setHour:-number02]; |
| 231 |
break; |
| 232 |
case XspfMDaysUnitType: |
| 233 |
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; |
| 234 |
[comp01 setDay:-number]; |
| 235 |
[comp02 setDay:-number02]; |
| 236 |
break; |
| 237 |
case XpsfMWeeksUnitType: |
| 238 |
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit; |
| 239 |
[comp01 setWeek:-number+1]; |
| 240 |
[comp01 setDay:-1]; |
| 241 |
[comp02 setWeek:-number02]; |
| 242 |
break; |
| 243 |
case XspfMMonthsUnitType: |
| 244 |
unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit; |
| 245 |
[comp01 setMonth:-number+1]; |
| 246 |
[comp01 setDay:-1]; |
| 247 |
[comp02 setMonth:-number02]; |
| 248 |
break; |
| 249 |
case XspfMYearsUnitType: |
| 250 |
unitFlag = NSYearCalendarUnit; |
| 251 |
[comp01 setYear:-number+1]; |
| 252 |
[comp01 setDay:-1]; |
| 253 |
[comp02 setYear:-number02]; |
| 254 |
break; |
| 255 |
default: |
| 256 |
[NSException raise:@"XspfMRuleUnknownUnitType" format:@"unknown unit type."]; |
| 257 |
break; |
| 258 |
} |
| 259 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 260 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 261 |
NSDateComponents *nowComp = [aCalendar components:unitFlag fromDate:now]; |
| 262 |
NSDate *aDay = [aCalendar dateFromComponents:nowComp]; |
| 263 |
|
| 264 |
NSDate *pastDay01 = [aCalendar dateByAddingComponents:comp01 toDate:aDay options:0]; |
| 265 |
NSDate *pastDay02 = [aCalendar dateByAddingComponents:comp02 toDate:aDay options:0]; |
| 266 |
|
| 267 |
id result = [NSArray arrayWithObjects:pastDay02, pastDay01, nil]; |
| 268 |
return result; |
| 269 |
} |
| 270 |
- (NSArray *)dateRangeFromVariable:(NSString *)date |
| 271 |
{ |
| 272 |
// HMLog(HMLogLevelDebug, @"In function argument is %@", date); |
| 273 |
|
| 274 |
NSCalendar *aCalendar = [NSCalendar currentCalendar]; |
| 275 |
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0]; |
| 276 |
NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
| 277 |
fromDate:now]; |
| 278 |
NSDate *startOfToday = [aCalendar dateFromComponents:nowComp]; |
| 279 |
|
| 280 |
// NSDateComponents *comp = [[[NSDateComponents alloc] init] autorelease]; |
| 281 |
// [comp setDay:-1]; |
| 282 |
// NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 283 |
|
| 284 |
// [comp setDay:0]; |
| 285 |
// [comp setWeekday:-[nowComp weekday]+1]; |
| 286 |
// NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 287 |
|
| 288 |
// [comp setWeekday:-[nowComp weekday]+1]; |
| 289 |
// [comp setWeek:-1]; |
| 290 |
// NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0]; |
| 291 |
|
| 292 |
// HMLog(HMLogLevelDebug, @"now -> %@\ntoday -> %@\nyesterday -> %@\nthisweek -> %@\nlastweek -> %@", |
| 293 |
// now, startOfToday, startOfYesterday, startOfThisWeek, startOfLastWeek); |
| 294 |
|
| 295 |
id result = [NSArray arrayWithObjects:now, startOfToday, nil]; |
| 296 |
return result; |
| 297 |
} |
| 298 |
|
| 299 |
@end |