Linux下如何查看系统运行时长《程序员必知》

Linux系统查看当前时间的命令


Linux系统的代码性操作很繁琐,怎样查看和修改Linux的时间呢?下面是Linux系统查看当前时间的命令,希望学习啦小编整理的对你有用,欢迎阅读:

  Linux系统查看当前时间的命令:

  一、查看和修改Linux的时区

  1. 查看当前时区

  命令 : "date -R"

  2. 修改设置Linux服务器时区

  方法 A

  命令 : "tzselect"

  方法 B 仅限于RedHat Linux 和 CentOS

  命令 : "timeconfig"

  方法 C 适用于Debian

  命令 : "dpkg-reconfigure tzdata"

  3. 复制相应的时区文件,替换系统时区文件;或者创建链接文件

  cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime

  例如:在设置中国时区使用亚洲/上海(+8)

  cp
/usr/share/zoneinfo/Asia/Shanghai /etc/localtime


Linux下如何查看系统运行时长《程序员必知》

二、查看和修改Linux的时间

  1. 查看时间和日期

  命令 : "date"

  2.设置时间和日期

  例如:将系统日期设定成2009年11月3日的命令

  命令 : "date -s 11/03/2009"

  将系统时间设定成下午5点55分55秒的命令

  命令 : "date -s 17:55:55"

  3. 将当前时间和日期写入BIOS,避免重启后失效

  命令 : "hwclock -w"

  注:

  date

  不加参数可以直接看到当前日期时间

  cal

  不加参数可以直接看到本月月历


Linux下如何查看系统运行时长《程序员必知》


Linux下如何查看系统运行时长《程序员必知》


Linux下如何查看系统运行时长《程序员必知》


Linux下如何查看系统运行时长《程序员必知》

Linux获取系统当前时间(精确到毫秒)


#include

#include

#include

void sysLocalTime()

{

time_t timesec;

struct tm *p;


time(×ec);

p = localtime(×ec);

printf("%d%d%d%d%d%d\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);

}

void sysUsecTime()

{

struct timeval tv;

struct timezone tz;

struct tm *p;

gettimeofday(&tv, &tz);

printf("tv_sec:%ld\n",tv.tv_sec);

printf("tv_usec:%ld\n",tv.tv_usec);

printf("tz_minuteswest:%d\n",tz.tz_minuteswest);

printf("tz_dsttime:%d\n",tz.tz_dsttime);

p = localtime(&tv.tv_sec);

printf("time_now:%d%d%d%d%d%d.%ld\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);

}

int main(void)

{

sysLocalTime();

printf("============gettimeofday==============\n");

sysUsecTime();

return 0;

}



Linux下如何查看系统运行时长《程序员必知》


Linux下如何查看系统运行时长《程序员必知》


Linux下如何查看系统运行时长《程序员必知》

关注+后台私信;资料;两个字可以免费领取 资料内容包括:C/C++,Linux,golang,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,嵌入式 等。。


分享到:


相關文章: