Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
Here's my solution. I've changed the name of your class to AppController, because it makes more sense (and starts with a capital letter!).

AppController.h:

Code:
#import <Cocoa/Cocoa.h>


@interface AppController : NSObject
{
	IBOutlet NSTextField *textField;
}

- (IBAction)createTime:(id)sender;

- (NSNumber *)longestRecordedUpTime;
- (NSArray *)lastTenRecordedUpTimes;
@end

AppController.m:
Code:
#import "AppController.h"

NSString *MBLongestRecordedUpTime = @"MBLongestRecordedUpTime";
NSString *MBLastTenRecordedUpTimes = @"MBLastTenRecordedUpTimes";


@interface NSNumber (MBUpTimeAdditions)

+ (id)currentUpTime;
- (NSString *)formattedUpTimeString;
@end


@implementation AppController

- (IBAction)createTime:(id)sender;
{
	NSNumber *currentUpTime = [NSNumber currentUpTime];
	[textField setStringValue:[currentUpTime formattedUpTimeString]];
	
	if ([[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime] == nil || [currentUpTime compare:[[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime]] == NSOrderedDescending)
		[[NSUserDefaults standardUserDefaults] setObject:currentUpTime forKey:MBLongestRecordedUpTime];
		
	NSMutableArray *lastTenUpTimes = [[[NSUserDefaults standardUserDefaults] objectForKey:MBLastTenRecordedUpTimes] mutableCopy];
	if (lastTenUpTimes == nil)
		lastTenUpTimes = [[NSMutableArray alloc] initWithCapacity:10];
	[lastTenUpTimes insertObject:currentUpTime atIndex:0];
	if ([lastTenUpTimes count] > 10)
		[lastTenUpTimes removeLastObject];
	[[NSUserDefaults standardUserDefaults] setObject:lastTenUpTimes forKey:MBLastTenRecordedUpTimes];
	[lastTenUpTimes release];
}

- (NSNumber *)longestRecordedUpTime;
{
	return [[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime];
}

- (NSArray *)lastTenRecordedUpTimes;
{
	return [[NSUserDefaults standardUserDefaults] objectForKey:MBLastTenRecordedUpTimes];
}

@end


@implementation NSNumber (MBUpTimeAdditions)

+ (id)currentUpTime;
{
	return [self numberWithUnsignedLongLong:UnsignedWideToUInt64(AbsoluteToNanoseconds(UpTime())) / 1000000000];	// UpTime in seconds
}

- (NSString *)formattedUpTimeString;
{
	unsigned days = [self unsignedLongLongValue] / (60 * 60 * 24);
	unsigned hours = ([self unsignedLongLongValue] / (60 * 60)) % 24;
	unsigned minutes = ([self unsignedLongLongValue] / 60) % 60;
	
	return [NSString stringWithFormat:@"%ud %uh %um", days, hours, minutes];
}

@end

thanks. i ran your code, and it compiled, but i don't think it works with our current user interface.

did you make your own user interface?
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
Er, yes, I had to as you didn't post your whole project.

To get that code working with your existing interface, you would have to drag the header file into Interface Builder for parsing, create an instance of AppController in the nib, and make the outlet and target/action connections between the instance of AppController and your text field.

In any case, I've made some adjustments - the full project is attached.
 

Attachments

  • UpTime.zip
    43.5 KB · Views: 52

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
Er, yes, I had to as you didn't post your whole project.

To get that code working with your existing interface, you would have to drag the header file into Interface Builder for parsing, create an instance of AppController in the nib, and make the outlet and target/action connections between the instance of AppController and your text field.

In any case, I've made some adjustments - the full project is attached.

thanks, that really helps. i'll try yours, and i'll try to use that code with my existing interface to see if i can get it to work.

thanks
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
well, your program works great. but when you click "get uptime" is puts a new uptime in the last 10 uptimes.

is there any way to keep track of your real uptime in that box, not just the last 10 times you clicked that button?
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
My example contains everything you need to get started - if you want to change the way the app behaves I'm afraid you will have to work on it yourself.

PS. If you're looking for a way to check whether or not the Mac has been restarted since the uptime was last measured, I don't know how to do that.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
My example contains everything you need to get started - if you want to change the way the app behaves I'm afraid you will have to work on it yourself.

PS. If you're looking for a way to check whether or not the Mac has been restarted since the uptime was last measured, I don't know how to do that.

thanks for all you've done. it works great. i don't know how to check to see if the mac has been restarted, and stuff like that.

but maybe it's better like it is though. it's not bad. i like it. i just need to try to figure out how to change the interface though.

thanks again :)
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
London, England
No problem ... My perfectionism got the better of me and I decided to fix it up after all.

The new version now automatically updates the current uptime every minute, and only places an uptime into the list of last ten uptimes after a restart.

EDIT: Oops, bug. Fixed.
 

Attachments

  • UpTime3.zip
    43.3 KB · Views: 57

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
No problem ... My perfectionism got the better of me and I decided to fix it up after all.

The new version now automatically updates the current uptime every minute, and only places an uptime into the list of last ten uptimes after a restart.

EDIT: Oops, bug. Fixed.

wow, thanks! you really didn't have to do that, but i'm glad that you did. thank you.

now do you know how to make the interface look more like leopard? or itunes?
 

Lixivial

macrumors 6502a
I apologize in advance if this is of no help whatsoever, Nutter. :)

Does anyone know how to get the time of the last restart?

You may find some help by looking at the manpages for utmp (5). ut_name will contain reboot, and time_t contains the time it went down for reboot. For an example of how this information is parsed, the Terminal command "last" parses /private/var/log/wtmp. "last reboot" will give the list of restarts, while "last -1 reboot" will display just the previous reboot time.

Otherwise the very last post in cocoadev's FindingUptime article may be of assistance. In that case it uses sysctl and gives you the time that the system (kernel) was booted this session.

Again, I don't know if that's of any assistance to you or not.

EDIT: Typo. Fixed "list -1 reboot".
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
Thank you Lixivial, that was very helpful.

Here's my final final version. I am retiring. :)

sorry to bring back this old thread, but i stumbled upon something.

your uptime is held in '/usr/bin/uptime'.

i was thinking that this could help this program?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.