免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2101 | 回复: 2
打印 上一主题 下一主题

[日期时间] mktime 和 strftime 函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-03 10:46 |只看该作者 |倒序浏览
  1. [root@localhost ~]# awk 'BEGIN{a=mktime("2001 02 02 12 12 12");print a}'
  2. 981087132


  3. [root@localhost ~]# awk 'BEGIN{a=mktime("2001 02 02 12 12 12");print strftime("%c",a)}'
  4. Fri 02 Feb 2001 12:12:12 PM CST
复制代码
mktime设置的时间格式输出的结果怎么是981087132? 这个是1970 01 01 所相差的时间么?
           这个函数不是用来设置显示的时间格式么?

必须要配合strftime?

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2012-12-03 11:40 |只看该作者
回复 1# 力哥丶

$ man awk
GAWK(1)                        Utility Commands                        GAWK(1)

NAME
       gawk - pattern scanning and processing language

SYNOPSIS
       gawk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
       gawk [ POSIX or GNU style options ] [ -- ] program-text file ...

       pgawk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
       pgawk [ POSIX or GNU style options ] [ -- ] program-text file ...

  ...
   Time Functions
       Since one of the primary uses of AWK programs is processing  log  files
       that  contain time stamp information, gawk provides the following func‐
       tions for obtaining time stamps and formatting them.

       mktime(datespec)
                 Turns datespec into a time stamp of the same form as returned
                 by  systime().
   The datespec is a string of the form YYYY MM
                 DD HH MM SS[ DST].  The contents of the  string  are  six  or
                 seven numbers representing respectively the full year includ‐
                 ing century, the month from 1 to 12, the  day  of  the  month
                 from  1  to  31, the hour of the day from 0 to 23, the minute
                 from 0 to 59, and the second from 0 to 60,  and  an  optional
                 daylight  saving  flag.  The values of these numbers need not
                 be within the ranges specified; for example, an  hour  of  -1
                 means 1 hour before midnight.  The origin-zero Gregorian cal‐
                 endar is assumed, with year 0 preceding year 1  and  year  -1
                 preceding  year  0.   The  time is assumed to be in the local
                 timezone.  If the daylight saving flag is positive, the  time
                 is  assumed  to be daylight saving time; if zero, the time is
                 assumed to be standard time; and if negative  (the  default),
                 mktime()  attempts  to determine whether daylight saving time
                 is in effect for the specified time.  If  datespec  does  not
                 contain  enough  elements  or if the resulting time is out of
                 range, mktime() returns -1.

       strftime([format [, timestamp[, utc-flag]]])
                 Formats timestamp according to the specification  in  format.
                 If  utc-flag  is  present  and  is  non-zero or non-null, the
                 result is in UTC, otherwise the result is in local time.  The
                 timestamp  should  be  of  the  same form as returned by sys‐
                 time().  If timestamp is missing, the current time of day  is
                 used.   If  format is missing, a default format equivalent to
                 the output of date(1) is used.  See the specification for the
                 strftime() function in ANSI C for the format conversions that
                 are guaranteed to be available.

       systime() Returns the current time of day  as  the  number  of  seconds
                 since the Epoch (1970-01-01 00:00:00 UTC on POSIX systems).

   

论坛徽章:
0
3 [报告]
发表于 2012-12-03 14:15 |只看该作者
回复 2# jason680


    返回systime() 那这个就相当于1970-01-01到现在的时间差了?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP