-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFuzzyClock.m
More file actions
executable file
·275 lines (237 loc) · 6.31 KB
/
FuzzyClock.m
File metadata and controls
executable file
·275 lines (237 loc) · 6.31 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// FuzzyClock.m
// FuzzyClock
//
// Created by Matthew West on 5/25/14.
// Copyright 2014 Matthew West. All Rights Reserved.
//
#import "FuzzyClock.h"
#import "FuzzyClockView.h"
@implementation FuzzyClock
- (id)initWithBundle:(NSBundle *)bundle
{
self = [super initWithBundle:bundle];
if(self == nil)
return nil;
// we will create and set the MenuExtraView
theView = [[FuzzyClockView alloc] initWithFrame:
[[self view] frame] menuExtra:self];
[self setView:theView];
// init menu stuff
theMenu = [[NSMenu alloc] initWithTitle: @""];
clockMenuItem = [theMenu addItemWithTitle: @"00:00"
action: nil
keyEquivalent: @""];
[clockMenuItem setEnabled:false];
dateMenuItem = [theMenu addItemWithTitle:@"day, month ##, ####" action:nil keyEquivalent:@""];
[dateMenuItem setEnabled:false];
[self setMenu:theMenu];
[self setHighlightMode:YES];
[self updateClock:true];
timer = [[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)1
target:self
selector:@selector(_updateTimer:)
userInfo:nil repeats:YES] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_menuClicked:)
name:NSMenuDidBeginTrackingNotification
object:theMenu];
showSeconds = true;
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMenuDidBeginTrackingNotification
object:theMenu];
[theView release];
[theMenu release];
[timer release];
[super dealloc];
}
- (NSString*)getDateText
{
NSDate *now = [[[NSDate alloc] init] autorelease];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"EEEE, MMMM d, y"];
return [formatter stringFromDate:now];
}
- (NSString*)getClockText
{
NSDate *now = [[[NSDate alloc] init] autorelease];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
if (showSeconds) {
[formatter setDateFormat:@"h:mm:ss a"];
} else {
[formatter setDateFormat:@"h:mm a"];
}
return [formatter stringFromDate:now];
}
- (void)updateClock:(Boolean)update
{
if (update)
[theView setClockString:[self getFuzzyTime]];
if ([self isMenuDown]) {
[self updateMenu];
}
}
- (void)_updateTimer:(NSTimer*)timer
{
NSCalendarDate *now = [NSCalendarDate calendarDate];
// Compute the current 30 seconds step of the current hour
int step = [now minuteOfHour] * 2 + [now secondOfMinute] / 30;
int nextState;
// ...during the first minute we stick at the full hour
if (step < 2)
nextState = 0;
// ...special state before the first full 5 minutes
else if (2 <= step && step <= 5)
nextState = 1;
// ...rounding to full 5 minute steps
else if (step < 116)
nextState = 1 + ((step + 4) / 10);
// ...round to full next hour
else
nextState = 13;
// Add the current hour to the state
nextState += [now hourOfDay] * 100;
// Update if needed
if (state != nextState)
{
state = nextState;
[self updateClock:true];
} else {
[self updateClock:false];
}
}
- (void)_menuClicked:(NSNotification *)notification
{
[self updateClock:true];
[self updateMenu];
}
- (NSString *)getFuzzyTime
{
NSCalendarDate *now = [NSCalendarDate calendarDate];
int hour = [now hourOfDay];
NSString *fuzzy_time;
NSString *hour_name;
NSString *formatting;
if (state % 100 > 7) {
hour++;
}
switch (hour) {
case 0:
case 24:
if (state % 100 == 0)
return @"noon";
hour_name = @"midnight";
break;
case 1:
case 13:
hour_name = @"one";
break;
case 2:
case 14:
hour_name = @"two";
break;
case 3:
case 15:
hour_name = @"three";
break;
case 4:
case 16:
hour_name = @"four";
break;
case 5:
case 17:
hour_name = @"five";
break;
case 6:
case 18:
hour_name = @"six";
break;
case 7:
case 19:
hour_name = @"seven";
break;
case 8:
case 20:
hour_name = @"eight";
break;
case 9:
case 21:
hour_name = @"nine";
break;
case 10:
case 22:
hour_name = @"ten";
break;
case 11:
case 23:
hour_name = @"eleven";
break;
case 12:
if (state % 100 == 0)
return @"noon";
hour_name = @"noon";
break;
default:
hour_name = @"??????";
break;
}
switch (state % 100) {
case 0:
formatting = @"%@ o'clock";
break;
case 1:
formatting = @"shortly after %@";
break;
case 2:
formatting = @"five past %@";
break;
case 3:
formatting = @"ten past %@";
break;
case 4:
formatting = @"quarter past %@";
break;
case 5:
formatting = @"twenty past %@";
break;
case 6:
formatting = @"twentyfive past %@";
break;
case 7:
formatting = @"half past %@";
break;
case 8:
formatting = @"twentyfive to %@";
break;
case 9:
formatting = @"twenty to %@";
break;
case 10:
formatting = @"quarter to %@";
break;
case 11:
formatting = @"ten to %@";
break;
case 12:
formatting = @"five to %@";
break;
case 13:
formatting = @"nearly %@";
break;
default:
formatting = @"???%@???";
break;
}
fuzzy_time = [NSString stringWithFormat: formatting, hour_name];
return fuzzy_time;
}
- (void)updateMenu
{
[clockMenuItem setTitle:[self getClockText]];
[dateMenuItem setTitle:[self getDateText]];
}
@end