Skip to content

Commit 0274181

Browse files
committed
Added class SSYSystemUptimer.
1 parent ae7055d commit 0274181

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

SSYSystemUptimer.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#import <Foundation/Foundation.h>
2+
3+
extern NSString* const SSYSystemUptimerErrorDomain;
4+
extern NSInteger const SSYSystemUptimerSystemCommandFailedErrorCode;
5+
extern NSInteger const SSYSystemUptimerCouldNotParseSystemResponse;
6+
7+
@interface SSYSystemUptimer : NSObject
8+
9+
/*!
10+
@brief Returns the last date at which the system woke from sleep
11+
12+
@details
13+
14+
@param error_p Pointer which will, upon return, if an error
15+
occurred and said pointer is not NULL, point to an NSError
16+
describing said error.
17+
*/
18+
+ (NSDate*)lastWakeFromSleepError_p:(NSError**)error_p ;
19+
20+
@end

SSYSystemUptimer.m

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#import "SSYSystemUptimer.h"
2+
#import "SSYShellTasker.h"
3+
4+
NSString* const SSYSystemUptimerErrorDomain = @"SSYSystemUptimerErrorDomain";
5+
NSInteger const SSYSystemUptimerSystemCommandFailedErrorCode = 214751;
6+
NSInteger const SSYSystemUptimerCouldNotParseSystemResponse = 214752;
7+
8+
@implementation SSYSystemUptimer
9+
10+
+ (NSDate*)lastWakeFromSleepError_p:(NSError**)error_p {
11+
NSDate* answer = nil;
12+
NSError* error = nil;
13+
14+
/* I tested using "/usr/bin/pmset -g log", but that seemed to generate
15+
much more stdout than this: */
16+
NSString* command = @"/usr/sbin/sysctl";
17+
NSArray* arguments = [NSArray arrayWithObjects:
18+
@"-a",
19+
nil];
20+
NSData* stdoutData = nil;
21+
NSInteger result = [SSYShellTasker doShellTaskCommand:command
22+
arguments:arguments
23+
inDirectory:nil
24+
stdinData:nil
25+
stdoutData_p:&stdoutData
26+
stderrData_p:NULL
27+
timeout:5.0
28+
error_p:&error];
29+
if (result != 0) {
30+
error = [NSError errorWithDomain:SSYSystemUptimerErrorDomain
31+
code:SSYSystemUptimerSystemCommandFailedErrorCode
32+
userInfo:@{
33+
NSLocalizedDescriptionKey: NSLocalizedString(@"System command failed", nil),
34+
@"Command": command,
35+
@"Arguments": arguments,
36+
@"Result": [NSNumber numberWithInteger:result],
37+
NSUnderlyingErrorKey: error
38+
}];
39+
} else {
40+
NSString* stdoutString = [[NSString alloc] initWithData:stdoutData
41+
encoding:NSUTF8StringEncoding] ;
42+
NSArray* lines = [stdoutString componentsSeparatedByString:@"\n"];
43+
for (NSString* line in lines) {
44+
if ([line rangeOfString:@"waketime"].location != NSNotFound) {
45+
NSScanner* scanner = [[NSScanner alloc] initWithString:line];
46+
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
47+
intoString:NULL];
48+
NSString* timeString = nil;
49+
[scanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
50+
intoString:&timeString];
51+
[scanner release];
52+
NSTimeInterval timeSeconds = [timeString integerValue];
53+
answer = [NSDate dateWithTimeIntervalSince1970:timeSeconds];
54+
break;
55+
}
56+
}
57+
58+
if (!answer) {
59+
error = [NSError errorWithDomain:SSYSystemUptimerErrorDomain
60+
code:SSYSystemUptimerCouldNotParseSystemResponse
61+
userInfo:@{
62+
NSLocalizedDescriptionKey: NSLocalizedString(@"Could not parse system response", nil)
63+
}];
64+
}
65+
66+
[stdoutString release];
67+
}
68+
69+
if (error && error_p) {
70+
*error_p = error ;
71+
}
72+
73+
return answer;
74+
}
75+
76+
@end

0 commit comments

Comments
 (0)