如何找出自登录以来的时间(以秒为单位)? [英] How to find out the time in seconds since log on?

查看:96
本文介绍了如何找出自登录以来的时间(以秒为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道自用户登录以来经过了多少秒. (TickCount不好,因为那是从系统开始的)

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)

我知道我可以使用lastwho来获取时间戳,但是我正在寻找一种简单但功能强大的解决方案. 我可以在Cocoa中使用它,但是使用Unix命令就可以了.

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.

推荐答案

最后,我使用了getlastlogx(),感谢Rob Napier将我指向正确的方向(+1).

At the end I used getlastlogx(), thanks for Rob Napier for pointing me to the right direction (+1).

这是我想出的:

    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); 

这篇关于如何找出自登录以来的时间(以秒为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