Unix/Linux编程日期时间汇编

 

 Unix/Linux 时间类型

Unix/Linux下有以下几种时间结构:

1time_t 类型:长整型,一般用来表示从1970-01-01 00:00:00时以来的秒数,精确度:秒;由函数time()获取;

     该类型定义在头文件 /usr/include/sys/time.h 中:

     #define _TIME_T

       typedef   long   time_t;         /* time of day in seconds */

     #endif  /* _TIME_T */

     函数定义:time_t   time(time_t*  lpt)

     如:time_t   time = time(NULL)

2struct timeb 结构:它有两个主要成员,一个是秒,另一个是毫秒;精确度:毫秒(10E-3)

     由函数ftime()获取struct timeb结构的时间;其定义如下:

     struct timeb

      {

        time_t  time;                     /* time, seconds since the epoch      :秒*/

        unsigned short millitm;     /* 1000 msec of additional accuracy :毫秒 */

        short   timezone;              /* timezone, minutes west of GMT     :时区 */

        short   dstflag;                 /* daylight savings when appropriate :时区标记 */

      };

      #include <sys/timeb.h>

      int  ftime(struct  timeb*  tp);

      调用成功返回0;调用失败返回-1

3struct  timeval 结构,它有两个成员;一个是秒,另一个表示微秒,精确度:微秒(10E-6)

      由函数gettime0fday()获取;

      struct  timeval结构定义为:

      struct  timeval

      {

          long  tv_sec     /*  Seconds  */

          long  tv_usec   /*  MicroSeconds:微秒*/

      }

      读取struct timeval结构数据的函数说明:

      #include  <sys/time.h>
      int  gettimeofday(struct  timeval*  tv
struct  timezone*  tz)

      该函数会提取系统当前时间,并把时间分为秒和微秒两部分填充到结构struct  timeval中;同时把当的时区信息填充到结构struct  timezone中;

      返回值:成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tvtz所指的内存空间超出存取权限。

      struct  timezone结构的定义为:

      struct  timezone

      {

         int  tz_minuteswest  /* Greenwich 时间差了多少分钟 */

         int  tz_dsttime          /*日光节约时间的状态*/

      }

      上述两个结构都定义在/usr/include/sys/time.htz_dsttime 所代表的状态如下

      DST_NONE /*不使用*/

      DST_USA /*美国*/

      DST_AUST /*澳洲*/

      DST_WET /*西欧*/

      DST_MET /*中欧*/ 

      DST_EET /*东欧*/

      DST_CAN /*加拿大*/

      DST_GB /*大不列颠*/

      DST_RUM /*罗马尼亚*/

      DST_TUR /*土耳其*/

      DST_AUSTALT /*澳洲(1986年以后)*/ 

4struct  timespec 结构:它是POSIX.4标准定义的一个时间结构,精确度:纳秒(10E-9)

      由函数gethrestime()gethrestime_lasttick()获取当前系统struct  timespec结构的时间;其定义如下:

      struct  timespec                /* definition per POSIX.4 */

      {

          time_t    tv_sec;            /* Seconds         :秒 */

          long       tv_nsec;          /*  Nanoseconds :纳秒 */

     };

     typedef   struct  timespec   timespec_t

     该结构定义在头头文件 /usr/include/sys/time_impl.h 中;

     extern   void   gethrestime(timespec_t*);
     extern   void   gethrestime_lasttick(timespec_t*);

5clock_t 类型:由函数clock()获取;

     #include  <time.h>

     clock_t   clock(void);

     该函数以微秒的方式返回CPU的时间;

     类型 clock_t 定义在头文件/usr/include/sys/types.h中:

     #ifndef _CLOCK_T
     #define _CLOCK_T
      typedef    long   clock_t; /* relative time in a specified resolution */
     #endif  /* ifndef _CLOCK_T */

