LINUX C系统编程与PYTHON中的时间模块对比

今天看python时间模块time的时候发现和LINUX系统编程中的时间调用函数基本一样,以前刚好没有好好学习LINUX C编程的时间模块就对比进行了学习。

  • 本文只是给出函数接口和使用方式,详细了解请参考LINUX main page和PYTHON help
  • 本文不涉及asctime和ctime,并且C中涉及多线程编程注意选择可重入性函数

一、时间存在方式

其实不管是C还是PYTHON这里都包含3种时间不同存在的方式

  • 日历时间:Epoch以来的秒及新纪元时间(1970年1月1日 00:00:00)以来的秒
  • 分解时间:实际上是时间在程序内部存在的结构,也是一个中间介质,C中使用结构体,PYTHON中使用元组
  1. LINUX C为tm结构体:
struct tm {
              int tm_sec;         /* seconds */
              int tm_min;         /* minutes */
              int tm_hour;        /* hours */
              int tm_mday;        /* day of the month */
              int tm_mon;         /* month */
              int tm_year;        /* year */
              int tm_wday;        /* day of the week */
              int tm_yday;        /* day in the year */
              int tm_isdst;       /* daylight saving time */
                                  /* >0 DST is in effect
                                     =0 DST is not effect;
                                     <0 DST information not available
                                   */
          }; 
  1. PYTHON struct_time元组:
索引(Index)   属性(Attribute)
0               tm_year(年)
1               tm_mon(月)
2               tm_mday(日)
3               tm_hour(时)
4               tm_min(分)
5               tm_sec(秒)
6               tm_wday(weekday)
7               tm_yday(一年中的第几天)
8               tm_isdst(是否是夏令时) 

可以看到他们基本是一致的

  • 可读时间:这种一般就是我们平时看到的一些方便读取的的时间,比如
2017-10-12 06:52:03(AM) +0800 

就是方便人类读取的可读取时间

LINUX C可读时间格式:

%a     The abbreviated weekday name according to the current locale.
%A     The full weekday name according to the current locale.
%b     The abbreviated month name according to the current locale.
%B     The full month name according to the current locale.
%c     The preferred date and time representation for the current locale.
%C     The century number (year/100) as a 2-digit integer. (SU)
%d     The day of the month as a decimal number (range 01 to 31).
%D     Equivalent  to  %m/%d/%y.   (Yecch—for  Americans only.  Americans should note that in other countries %d/%m/%y is rather common.  This means that in
      international context this format is ambiguous and should not be used.) (SU)
%e     Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU)
%E     Modifier: use alternative format, see below. (SU)
%F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
%G     The ISO 8601 week-based year (see NOTES) with century as a decimal number.  The 4-digit year corresponding to the ISO week number (see %V).  This has
      the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ)
%g     Like %G, but without century, that is, with a 2-digit year (00-99). (TZ)
%h     Equivalent to %b.  (SU)
%H     The hour as a decimal number using a 24-hour clock (range 00 to 23).
%I     The hour as a decimal number using a 12-hour clock (range 01 to 12).
%j     The day of the year as a decimal number (range 001 to 366).
%k     The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank.  (See also %H.)  (TZ)
%l     The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank.  (See also %I.)  (TZ)
%m     The month as a decimal number (range 01 to 12).
%M     The minute as a decimal number (range 00 to 59).
%n     A newline character. (SU)
%O     Modifier: use alternative format, see below. (SU)
%p     Either  "AM" or "PM" according to the given time value, or the corresponding strings for the current locale.  Noon is treated as "PM" and midnight as
      "AM".
%P     Like %p but in lowercase: "am" or "pm" or a corresponding string for the current locale. (GNU)
%r     The time in a.m. or p.m. notation.  In the POSIX locale this is equivalent to %I:%M:%S %p.  (SU)
%R     The time in 24-hour notation (%H:%M).  (SU) For a version including the seconds, see %T below.
%s     The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
%S     The second as a decimal number (range 00 to 60).  (The range is up to 60 to allow for occasional leap seconds.)
%t     A tab character. (SU)
%T     The time in 24-hour notation (%H:%M:%S).  (SU)
%u     The day of the week as a decimal, range 1 to 7, Monday being 1.  See also %w.  (SU)
%U     The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01.  See also %V and
      %W.
