source: trunk/Cocoa/Pester/Source/NSCalendarDate-OFExtensions.m@ 184

Last change on this file since 184 was 102, checked in by Nicholas Riley, 22 years ago

Pester 1.1b3

File size: 3.8 KB
Line 
1// Copyright 1997-2002 Omni Development, Inc. All rights reserved.
2//
3// This software may only be used and reproduced according to the
4// terms in the file OmniSourceLicense.html, which should be
5// distributed with this project and can also be found at
6// http://www.omnigroup.com/DeveloperResources/OmniSourceLicense.html.
7
8#import "NSCalendarDate-OFExtensions.h"
9
10#import <Foundation/Foundation.h>
11
12// RCS_ID("$Header: /Network/Source/CVS/OmniGroup/Frameworks/OmniFoundation/OpenStepExtensions.subproj/NSCalendarDate-OFExtensions.m,v 1.15 2002/03/09 01:54:02 kc Exp $")
13
14@implementation NSCalendarDate (OFExtensions)
15
16+ (NSCalendarDate *)unixReferenceDate;
17{
18 static NSCalendarDate *unixReferenceDate = nil;
19 const long zero = 0;
20
21 if (unixReferenceDate == nil) {
22 unixReferenceDate = [[NSCalendarDate dateWithString:[NSString stringWithCString:ctime(&zero)] calendarFormat:@"%a %b %d %H:%M:%S %Y\n"] retain];
23 }
24 return unixReferenceDate;
25}
26
27- (void)setToUnixDateFormat;
28{
29 if ([self yearOfCommonEra] == [(NSCalendarDate *)[NSCalendarDate date] yearOfCommonEra])
30 [self setCalendarFormat:@"%b %d %H:%M"];
31 else
32 [self setCalendarFormat:@"%b %d %Y"];
33}
34
35- initWithTime_t:(int)time;
36{
37 NSCalendarDate *date;
38
39 date = (id)[self initWithTimeInterval:time sinceDate:[isa unixReferenceDate]];
40 [date setToUnixDateFormat];
41 return date;
42}
43
44// We're going with Noon instead of midnight, since it's a bit more tolerant of
45// time zone switching. (When you're adding days.)
46
47- (NSCalendarDate *)safeReferenceDate;
48{
49 int year, month, day;
50
51 year = [self yearOfCommonEra];
52 month = [self monthOfYear];
53 day = [self dayOfMonth];
54
55 return [NSCalendarDate dateWithYear:year month:month day:day
56 hour:12 minute:0 second:0 timeZone:[NSTimeZone localTimeZone]];
57}
58
59- (NSCalendarDate *)firstDayOfMonth;
60{
61 NSCalendarDate *firstDay;
62
63 firstDay = [[NSCalendarDate alloc] initWithYear:[self yearOfCommonEra]
64 month:[self monthOfYear]
65 day:1
66 hour:12
67 minute:0
68 second:0
69 timeZone:nil];
70 return [firstDay autorelease];
71}
72
73- (NSCalendarDate *)lastDayOfMonth;
74{
75 return [[self firstDayOfMonth] dateByAddingYears:0 months:1 days:-1 hours:0 minutes:0 seconds:0];
76}
77
78- (int)numberOfDaysInMonth;
79{
80 return [[self lastDayOfMonth] dayOfMonth];
81}
82
83- (int)weekOfMonth;
84{
85 // Returns 1 through 6. Weeks are Sunday-Saturday.
86 int dayOfMonth;
87 int firstWeekDayOfMonth;
88
89 dayOfMonth = [self dayOfMonth];
90 firstWeekDayOfMonth = [[self firstDayOfMonth] dayOfWeek];
91 return (dayOfMonth - 1 + firstWeekDayOfMonth) / 7 + 1;
92}
93
94- (BOOL)isInSameWeekAsDate:(NSCalendarDate *)otherDate;
95{
96 int weekOfMonth;
97
98 // First, do a quick check to filter out dates which are more than a week away.
99 if (abs([self dayOfCommonEra] - [otherDate dayOfCommonEra]) > 6)
100 return NO;
101
102 // Then, handle the simple case, when both dates are the same year and month.
103 if ([self yearOfCommonEra] == [otherDate yearOfCommonEra] && [self monthOfYear] == [otherDate monthOfYear])
104 return ([self weekOfMonth] == [otherDate weekOfMonth]);
105
106 // Now we know the other date is within a week of us, and not in the same month.
107 weekOfMonth = [self weekOfMonth];
108 if (weekOfMonth == 1) {
109 // We are in the first week of the month. The otherDate is in the same week if its weekday is earlier than ours.
110 return ([otherDate dayOfWeek] < [self dayOfWeek]);
111 } else if (weekOfMonth == [[self lastDayOfMonth] weekOfMonth]) {
112 // We are in the last week of the month. The otherDate is in the same week if its weekday is later than ours.
113 return ([otherDate dayOfWeek] > [self dayOfWeek]);
114 } else {
115 // We are somewhere in the middle of the month, so the otherDate cannot be in the same week.
116 return NO;
117 }
118}
119
120@end
Note: See TracBrowser for help on using the repository browser.