6struct  tm 结构:由函数gmtime()解析time_t得到

      struct tm*gmtime(const time_t*timep);

      函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。

      结构tm的定义为

      struct tm

      {

         int   tm_sec;  

       int   tm_min;

        int   tm_hour; 

       int   tm_mday;

         int   tm_mon;

         int   tm_year; 

         int   tm_wday;

         int   tm_yday; 

         int tm_isdst;

      };

      int   tm_sec 代表目前秒数,正常范围为0-59,但允许至61

      int   tm_min 代表目前分数,范围0-59

      int   tm_hour 从午夜算起的时数,范围为0-23

      int   tm_mday 目前月份的日数,范围01-31 

      int   tm_mon 代表目前月份,从一月算起,范围从0-11

      int   tm_year 1900 年算起至今的年数

      int   tm_wday 一星期的日数,从星期一算起,范围为0-6

      int   tm_yday 从今年11日算起至今的天数,范围为0-365

      int   tm_isdst 日光节约时间的旗标

      此函数返回的时间日期未经时区转换,而是UTC时间。

      返回值:返回结构tm代表目前UTC 时间 

7Unix对时间单位的定义:

     #define SEC                1                        //

     #define MILLISEC        1000                  // 毫秒

     #define MICROSEC     1000000            // 微秒

     #define NANOSEC       1000000000      // 纳秒

 

8、时间格式化函数:

     size_t strftime(char *str,size_t max,char *fmt,struct tm *tp);  strftime有点像sprintf,其格式由fmt来指定。  

     %a : 本第几天名称,缩写

     %A : 本第几天名称,全称

     %b : 月份名称,缩写

     %B : 月份名称,全称

     %c : ctime/asctime格式相同

     %d : 本月第几日名称,由零算起

     %H : 当天第几个小时,24小时制,由零算起

     %I : 当天第几个小时,12小时制,由零算起

     %j : 当年第几天,由零算起

     %m : 当年第几月,由零算起

     %M : 该小时的第几分,由零算起

     %p : AMPM

     %S : 该分钟的第几秒,由零算起

     %U : 当年第几,由第一个日开始计算

     %W : 当年第几,由第一个一开始计算

     %w : 当第几日,由零算起

     %x : 当地日期

     %X : 当地时间

     %y : 两位数的年份

     %Y : 四位数的年份

     %Z : 时区名称的缩写

     %% : %符号

 

     char * strptime(char *s,char *fmt,struct tm *tp);  如同scanf一样,解译字串成为tm格式

     %h : %b%B

     %c : 读取%x%X格式

     %C : 读取%C格式

     %e : %d

     %D : 读取%m/%d/%y格式

     %k : %H

     %l : %I

     %r : 读取"%I:%M:%S %p"格式

     %R : 读取"%H:%M"格式

     %T : 读取"%H:%M:%S"格式

     %y : 读取两位数年份

     %Y : 读取四位数年份

 

一些解释和举例

Unix的时间起点为GMT on January 1, 1970

MS-DOS开始于1980

 

时间类型:time_tlinux中为long integer

 

语法:

#include <time.h>

time_t time(time_t *tloc);

time的返回值存储在tloc,

 

实例:

# cat envtime.c

#include <time.h>

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

 

int main()

{

    int i;

    time_t the_time;

 

    for(i = 1; i <= 10; i++) {

        the_time = time((time_t *)0);

        printf("The time is %ld/n", the_time);

        sleep(2);

    }

 

    exit(0);

}

 

运行结果:

 

# ./envtime

The time is 1228204398

The time is 1228204400

The time is 1228204402

The time is 1228204404

The time is 1228204406

The time is 1228204408

The time is 1228204410

The time is 1228204412

The time is 1228204414

The time is 1228204416

 

 

ISO/ANSI C使用difftime的结构。

 

#include <time.h>

double difftime(time_t time1, time_t time2);

 

gmtime提供更精确的时间:

 

#include <time.h>

struct tm *gmtime(const time_t timeval);

 

tm Member Description

int tm_sec Seconds, 0-61

int tm_min Minutes, 0-59

int tm_hour Hours, 0-23

int tm_mday Day in the month, 1-31

int tm_mon Month in the year, 0-11 (January = 0)

int tm_year Years since 1900

int tm_wday Day in the week, 0-6 (Sunday = 0)

int tm_yday Day in the year, 0-365

int tm_isdst Daylight savings in effect

 

实例:

# cat gmtime.c

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    struct tm *tm_ptr;

    time_t the_time;

 

    (void) time(&the_time);

    tm_ptr = gmtime(&the_time);

     printf("Raw time is %ld/n", the_time);

    printf("gmtime gives:/n");

    printf("date: %02d/%02d/%02d/n",

        tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);

    printf("time: %02d:%02d:%02d/n",

        tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);

    exit(0);

}

 

 

运行结果:

 

# ./gmtime

Raw time is 1228205755

gmtime gives:

date: 108/12/02

time: 08:15:55

 

输出的是:GMT (now known as Coordinated Universal Time, or UTC)时间,本地时间需要使用:

 

#include <time.h>

struct tm *localtime(const time_t *timeval);

 

tm转换为time_t,可以使用mktime,如果转换不成功,返回-1

 

#include <time.h>

time_t mktime(struct tm *timeptr);

 

需要更友好的输出,

 

#include <time.h>

char *asctime(const struct tm *timeptr);

char *ctime(const time_t *timeval);

 

前者的输出类似:

Sun Jun 9 12:34:56 2007/n/0

 

后者的输出类似:

asctime(localtime(timeval))

 

实例:

# cat ctime.c

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    time_t timeval;

    (void)time(&timeval);

    printf("The date is: %s", ctime(&timeval));

    exit(0);

}

 

# ./ctime

The date is: Tue Dec  2 16:26:27 2008

 

strftime函数可以提供更多的控制:

 

#include <time.h>

 

size_t strftime(char *s, size_t maxsize, const char *format, struct tm *timeptr);

 

Conversion Specifier Description

%a Abbreviated weekday name

%A Full weekday name

%b Abbreviated month name

%B Full month name

%c Date and time

%d Day of the month, 01-31

%H Hour, 00-23

%I Hour in 12-hour clock, 01-12

%j Day of the year, 001-366

%m Month of the year, 01-12

%M Minutes, 00-59

%p a.m. or p.m.

%S Seconds, 00-61

%u Day in the week, 1-7 (1 = Monday)

%U Week in the year, 01-53 (Sunday is the first day of the week.)

%V Week in the year, 01-53 (Monday is the first day of the week.)

%w Day in the week, 0-6 (0 = Sunday)

%x Date in local format

%X Time in local format

%y Year number less 1900

%Y Year

%Z Time zone name

%% A % character

 

比如通常date的显示“Wed Dec  3 09:20:41 CST 2008”就类似: %a %b %d %H:%M:%S %Y”。

 

strptime可以帮助阅读日期,格式化和format类似,扫描字符串时和sscanf类似。strptime的转换比strftime复杂,

 

#include <time.h>

char *strptime(const char *buf, const char *format, struct tm *timeptr);

 

实例:

 

# cat strftime.c

#define _XOPEN_SOURCE /* glibc2 needs this for strptime */

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

    struct tm *tm_ptr, timestruct;

    time_t the_time;

    char buf[256];

char *result;

 

    (void) time(&the_time);

 

    tm_ptr = localtime(&the_time);

strftime(buf, 256, "%A %d %B, %I:%S %p", tm_ptr);

 

    printf("strftime gives: %s/n", buf);

    strcpy(buf,"Thu 26 July 2007, 17:53 will do fine");

    printf("calling strptime with: %s/n", buf);

    tm_ptr = &timestruct;

    result = strptime(buf,"%a %d %b %Y, %R", tm_ptr);

    printf("strptime consumed up to: %s/n", result);

    printf("strptime gives:/n");

    printf("date: %02d/%02d/%02d/n",

        tm_ptr->tm_year % 100, tm_ptr->tm_mon+1, tm_ptr->tm_mday);

    printf("time: %02d:%02d/n",

        tm_ptr->tm_hour, tm_ptr->tm_min);

    exit(0);

}

 

运行结果:

# ./strftime

strftime gives: Wednesday 03 December, 09:58 AM

calling strptime with: Thu 26 July 2007, 17:53 will do fine

strptime consumed up to:  will do fine

strptime gives:

date: 07/07/26

time: 17:53

 

注意:

本文章来自互联网络,感谢资料提供者!

 

可以参照:

http://blog.csdn.net/zkx1982/archive/2007/12/21/1957021.aspx

http://blog.chinaunix.net/u/21908/showart_1676556.html

 http://www.rosoo.net/a/cpp/2010/0126/8398.html

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值