|
Wired Foundation
2.0
A foundation framework for the Wired implementation on Mac OS X
|
00001 /* $Id$ */ 00002 00003 /* 00004 * Copyright (c) 2003-2009 Axel Andersson 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00017 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00020 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00023 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00024 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00025 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 * POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 #import <WiredFoundation/NSDate-WIFoundation.h> 00030 00031 @implementation NSDate(WIFoundation) 00032 00033 + (NSDate *)dateAtStartOfCurrentDay { 00034 return [[NSDate date] dateAtStartOfDay]; 00035 } 00036 00037 00038 00039 + (NSDate *)dateAtStartOfCurrentWeek { 00040 return [[NSDate date] dateAtStartOfWeek]; 00041 } 00042 00043 00044 00045 + (NSDate *)dateAtStartOfCurrentMonth { 00046 return [[NSDate date] dateAtStartOfMonth]; 00047 } 00048 00049 00050 00051 + (NSDate *)dateAtStartOfCurrentYear { 00052 return [[NSDate date] dateAtStartOfYear]; 00053 } 00054 00055 00056 00057 #pragma mark - 00058 00059 - (NSDate *)dateAtStartOfDay { 00060 NSDateComponents *components; 00061 00062 components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:self]; 00063 00064 [components setHour:-[components hour]]; 00065 [components setMinute:-[components minute]]; 00066 [components setSecond:-[components second]]; 00067 00068 return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self options:0]; 00069 } 00070 00071 00072 00073 - (NSDate *)dateAtStartOfWeek { 00074 NSDate *date; 00075 NSDateComponents *components; 00076 NSInteger firstWeekday; 00077 00078 date = [self dateAtStartOfDay]; 00079 firstWeekday = [[NSCalendar currentCalendar] firstWeekday]; 00080 components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date]; 00081 00082 if([components weekday] < firstWeekday) 00083 [components setWeekday:-[components weekday] + firstWeekday - 7]; 00084 else 00085 [components setWeekday:-[components weekday] + firstWeekday]; 00086 00087 return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:date options:0]; 00088 } 00089 00090 00091 00092 - (NSDate *)dateAtStartOfMonth { 00093 NSDate *date; 00094 NSDateComponents *components; 00095 00096 date = [self dateAtStartOfDay]; 00097 components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:date]; 00098 00099 [components setDay:-[components day] + 1]; 00100 00101 return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:date options:0]; 00102 } 00103 00104 00105 00106 - (NSDate *)dateAtStartOfYear { 00107 NSDate *date; 00108 NSDateComponents *components; 00109 00110 date = [self dateAtStartOfMonth]; 00111 components = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit fromDate:date]; 00112 00113 [components setMonth:-[components month] + 1]; 00114 00115 return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:date options:0]; 00116 } 00117 00118 00119 00120 #pragma mark - 00121 00122 - (BOOL)isAtBeginningOfAnyEpoch { 00123 return ([self isEqualToDate:[NSDate dateWithTimeIntervalSince1970:0.0]] || 00124 [self isEqualToDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0.0]]); 00125 } 00126 00127 00128 00129 - (NSDate *)dateByAddingDays:(NSInteger)days { 00130 NSDateComponents *components; 00131 00132 components = [[[NSDateComponents alloc] init] autorelease]; 00133 [components setDay:days]; 00134 00135 return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self options:0]; 00136 } 00137 00138 @end