获取系统开机的时间(Windows、Linux)

简介:

获取系统启动的时间。Windows系统和Linux系统

 

1、Windows系统

  1)代码如下

复制代码
#include <stdio.h>
#include <time.h>
#include <windows.h>

#define PRINT_ALL    0xff
#define PRINT_DATE    (1 << 0)
#define PRINT_TIME    (1 << 1)

void printTime(time_t nowtime, int iFlag)
{
    struct tm *timeinfo;
    timeinfo = localtime( &nowtime );

    int year, month, day, hour, min, sec;
    year = timeinfo->tm_year + 1900;
    month = timeinfo->tm_mon + 1;
    day = timeinfo->tm_mday;
    hour = timeinfo->tm_hour;
    min = timeinfo->tm_min;
    sec = timeinfo->tm_sec;
    if(iFlag & PRINT_DATE)
    {
        printf("%d-%d-%d ", year, month, day);
    }
    if(iFlag & PRINT_TIME)
    {
        printf("%02d:%02d:%02d", hour, min, sec);
    }
    fflush(stdout);
}

int main()
{
    int iRunTime = GetTickCount();
    time_t nowtime;
    time( &nowtime );
    printf("Now          : ");
    printTime(nowtime, PRINT_ALL);
    printf("\n");

    time_t DateTime = nowtime - (iRunTime / 1000);
    printf("System start : ");
    printTime(DateTime, PRINT_ALL);
    printf("\n");

    int iH, iM, iS, iSec;
    iSec = iRunTime / 1000;
    iH = iSec / 3600;
    iM = iSec / 60 % 60;
    iS = iSec % 60;
    printf("System run   : %02d:%02d:%02d\n", iH, iM, iS);

    return 0;
}
复制代码

  2)运行结果

2、Linux下

   1)代码如下

复制代码
#include <stdio.h>
#include <sys/sysinfo.h>
#include <time.h>
#include <errno.h>
#include <string.h>

static int print_system_boot_time()
{
    struct sysinfo info;
    time_t cur_time = 0;
    time_t boot_time = 0;
    struct tm *ptm = NULL;
    if (sysinfo(&info)) 
    {
        fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",
                errno, strerror(errno));
        return -1;
    }
    time(&cur_time);
    if (cur_time > info.uptime) 
    {
        boot_time = cur_time - info.uptime;
    }
    else
    {
        boot_time = info.uptime - cur_time;
    }
    ptm = gmtime(&boot_time);
    printf("System boot time: %d-%-d-%d %d:%d:%d\n", ptm->tm_year + 1900,
            ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
    return 0; 
}

int main()
{
    if (print_system_boot_time() != 0) 
    {
        return -1;
    }

    return 0;
}
复制代码

  2)运行结果

$ ./getSysRunTime 
System boot time: 2015-2-6 1:29:50

 



本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4277082.html,如需转载请自行联系原作者


相关文章
|
1月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025080401 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025080401 (Linux, Windows) - 专业渗透测试框架
55 0
|
23天前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
333 10
|
22天前
|
安全 Linux iOS开发
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
147 0
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
|
22天前
|
安全 Linux 生物认证
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
32 0
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
|
24天前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
98 0
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
27天前
|
NoSQL IDE MongoDB
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
108 0
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
27天前
|
安全 Linux 数据安全/隐私保护
为Linux系统的普通账户授予sudo访问权限的过程
完成上述步骤后,你提升的用户就能够使用 `sudo`命令来执行管理员级别的操作,而无需切换到root用户。这是一种更加安全和便捷的权限管理方式,因为它能够留下完整的权限使用记录,并以最小权限的方式工作。需要注意的是,随意授予sudo权限可能会使系统暴露在风险之中,尤其是在用户不了解其所执行命令可能带来的后果的情况下。所以在配置sudo权限时,必须谨慎行事。
200 0
|
1月前
|
Ubuntu Linux 开发者
国产 Linux 发行版再添新成员,CutefishOS 系统简单体验
当然,系统生态构建过程并不简单,不过为了帮助国产操作系统优化生态圈,部分企业也开始用国产操作系统替代 Windows,我们相信肯定会有越来越多的精品软件登录 Linux 平台。
79 0
|
1月前
|
Ubuntu 安全 Linux
Linux系统入门指南:从零开始学习Linux
Shell脚本是一种强大的自动化工具,可以帮助您简化重复的任务或创建复杂的脚本程序。了解Shell脚本的基本语法和常用命令,以及编写和运行Shell脚本的步骤,将使您更高效地处理日常任务。
123 0

热门文章

最新文章