I like to know how much time in second has elapsed since the user logged on. (TickCount
is not good because that is from system start)
I know I can use last
or who
to get the timestamp but what I am looking for a simple开发者_运维问答 but robust solution.
I would use it in Cocoa but a unix command is fine.
See the utmpx(5) interface, specifically endutxent(3). It's the programmatic interface to what you're looking at from who
and last
.
At the end I used getlastlogx(), thanks for Rob Napier for pointing me to the right direction (+1).
this is I came up with:
struct lastlogx *lastLogin;
uid_t myuid = getuid();
lastLogin = getlastlogx(myuid,nil);
NSDate *dateAtLogon = [NSDate dateWithTimeIntervalSince1970:lastLogin->ll_tv.tv_sec];
NSDate *currentDate = [NSDate date];
NSTimeInterval timeSinceLogin = [currentDate timeIntervalSinceDate:dateAtLogon];
NSLog(@"%1.1f seconds since logon",timeSinceLogin);
精彩评论