一、 localtime 函数获取(年/月/日/时/分/秒)数值。

1、感性认识

#include<time.h>     //C语言的头文件

#include<stdio.h>

void   main()

{

time_t   now;         //实例化time_t结构

struct   tm     *timenow;         //实例化tm结构指针

time(&now);//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now

timenow   =   localtime(&now);//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)

printf("Local   time   is   %s\n",asctime(timenow));//asctime函数把时间转换成字符

}

2、tm结构体原形

struct   tm

{

int   tm_sec;//seconds   0-61

int   tm_min;//minutes   1-59

int   tm_hour;//hours   0-23

int   tm_mday;//day   of   the   month   1-31

int   tm_mon;//months   since   jan   0-11

int   tm_year;//

int   tm_wday;//week days   since   Sunday,   0-6

int   tm_yday;//year days   since   Jan   1,   0-365

int   tm_isdst;//Daylight   Saving   time   indicator

};

二. gettimeofday函数(较高精准度的需求--Linux提供)

[

注释:

下面例子是计算程序在执行操作过程中所使用时间

]

#include   <stdio.h>

#include   <stdlib.h>

#include   <sys/time.h>

int  main(int argc,   char **argv)

{

struct   tim   start,stop,diff;

gettimeofday(&start,0);

//做你要做的事...

gettimeofday(&stop,0);

tim_subtract(&diff,&start,&stop);

printf("总计用时:%d毫秒\n",diff.tv_usec);

}

int tim_subtract(struct tim *result, struct tim *x, struct tim *y)

{

int nsec;

if ( x->tv_sec > y->tv_sec )

return   -1;

if ((x->tv_sec == y->tv_sec) && (x->tv_usec > y->tv_usec))

return   -1;

result->tv_sec = ( y->tv_sec - x->tv_sec );

result->tv_usec = ( y->tv_usec - x->tv_usec );

if ( result->tv_usec < 0 )

{

result->tv_sec --;

result->tv_usec += 1000000;

}

return   0;

}

三、linux下date命令获取当前时间与修改。

date //显示当前日期

date -s //设置当前时间,只有root权限才能设置,其他只能查看。

date -s 20061010 //设置成20061010,这样会把具体时间设置成空00:00:00

date -s 12:23:23 //设置具体时间,不会对日期做更改

date -s “12:12:23 2006-10-10″ //设置全部时间

[置顶] 获取系统时间的方法--linux的更多相关文章

  1. C++11新特性,利用std::chrono精简传统获取系统时间的方法

    一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...

  2. C++获取系统时间的方法

    //首先是了解这个结构体,_SYSTEMTIME ,然后通过系统函数GetLocalTime往这个结构体的变量中写入当前系统时间typedef struct _SYSTEMTIME { WORD wY ...

  3. linux下C获取系统时间的方法

    asctime(将时间和日期以字符串格式表示)  相关函数 time,ctime,gmtime,localtime  表头文件 #include  定义函数 char * asctime(const ...

  4. c#获取系统时间的方法(转)

      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  5. Android各代码层获取系统时间的方法

    1. 在java层,long now = SystemClock.uptimeMillis(); 2. 在native层,nsecs_t now = systemTime(SYSTEM_TIME_MO ...

  6. Linux C 语言 获取系统时间信息

    比如获取当前年份:        /* 获取当前系统时间 暂时不使用        int iyear = 0;        int sysyear = 0;        time_t now;  ...

  7. Linux C 获取系统时间信息

    比如获取当前年份:               /* 获取当前系统时间 暂时不使用 ; ; time_t now; struct tm *timenow; time(&now); timeno ...

  8. Linux驱动中获取系统时间

    最近在做VoIP方面的驱动,总共有16个FXS口和FXO口依次初始化,耗用的时间较多.准备将其改为多线程,首先需要确定哪个环节消耗的时间多,这就需要获取系统时间. #include <linux ...

  9. shell下获取系统时间

    shell下获取系统时间的方法直接调用系统变量 获取今天时期:`date +%Y%m%d` 或 `date +%F` 或 $(date +%y%m%d) 获取昨天时期:`date -d yesterd ...

随机推荐

  1. ysql+heartbeat+DRBD+LVS实现mysql高可用

    在企业应用中,mysql+heartbeat+DRBD+LVS是一套成熟的集群解决方案,通过heart+DRBD实现mysql的主 节点写操作的高可用性,而通过mysql+LVS实现数据库的主从复制和 ...

  2. C语言的本质(38)——makefile之变量

    我们详细看看Makefile中关于变量的语法规则.先看一个简单的例子: foo = $(bar) bar = Huh? all: @echo$(foo) 我们执行make将会打出Huh?.当make读 ...

  3. windows phone中,将crash report记录下来,写入文件,方便分析

    APP出现crash(崩溃)总是不能忍的 当我们连接调试器调试的时候,发现每当APP崩溃的时候 程序都会走到App.xaml.cs中的 // Code to execute on Unhandled ...

  4. java面试题集1

    一:单选题 下列哪一种叙述是正确的(D )A. abstract修饰符可修饰字段.方法和类B. 抽象方法的body部分必须用一对大括号{ }包住C. 声明抽象方法,大括号可有可无D. 声明抽象方法不可 ...

  5. vs debug 快捷键

    命令名 快捷键 说明 调试.应用代码更改 Alt + F10 启动生成操作,利用它可以通过“编辑并继续”功能应用对正在调试的代码所作的更改. 调试.自动窗口 Ctrl + D,Ctrl + A 显示“ ...

  6. POJ2241——The Tower of Babylon

    The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1 ...

  7. react redux 相关技术

    React全都是围绕着组件的, 所以React基础也就是:写组件的jsx.组件的生命周期以及组件的属性和状态.jsx,只要是用过html模板的分分钟就能写了: 所谓生命周期就是组件在创建.销毁.更新阶 ...

  8. php 添加 redis 扩展模块

    由于PHP源码中并未有redis的文件,所以需要自己下载. 下载地址: http://pecl.php.net/get/redis-2.2.5.tgz [root@study package]# ta ...

  9. Gridview 多重表头 (一)

    今天看到一个人每个月更新博客,结果七年后改行去卖土特产...感慨良多... 虽然我也想去开餐厅~~ 今天需求里有一个多重表头,感觉比较奇特,特意留下记录,以防我的大脑被艾滋海默攻占~~没有女主的命,不 ...

  10. Ascll

    ascll - 概述 ASCII码(American Standard Code for Information Interchange, 美国标准信息交换码).   信息编码就是将表示信息的某种符号 ...