%V     The ISO 8601 week number (see NOTES) of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days
      in the new year.  See also %U and %W.  (SU)
%w     The day of the week as a decimal, range 0 to 6, Sunday being 0.  See also %u.
%W     The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01.
%x     The preferred date representation for the current locale without the time.
%X     The preferred time representation for the current locale without the date.
%y     The year as a decimal number without a century (range 00 to 99).
%Y     The year as a decimal number including the century.
%z     The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC). (SU)
%Z     The timezone name or abbreviation.
%+     The date and time in date(1) format. (TZ) (Not supported in glibc2.)
%%     A literal '%' character. 

python 可读时间格式:

%Y  Year with century as a decimal number.
%m  Month as a decimal number [01,12].
%d  Day of the month as a decimal number [01,31].
%H  Hour (24-hour clock) as a decimal number [00,23].
%M  Minute as a decimal number [00,59].
%S  Second as a decimal number [00,61].
%z  Time zone offset from UTC.
%a  Locale's abbreviated weekday name.
%A  Locale's full weekday name.
%b  Locale's abbreviated month name.
%B  Locale's full month name.
%c  Locale's appropriate date and time representation.
%I  Hour (12-hour clock) as a decimal number [01,12].
%p  Locale's equivalent of either AM or PM.

Other codes may be available on your platform.  See documentation for
the C library strftime function. 

本文使用这三个术语

二、日历时间

下面方法获取当前系统时间的当前时间

  • LINUX C:
    int gettimeofday(struct timeval *tv,struct timezone *tz);
    time_t time(time_t *timep)
  • PYTHON:
    time.time()
    上面的方式都是获取当前时间的日历时间的方式为了方便我们使用time方法

三、日历时间转换为分解时间

  • LINUX C:
    strcut tm* gmtime(const time_t* timep);
    strcut tm* localtime(const time_t* timep);
Each of these functions returns the value described, or NULL  in case an error was detected. 
  • PYTHON:
    time.gmtime(seconds=None)
    time.localtime(seconds=None)

可以看到他们是一致的其中gmtime代表返回UTC时间,而localtime代表返回本地时区时间

四、将分解时间转换为日历时间

  • LINUX C:
    time_t mktime(struct tm* tmieptr)
RETURN VALUE
      -1 in case of mktime() in case an error was detected. 
  • PYTHON:
    time.mktime(p_tuple)

五、将分解时间转换为可读时间

  • LINUX C:
    size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
RETURN VALUE
      Provided that the result string, including the terminating null byte, does not exceed max bytes, strftime() returns the number of bytes (excluding the ter‐
      minating  null  byte)  placed  in the array s.  If the length of the result string (including the terminating null byte) would exceed max bytes, then strf‐
      time() returns 0, and the contents of the array are undefined.  (This behavior applies since at least libc 4.4.4; very old versions of libc, such  as  libc
      4.4.1, would return max if the array was too small.)

      Note  that the return value 0 does not necessarily indicate an error.  For example, in many locales %p yields an empty string.  An empty format string will
      likewise yield an empty string. 
  • PYTHON:
    strftime(format, p_tuple=None)

这里需要注意format,format在文章开头已经给出

六、将可读时间转换为分解时间

  • LINUX C:
    char *strptime(const char *s, const char *format, struct tm *tm);
RETURN VALUE
      The return value of the function is a pointer to the first character not processed in this function call.  In case the input string contains  more  charac‐
      ters  than required by the format string the return value points right after the last consumed input character.  In case the whole input string is consumed
      the return value points to the null byte at the end of the string.  If strptime() fails to match all of the format string and therefore an  error  occurred
      the function returns NULL. 
  • PYTHON:
    strptime(string, format)
    这里需要注意format,format在文章开头已经给出

