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.
说白了就是一个显示: 日期+时间 的命令
-
#!/bin/bash
-
# Exercising the 'date' command
-
-
echo "The number of days since the year's beginning is `date +%j`."
-
# Needs a leading '+' to invoke formatting.
-
# %j gives day of year.
-
-
echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
-
# %s yields number of seconds since "UNIX epoch" began,
-
#+ but how is this useful?
-
-
prefix=temp
-
suffix=$(date +%s) # The "+%s" option to 'date' is GNU-specific.
-
filename=$prefix.$suffix
-
echo "Temporary filename = $filename"
-
# It's great for creating "unique and random" temp filenames,
-
#+ even better than using $$. 这个方法可以生成临时文件,比$$ 的方法好
-
-
# Read the 'date
-
#!/bin/bash
-
# date-calc.sh
-
# Author: Nathan Coulter
-
# Used in ABS Guide with permission (
-
-
MPHR=60 # Minutes per hour.
-
HPD=24 # Hours per day.
-
-
diff () {
-
printf '%s' $(( $(date -u -d"$TARGET" +%s) -
-
$(date -u -d"$CURRENT" +%s)))
-
# %d = day of month.
-
} # 两个具体的日期之间相差多少秒
-
-
-
CURRENT=$(date -u -d '2007-09-01 17:30:24' '+%F %T.%N %Z')
-
TARGET=$(date -u -d'2007-12-25 12:30:00' '+%F %T.%N %Z')
-
# %F = full date, %T = %H:%M:%S, %N = nanoseconds, %Z = time zone.
-
# %F= 年-月-日 %T: 小时:分钟:秒 %Z:时区
-
printf '\nIn 2007, %s ' \
-
"$(date -d"$CURRENT +
-
$(( $(diff) /$MPHR /$MPHR /$HPD / 2 )) days" '+%d %B')"
-
# %B = name of month ^ halfway -- eg. %b:1月 %B:一月
-
printf 'was halfway between %s ' "$(date -d"$CURRENT" '+%d %B')"
-
printf 'and %s\n' "$(date -d"$TARGET" '+%d %B')"
-
-
printf '\nOn %s at %s, there were\n' \
-
$(date -u -d"$CURRENT" +%F) $(date -u -d"$CURRENT" +%T)
-
DAYS=$(( $(diff) / $MPHR / $MPHR / $HPD ))
-
CURRENT=$(date -d"$CURRENT +$DAYS days" '+%F %T.%N %Z')
-
HOURS=$(( $(diff) / $MPHR / $MPHR ))
-
CURRENT=$(date -d"$CURRENT +$HOURS hours" '+%F %T.%N %Z')
-
MINUTES=$(( $(diff) / $MPHR ))
-
CURRENT=$(date -d"$CURRENT +$MINUTES minutes" '+%F %T.%N %Z')
-
printf '%s days, %s hours, ' "$DAYS" "$HOURS"
-
printf '%s minutes, and %s seconds ' "$MINUTES" "$(diff)"
-
printf 'until Christmas Dinner!\n\n'
-
-
# Exercise:
-
# --------
-
# Rewrite the diff () function to accept passed parameters,
-
#+ rather than using global variables.
-
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.
-
-
date +%N | sed -e 's/000$//' -e 's/^0//'
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
# Strip off leading and trailing zeroes, if present.
-
# Length of generated integer depends on
-
#+ how many zeroes stripped off.
-
-
# 115281032
-
# 63408725
-
# 394504284
-
date +%j
-
# Echoes day of the year (days elapsed since January 1).
-
-
date +%k%M
-
# Echoes hour and minute in 24-hour format, as a single digit string.
-
-
-
-
# The 'TZ' parameter permits overriding the default time zone.
-
date # Mon Mar 28 21:42:16 MST 2005
-
TZ=EST date # Mon Mar 28 23:42:16 EST 2005
-
# Thanks, Frank Kannemann and Pete Sjoberg, for the tip.
-
-
-
SixDaysAgo=$(date --date='6 days ago')
-
OneMonthAgo=$(date --date='1 month ago') # Four weeks back (not a
-
OneYearAgo=$(date --date='1 year ago')
Please Man date for more information!
-
zdump
-
-
Time zone dump: echoes the time in a specified time zone.
-
-
bash$ zdump EST
-
EST Tue Sep 18 22:09:22 2001 EST
-
-
-
time
-
-
Outputs verbose timing statistics for executing a command.
-
-
time ls -l / gives something like this:
-
-
real 0m0.067s
-
user 0m0.004s
-
sys 0m0.005s
阅读(471) | 评论(0) | 转发(0) |