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

Last change on this file since 612 was 603, checked in by Nicholas Riley, 15 years ago

Remove [NSCalendarDate initWithTime_t:]: it's not used anywhere and was generating a compiler warning.

File size: 3.6 KB
RevLine 
[102]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// We're going with Noon instead of midnight, since it's a bit more tolerant of
36// time zone switching. (When you're adding days.)
37
38- (NSCalendarDate *)safeReferenceDate;
39{
40 int year, month, day;
41
42 year = [self yearOfCommonEra];
43 month = [self monthOfYear];
44 day = [self dayOfMonth];
45
46 return [NSCalendarDate dateWithYear:year month:month day:day
47 hour:12 minute:0 second:0 timeZone:[NSTimeZone localTimeZone]];
48}
49
50- (NSCalendarDate *)firstDayOfMonth;
51{
52 NSCalendarDate *firstDay;
53
54 firstDay = [[NSCalendarDate alloc] initWithYear:[self yearOfCommonEra]
55 month:[self monthOfYear]
56 day:1
57 hour:12
58 minute:0
59 second:0
60 timeZone:nil];
61 return [firstDay autorelease];
62}
63
64- (NSCalendarDate *)lastDayOfMonth;
65{
66 return [[self firstDayOfMonth] dateByAddingYears:0 months:1 days:-1 hours:0 minutes:0 seconds:0];
67}
68
69- (int)numberOfDaysInMonth;
70{
71 return [[self lastDayOfMonth] dayOfMonth];
72}
73
74- (int)weekOfMonth;
75{
76 // Returns 1 through 6. Weeks are Sunday-Saturday.
77 int dayOfMonth;
78 int firstWeekDayOfMonth;
79
80 dayOfMonth = [self dayOfMonth];
81 firstWeekDayOfMonth = [[self firstDayOfMonth] dayOfWeek];
82 return (dayOfMonth - 1 + firstWeekDayOfMonth) / 7 + 1;
83}
84
85- (BOOL)isInSameWeekAsDate:(NSCalendarDate *)otherDate;
86{
87 int weekOfMonth;
88
89 // First, do a quick check to filter out dates which are more than a week away.
90 if (abs([self dayOfCommonEra] - [otherDate dayOfCommonEra]) > 6)
91 return NO;
92
93 // Then, handle the simple case, when both dates are the same year and month.
94 if ([self yearOfCommonEra] == [otherDate yearOfCommonEra] && [self monthOfYear] == [otherDate monthOfYear])
95 return ([self weekOfMonth] == [otherDate weekOfMonth]);
96
97 // Now we know the other date is within a week of us, and not in the same month.
98 weekOfMonth = [self weekOfMonth];
99 if (weekOfMonth == 1) {
100 // We are in the first week of the month. The otherDate is in the same week if its weekday is earlier than ours.
101 return ([otherDate dayOfWeek] < [self dayOfWeek]);
102 } else if (weekOfMonth == [[self lastDayOfMonth] weekOfMonth]) {
103 // We are in the last week of the month. The otherDate is in the same week if its weekday is later than ours.
104 return ([otherDate dayOfWeek] > [self dayOfWeek]);
105 } else {
106 // We are somewhere in the middle of the month, so the otherDate cannot be in the same week.
107 return NO;
108 }
109}
110
111@end
Note: See TracBrowser for help on using the repository browser.