七、转换图

这里给出一张LINUX系统编程手册里面的一张图,其实python是一样的,红色箭头是本文涉及的函数

Paste_Image.png

八、方法使用举例

下面使用C和PYTHON分别实现将日期

2017-10-12 08:52:03(AM) CST 

转换分解时间,日历时间然后再减去3600秒转换回分解时间和可读时间的小程序,以熟悉使用方式

  • C程序:
/*************************************************************************
 > File Name: testtime.c
 > Author: gaopeng QQ:22389860 all right reserved
 > Mail: gaopp_200217@163.com
 > Created Time: Wed 01 Nov 2017 02:28:25 AM CST
************************************************************************/

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define MAXSIZE 99

int  rtime2ctime(/*in*/const char* r_time,/*out*/time_t* ctime)
{
       time_t i_ctime;
       struct tm tm_st;
       //此处根据固定测试转换为分解时间tm_st
       if ( !strptime(r_time,"%Y-%m-%d %X(%p) %z",&tm_st))
       {
               return -1;
       }

       //此处将分解时间tm_st转换为日历时间ctime
       if( (*ctime = mktime(&tm_st)) == -1)
       {
               return -1;
       }

       return 0;
}

int ctime2rtime(/*in*/time_t c_time)
{

       struct tm* tm_st = NULL;
       char time[MAXSIZE] ;
       //此处将日历时间转换为分解时间
       if((tm_st = localtime(&c_time)) == NULL)
       {
               return -1;
       }

       //此处将分解时间转换为可读时间
       if(strftime(time,MAXSIZE-1,"%Y-%m-%d %X(%p) %z",tm_st) == 0)
       {
               return -1;
       }

       printf("%s\n",time);
       return 0;
}

int main(void)
{
       char r_time[100] = "2017-10-12 07:52:03(AM) +0800";
       time_t ctime;

       if( rtime2ctime(r_time,&ctime) == -1 )
       {
               printf("rtime2ctime error\n");
               return -1;
       }

       printf("2017-10-12 07:52:03(AM) +0800 to calenlar time is:%ld\n",ctime);

       ctime = ctime - (time_t)3600;

       printf("after sub 1 hours time is:");

       if(ctime2rtime(ctime) == -1)
       {
               printf("ctime2rtime error\n");
       }

} 
  • python程序:
import time

def rtime2ctime(r_time):
    #此处根据固定测试可读时间转换为分解时间
    p_time = time.strptime(r_time,"%Y-%m-%d %X(%p) %z")
    #此处将分解时间tm_st转换为日历时间
    return time.mktime(p_time)

def ctime2rtime(ctime):
    #此处将日历时间转换为分解时间
    p_time = time.localtime(ctime)
    #此处将分解时间转换为可读时间
    r_time = time.strftime("%Y-%m-%d %X(%p) %z",p_time)
    return r_time

r_time="2017-10-12 06:52:03(AM) +0800"
ctime = rtime2ctime(r_time)
print("2017-10-12 06:52:03(AM) +0800 to calenlar time is:%d" %(ctime))
ctime = ctime - 3600
print("after sub 1 hours time is:%s" %(ctime2rtime(ctime))) 

他们的运行结果均是:

2017-10-12 06:52:03(AM) +0800 to calenlar time is:1507762323
after sub 1 hours time is:2017-10-12 05:52:03(AM) +0800 

作者微信:

微信.jpg

时间: 2024-09-17 05:47:22

LINUX C系统编程与PYTHON中的时间模块对比的相关文章

Python中使用select模块实现非阻塞的IO_python

Socket的英文原义是"孔"或"插座".作为BSD UNIX的进程通信机制,取后一种意思.通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.在Internet上的主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务.Socket正如其英文原意那样,像一个多孔插座.一台主机犹如布满各种插座的房间,每个插座有一个编号,有的插座提供220伏交流电, 有的提供110

