开发者

How to find out the time in seconds since log on?

开发者 https://www.devze.com 2023-04-05 16:34 出处:网络
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 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); 
0

精彩评论

暂无评论...
验证码 换一张
取 消