比如获取当前年份:
     
    

   /* 获取当前系统时间 暂时不使用
        int iyear = 0;
int sysyear = 0;
time_t now;
struct tm *timenow;
time(&now);
timenow = localtime(&now);
sysyear = timenow->tm_year+1900;
*/

linux下获取系统时间的方法

可以用 localtime 函数分别获取年月日时分秒的数值。

Linux下获得系统时间的C语言的实现方法:

1. 可以用 localtime 函数分别获取年月日时分秒的数值。

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

  #include<stdio.h>     //C语言的I/O

  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函数把时间转换成字符,通过printf()函数输出

  }

  注释:time_t是一个在time.h中定义好的结构体。而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;//years   from   1900

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

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

  int   tm_isdst;//Daylight   Saving   time   indicator

  };

   2. 对某些需要较高精准度的需求,Linux提供了gettimeofday()。

#include   <stdio.h>

  #include   <stdlib.h>

  #include   <sys/time.h>

  int  main(int argc,   char **argv)

  {

  struct   timeval   start,stop,diff;

  gettimeofday(&start,0);

  //做你要做的事...

  gettimeofday(&stop,0);

  tim_subtract(&diff,&start,&stop);
//不同的版本函数名称可能有些不一样
   //ubuntu 10.10 上的函数是timersub(&start,&stop,&diff);   
    printf("总计用时:%d毫秒/n",diff.tv_usec);

  }

  int tim_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
  //int timersub( struct timeval *x, struct timeval *y,struct timeval *result)
  {   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 C 获取系统时间信息的更多相关文章

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

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

  2. Linux sysinfo获取系统相关信息

    Linux中,可以用sysinfo来获取系统相关信息. #include <stdio.h> #include <stdlib.h> #include <errno.h& ...

  3. linux下获取系统时间 和 时间偏移

    获取linux时间  并计算时间偏移 void getSystemTimer(void){#if 0 char *wdate[]={"Sun","Mon",&q ...

  4. linux中获取系统时间的几种方法

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

  5. linux中获取系统时间 gettimeofday函数

    linux的man页中对gettimeofday函数的说明中,有这样一个说明:   $ man gettimeofday DESCRIPTION     The functions gettimeof ...

  6. linux c 获取系统时间

    #include <time.h> main() { time_t timep; time (&timep); printf(“%s”,asctime(gmtime(&ti ...

  7. c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET

    c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET c++ 如何获取系统时间 分类: C/C++ 2008-05-08 22:15 14115人阅读 评论(5 ...

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

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

  9. Android获取系统时间方法的总结

    Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...

随机推荐

  1. Android使用tcpdump抓包

    AllJoyn中有个问题:Server切换到Client后,重新加入其他Server时join session会失败,原因是timeout(join session是异步的,在指定时间内没有收到回应) ...

  2. Netty4 ServerBootstrap 初始化channelFactory ReflectiveChannelFactory

    只需要在启动之前传入你需要用的channel类型就可以了. ServerBootstrap初始化channelFactory过程: 最后我们再来看看这个channelFactory的使用场景:

  3. JavaScript 数字与字符串 比较大小

    总结一下JS中经常遇到纯数字和各种各样的字符串进行比较: 纯数字之间的比较 alert(1<3);//true 数字字符串比较,会将其先转成数字 alert("1"<& ...

  4. JS里取前天,昨天和今天

    var today=new Date(); var yesterday=new Date(today.getTime()-1000*60*60*24); var thedaybeforeyesterd ...

  5. 【CI】系列一:总体环境规划

    上周花了点时间把CI环境再次给搞起来了,但是觉得在实体机中总觉得不是很安心,安全性不足,另外没有做备份,安全性.扩展性等都不足,且不好迁移. 因为目前只给了我一台PC及,配置其实也不怎么样.但是却需要 ...

  6. jQuery控制form表单元素聚焦

      CreateTime--2017年5月28日08:57:16Author:Marydon jQuery使form表单的第一个文本框聚焦 /** * 使form表单的第一个文本框聚焦 */ func ...

  7. POJ1088 动态规划

    题意: id=1088">题目链接 解答: 这个题目和最长子序列什么的极为类似.只是之前都是一维,如今变成二维的了.仅此而已.因此我们能够想办法把它先变成一维的. 先用一个结构体存储这 ...

  8. 异步编程C#回调方法

    1.什么是异步? 异步操作通常用于执行完成时间可能较长的任务,如打开大文件.连接远程计算机或查询数据库.异步操作在主应用程序线程以外的线程中执行.应用程序调用方法异步执行某个操作时,应用程序可在异步方 ...

  9. C# NPOI操作Excel(上)

    NPOI 官网下载DLL:http://npoi.codeplex.com/releases 1.读取Excel转为DataTable /// <summary> /// 读取excel转 ...

  10. android之ScrollView里嵌套ListView(转)

    hi,大家好,研究完ScrollView嵌套ScrollView之后,本人突然又想研究ScrollView里嵌套ListView了. 如果还不知道ScrollView嵌套ScrollView是怎么实现 ...