在Python中使用zlib模块进行数据压缩的教程

  这篇文章主要介绍了在Python中使用zlib模块进行数据压缩的教程,是Python入门学习中的基础知识,需要的朋友可以参考下 Python标准模块中,有多个模块用于数据的压缩与解压缩,如zipfile,gzip, bz2等等.上次介绍了zipfile模块,今天就来讲讲zlib模块. zlib.compress(string[, level]) zlib.decompress(string[, wbits[, bufsize]]) zlib.compress用于压缩流数据.参数string指

Python中的zipfile模块使用详解

  这篇文章主要介绍了Python中的zipfile模块使用详解,zipfile模块是用来操作zip文件,需要的朋友可以参考下 zip文件格式是通用的文档压缩标准,在ziplib模块中,使用ZipFile类来操作zip文件,下面具体介绍一下: class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]]) 创建一个ZipFile对象,表示一个zip文件.参数file表示文件的路径或类文件对象(file-like object);参

Python中的日志模块logging

  这篇文章主要介绍了Python中的日志模块logging,包括Python下的日志级别以及模块内常用方法的使用,需要的朋友可以参考下 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4cpp,而在python中,我们不需要第三方的日志组件,因为它已经为我们提供了简单易用.且功能强大的日志模块:logging.logging模块支持将日志信息保存到不

在Python中使用mechanize模块模拟浏览器功能

  这篇文章主要介绍了在Python中使用mechanize模块模拟浏览器功能,包括使用cookie和设置代理等功能的实现,需要的朋友可以参考下 知道如何快速在命令行或者python脚本中实例化一个浏览器通常是非常有用的. 每次我需要做任何关于web的自动任务时,我都使用这段python代码去模拟一个浏览器. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import mechanize import cookielib # Br

在Python中编写数据库模块的教程

  这篇文章主要介绍了在Python中编写数据库模块的教程,本文代码基于Python2.x版本,需要的朋友可以参考下 在一个Web App中,所有数据,包括用户信息.发布的日志.评论等,都存储在数据库中.在awesome-python-app中,我们选择MySQL作为数据库. Web App里面有很多地方都要访问数据库.访问数据库需要创建数据库连接.游标对象,然后执行SQL语句,最后处理异常,清理资源.这些访问数据库的代码如果分散到各个函数中,势必无法维护,也不利于代码复用. 此外,在一个Web

Python中由于logging模块误用导致的内存泄露的解决方法

  Python中由于logging模块误用导致的内存泄露的解决方法         这篇文章主要介绍了解决Python中由于logging模块误用导致的内存泄露,针对由于过多的UDP连接所产生的问题,需要的朋友可以参考下 首先介绍下怎么发现的吧, 线上的项目日志是通过 logging 模块打到 syslog 里, 跑了一段时间后发现 syslog 的 UDP 连接超过了 8W, 没错是 8 W. 主要是 logging 模块用的不对 我们之前有这么一个需求, 就是针对每一个连接日志输出当前连接

在Python中使用glob模块查找文件路径的方法

  这篇文章主要介绍了在Python中使用glob模块查找文件路径的方法,使用模块其中的iglob方法实现起来非常简单,需要的朋友可以参考下 glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符:"*", "?", "[]"."*"匹配0个或多个字符;"?"匹配单个字符;"[]"匹配指定范围内的

给Python中的MySQLdb模块添加超时功能的教程

  这篇文章主要介绍了给Python中的MySQLdb模块添加超时功能的教程,timeout功能在服务器的运维当中非常有用,需要的朋友可以参考下 使用Python操作MySQL数据库的时候常使用MySQLdb这个模块. 今天在开发的过程发现MySQLdb.connect有些参数没法设置.通过这个页面我们可以看到在connect的时候,可以设置的option和client_flags和MySQL c api相比差不少. 一个很重要的参数 MYSQL_OPT_READ_TIMEOUT没法设置,这个参