File: DateRange.m

package info (click to toggle)
agenda.app 0.47-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,008 kB
  • sloc: objc: 8,103; makefile: 16; sh: 5
file content (101 lines) | stat: -rw-r--r-- 2,218 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
#import <Foundation/Foundation.h>
#import "DateRange.h"

@implementation DateRange
- (id)initWithStart:(Date *)date duration:(NSTimeInterval)seconds
{
  self = [super init];
  if (self) {
    _length = seconds;
    _start = [date retain];
  }
  return self;
}

- (id)initWithDay:(Date *)day
{
  NSAssert([day isDate], @"This method expects a date, not a datetime");
  return [self initWithStart:day duration:86400];
}

- (void)dealloc
{
  [_start release];
  [super dealloc];
}

- (Date *)start
{
  return _start;
}

- (void)setStart:(Date *)start
{
  ASSIGN(_start, start);
}

- (NSTimeInterval)length
{
  return _length;
}

- (void)setLength:(NSTimeInterval)length
{
  _length = length;
}

- (BOOL)contains:(Date *)date
{
  if ([_start compare:date withTime:YES] > 0)
    return NO;
  if ([date timeIntervalSinceDate:_start] > _length)
    return NO;
  return YES;
}

- (BOOL)intersectsWith:(DateRange *)range
{
  NSTimeInterval s1 = [_start timeIntervalSince1970];
  NSTimeInterval e1 = s1 + _length;
  NSTimeInterval s2 = [[range start] timeIntervalSince1970];
  NSTimeInterval e2 = s2 + [range length];
  NSTimeInterval s = MAX(s1, s2);
  NSTimeInterval e = MIN(e1, e2);
  if (e <= s)
    return NO;
  return YES;
}

- (BOOL)intersectsWithDay:(Date *)day
{
  NSAssert([day isDate], @"This method expects a date, not a datetime");
  NSTimeInterval s1 = [_start timeIntervalSince1970];
  NSTimeInterval e1 = s1 + _length;
  NSTimeInterval s2 = [day timeIntervalSince1970];
  NSTimeInterval e2 = s2 + 86400;
  NSTimeInterval s = MAX(s1, s2);
  NSTimeInterval e = MIN(e1, e2);
  if (e <= s)
    return NO;
  return YES;
}

- (NSRange)intersectionWithDay:(Date *)day
{
  NSAssert([day isDate], @"This method expects a date, not a datetime");
  NSTimeInterval s1 = [_start timeIntervalSince1970];
  NSTimeInterval e1 = s1 + _length;
  NSTimeInterval s2 = [day timeIntervalSince1970];
  NSTimeInterval e2 = s2 + 86400;
  NSTimeInterval s = MAX(s1, s2);
  NSTimeInterval e = MIN(e1, e2);
  if (e <= s)
    return NSMakeRange(0, 0);
  return NSMakeRange(s - s2, e - s);
}

- (NSString *)description
{
  return [NSString stringWithFormat:@"DateRange starting %@ for %d seconds", [_start description], (int)_length];
}
@end