星期六, 四月 29, 2006

[Debian] [FreeBSD] 学习下 date 命令的用法

由ChinaUnix 上的 一篇讨论 引起了看 date 命令的兴趣

在FreeBSD的实现上,因为可以用多个 -v 选项,这样可以很灵活的得到你要的日期,举个例子说,要得到2010年2月的最后一天,可以用 date -v10y -v3m -v1d -v-1d ,解释一下,就是取得2010年(-v10y)3月(-v3m)1日(-v1d)再减一天(-v-1d)的日期。这其中,数字后面的字符有固定的意义:ymwdHMS -> 年月周日时分秒

在Linux上,date的实现有些不一样,有个-d 的选项,可以用字符串来表示时间,这个字符串也很灵活:可以是年月日(20081002);也可以是英语词句的表达,如yesterday,last year;还可以是数字加时间单词如-1month(一个月前,这儿有趣的是,这个单词可以用单数,也可以用复数)。但是总体来说,似乎没有BSD上的灵活,想想表达下 "2010年2月的最后一天" 该怎么说?恐怕还要在自己脑子里转个弯弯。

date命令的输出格式,可以用百分符带一个字符表示日期中某个部分,应该都是来源于strftime

       %%     The % character.
       %a or %A
          The weekday name according to the current locale, in abbreviated
          form or the full name.
       %b or %B or %h
          The month name according to the current locale,  in  abbreviated
          form or the full name.
       %c     The date and time representation for the current locale.
       %C     The century number (0-99).
       %d or %e
          The day of month (1-31).
       %D     Equivalent  to  %m/%d/%y. (This is the American style date, very
          confusing to non-Americans, especially since %d/%m/%y is  widely
          used in Europe.  The ISO 8601 standard format is %Y-%m-%d.)
       %H     The hour (0-23).
       %I     The hour on a 12-hour clock (1-12).
       %j     The day number in the year (1-366).
       %m     The month number (1-12).
       %M     The minute (0-59).
       %n     Arbitrary whitespace.
       %p     The  locales equivalent of AM or PM. (Note: there may be none.)
       %r     The 12-hour clock time (using the locales AM or  PM).   In  the
          POSIX  locale equivalent to %I:%M:%S %p.  If t_fmt_ampm is empty
          in the LC_TIME part of the current locale then the behaviour  is
          undefined.
       %R     Equivalent to %H:%M.
       %S     The second (0-60; 60 may occur for leap seconds; earlier also 61
          was allowed).
       %t     Arbitrary whitespace.
       %T     Equivalent to %H:%M:%S.
       %U     The week number with Sunday the first day of  the  week  (0-53).
          The first Sunday of January is the first day of week 1.
       %w     The weekday number (0-6) with Sunday = 0.
       %W     The  week  number  with Monday the first day of the week (0-53).
          The first Monday of January is the first day of week 1.
       %x     The date, using the locales date format.
       %X     The time, using the locales time format.
       %y     The year within century (0-99).  When a century is not otherwise
          specified, values in the range 69-99 refer to years in the twen
          tieth century (1969-1999); values in the range  00-68  refer  to
          years in the twenty-first century (2000-2068).
       %Y     The year, including century (for example, 1991).

0 Comments:

发表评论

<< Home