16.3. Time / Date Commands 笔记

460阅读 0评论2014-01-05 wuxiaobo_2009
分类:LINUX

Time/date and timing

date

Simply invoked,dateprints the date and time tostdout. Where this command gets interesting is in its formatting and parsing options.
说白了就是一个显示: 日期+时间 的命令

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # Exercising the 'date' command

  3. echo "The number of days since the year's beginning is `date +%j`."
  4. # Needs a leading '+' to invoke formatting.
  5. # %j gives day of year.

  6. echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
  7. # %s yields number of seconds since "UNIX epoch" began,
  8. #+ but how is this useful?

  9. prefix=temp
  10. suffix=$(date +%s) # The "+%s" option to 'date' is GNU-specific.
  11. filename=$prefix.$suffix
  12. echo "Temporary filename = $filename"
  13. # It's great for creating "unique and random" temp filenames,
  14. #+ even better than using $$.   这个方法可以生成临时文件,比$$ 的方法好

  15. # Read the 'date

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # date-calc.sh
  3. # Author: Nathan Coulter
  4. # Used in ABS Guide with permission (

  5. MPHR=60 # Minutes per hour.
  6. HPD=24 # Hours per day.

  7. diff () {
  8.         printf '%s' $(( $(date -u -d"$TARGET" +%s) -
  9.                         $(date -u -d"$CURRENT" +%s)))
  10. # %d = day of month.
  11. } # 两个具体的日期之间相差多少秒


  12. CURRENT=$(date -u -d '2007-09-01 17:30:24' '+%F %T.%N %Z')
  13. TARGET=$(date -u -d'2007-12-25 12:30:00' '+%F %T.%N %Z')
  14. # %F = full date, %T = %H:%M:%S, %N = nanoseconds, %Z = time zone.
  15. # %F= 年-月-日     %T: 小时:分钟:秒    %Z:时区
  16. printf '\nIn 2007, %s ' \
  17.        "$(date -d"$CURRENT +
  18.         $(( $(diff) /$MPHR /$MPHR /$HPD / 2 )) days" '+%d %B')"
  19. # %B = name of month ^ halfway      -- eg. %b:1月 %B:一月
  20. printf 'was halfway between %s ' "$(date -d"$CURRENT" '+%d %B')"
  21. printf 'and %s\n' "$(date -d"$TARGET" '+%d %B')"

  22. printf '\nOn %s at %s, there were\n' \
  23.         $(date -u -d"$CURRENT" +%F) $(date -u -d"$CURRENT" +%T)
  24. DAYS=$(( $(diff) / $MPHR / $MPHR / $HPD ))
  25. CURRENT=$(date -d"$CURRENT +$DAYS days" '+%F %T.%N %Z')
  26. HOURS=$(( $(diff) / $MPHR / $MPHR ))
  27. CURRENT=$(date -d"$CURRENT +$HOURS hours" '+%F %T.%N %Z')
  28. MINUTES=$(( $(diff) / $MPHR ))
  29. CURRENT=$(date -d"$CURRENT +$MINUTES minutes" '+%F %T.%N %Z')
  30. printf '%s days, %s hours, ' "$DAYS" "$HOURS"
  31. printf '%s minutes, and %s seconds ' "$MINUTES" "$(diff)"
  32. printf 'until Christmas Dinner!\n\n'

  33. # Exercise:
  34. # --------
  35. # Rewrite the diff () function to accept passed parameters,
  36. #+ rather than using global variables.

点击(此处)折叠或打开

  1. The date command has quite a number of output options. For example %N gives the nanosecond portion of the current time. One interesting use for this is to generate random integers.

  2. date +%N | sed -e 's/000$//' -e 's/^0//'
  3.            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4. # Strip off leading and trailing zeroes, if present.
  5. # Length of generated integer depends on
  6. #+ how many zeroes stripped off.

  7. # 115281032
  8. # 63408725
  9. # 394504284

点击(此处)折叠或打开

  1. date +%j
  2. # Echoes day of the year (days elapsed since January 1).

  3. date +%k%M
  4. # Echoes hour and minute in 24-hour format, as a single digit string.



  5. # The 'TZ' parameter permits overriding the default time zone.
  6. date # Mon Mar 28 21:42:16 MST 2005
  7. TZ=EST date # Mon Mar 28 23:42:16 EST 2005
  8. # Thanks, Frank Kannemann and Pete Sjoberg, for the tip.


  9. SixDaysAgo=$(date --date='6 days ago')
  10. OneMonthAgo=$(date --date='1 month ago') # Four weeks back (not a
  11. OneYearAgo=$(date --date='1 year ago')

Please Man date for more information!


点击(此处)折叠或打开

  1. zdump

  2.     Time zone dump: echoes the time in a specified time zone.

  3.     bash$ zdump EST
  4.     EST Tue Sep 18 22:09:22 2001 EST
  5.         

  6. time

  7.     Outputs verbose timing statistics for executing a command.

  8.     time ls -l / gives something like this:

  9.     real 0m0.067s
  10.      user 0m0.004s
  11.      sys 0m0.005s





上一篇:Ctime Atime Mtime 及其 +N N -N 的含义剖析
下一篇:16.4. Text Processing Commands 学习笔记