当前位置: 技术问答>linux和unix
linux系统中有没有获得当前时钟信息
来源: 互联网 发布时间:2016-06-08
本文导语: 在windows系统中,有GetTickCount函数,来获得自系统启动后经过的时间(单位ms),在linux下有没有类型的函数来获得当前的时间值。 | #include #include #include #include #include #ifndef FSHIFT # defi...
在windows系统中,有GetTickCount函数,来获得自系统启动后经过的时间(单位ms),在linux下有没有类型的函数来获得当前的时间值。
|
#include
#include
#include
#include
#include
#ifndef FSHIFT
# define FSHIFT 16 /* nr of bits of precision */
#endif
#define FIXED_1 (1 FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
int main(int argc, char **argv)
{
int updays, uphours, upminutes;
struct sysinfo info;
struct tm *current_time;
time_t current_secs;
time(¤t_secs);
current_time = localtime(¤t_secs);
sysinfo(&info);
printf(" %02d:%02d:%02d up ",
current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
updays = (int) info.uptime / (60*60*24);
if (updays)
printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
upminutes = (int) info.uptime / 60;
uphours = (upminutes / 60) % 24;
upminutes %= 60;
if (uphours)
printf("%2d:%02d, ", uphours, upminutes);
else
printf("%d min, ", upminutes);
printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ldn",
LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
return 0;
}
#include
#include
#include
#include
#ifndef FSHIFT
# define FSHIFT 16 /* nr of bits of precision */
#endif
#define FIXED_1 (1 FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
int main(int argc, char **argv)
{
int updays, uphours, upminutes;
struct sysinfo info;
struct tm *current_time;
time_t current_secs;
time(¤t_secs);
current_time = localtime(¤t_secs);
sysinfo(&info);
printf(" %02d:%02d:%02d up ",
current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
updays = (int) info.uptime / (60*60*24);
if (updays)
printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
upminutes = (int) info.uptime / 60;
uphours = (upminutes / 60) % 24;
upminutes %= 60;
if (uphours)
printf("%2d:%02d, ", uphours, upminutes);
else
printf("%d min, ", upminutes);
printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ldn",
LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
return 0;
}