在编写Linux Bash脚本时,经常会用到一些日期时间有关的命令,下面是我多年Shell编程中常用的函数,现在整理出来,希望起到抛砖引玉的作用。
附件包括三个文件:
datetime.sh 包含了Bash中关于日期时间操作的常用自定义函数
test_datetime.sh 用来展示datetime.sh中自定义函数的用法
test_datetime.txt 是test_datetime.sh的一次执行输出样本
执行命令:
./test_datetime.sh >test_datetime.txt
文件:datetime.sh
#!/bin/sh
# Copyright (c) 2010 codingstandards. All rights reserved.
# file: datetime.sh
# description: Bash中关于日期时间操作的常用自定义函数
# license: LGPL
# author: codingstandards
# email: codingstandards@gmail.com
# version: 1.0
# date: 2010.02.27
# usage: yesterday
# 昨天
# 比如今天是2010年2月27日,那么结果就是2010-02-26
yesterday()
{
date --date='1 day ago' +%Y-%m-%d
}
# usage: today
# 今天
# 比如今天是2010年2月27日,那么结果就是2010-02-27
today()
{
date +%Y-%m-%d
}
# usage: now
# 现在,包括日期和时间、纳秒
# 比如:2010-02-27 11:29:52.991774000
now()
{
date "+%Y-%m-%d %H:%M:%S.%N"
}
# usage: curtime
# 当前时间,包括日期和时间
# 比如:2010-02-27 11:51:04
curtime()
{
date '+%Y-%m-%d %H:%M:%S'
# 也可写成:date '+%F %T'
}
# usage: last_month
# 取上个月的年月
# 比如:2010-01
last_month()
{
date --date='1 month ago' '+%Y-%m'
}
# usage: last_month_packed
# 取上个月的年月
# 比如:201001
last_month_packed()
{
date --date='1 month ago' '+%Y%m'
}
# usage: first_date_of_last_month
# 取上个月的第一天
# 比如本月是2010年2月,那么结果就是2010-01-01
first_date_of_last_month()
{
date --date='1 month ago' '+%Y-%m-01'
}
# usage: last_date_of_last_month
# 取上个月的最后一天
# 比如当前是2010年2月,那么结果就是2010-01-31
last_date_of_last_month()
{
date --date="$(date +%e) days ago" '+%Y-%m-%d'
}
# usage: day_of_week
# 今天的星期
# day of week (0..6); 0 represents Sunday
day_of_week()
{
date +%w
}
# usage: last_hour
# 上个小时
# 比如:2010-02-27-10
# 适合处理log4j生成的日志文件名
last_hour()
{
date --date='1 hour ago' +%Y-%m-%d-%H
}
# usage: the_hour
# 当前的小时,为方便算术比较,结果不以0开头
# 比如:12
the_hour()
{
#date +%H # hour (00..23)
date +%k # hour ( 0..23)
}
# usage: the_minute
# 当前的分钟,为方便算术比较,结果不以0开头
# 比如:
the_minute()
{
MM=$(date +%M) # minute (00..59)
echo $[1$MM-100]
}
# usage: the_second
# 当前的秒数
# 比如:
the_second()
{
SS=$(date +%S) # second (00..60); the 60 is necessary to accommodate a leap second
echo $[1$SS-100]
}
# usage: the_year
# 当前的年份 year (1970...)
# 比如:2010
the_year()
{
date +%Y
}
# usage: the_month
# 当前的月份,为方便算术比较,结果不以0开头
# 比如:2
the_month()
{
M=$(date +%m) # month (01..12)
echo $[1$M-100]
}
# usage: the_date
# 当前的日期,为方便算术比较,结果不以0开头
# 比如:27
the_date()
{
date +%e # day of month, blank padded ( 1..31)
}
# usage: days_ago <n>
# 取n天前的日期
# 比如:days_ago 0就是今天,days_ago 1就是昨天,days_ago 2就是前天,days_ago -1就是明天
# 格式:2010-02-27
days_ago()
{
date --date="$1 days ago" +%Y-%m-%d
}
# usage: chinese_date_and_week()
# 打印中文的日期和星期
# 比如:2月27日 星期六
chinese_date_and_week()
{
WEEKDAYS=(星期日 星期一 星期二 星期三 星期四 星期五 星期六)
WEEKDAY=$(date +%w)
#DT="$(date +%Y年%m月%d日) ${WEEKDAYS[$WEEKDAY]}"
MN=1$(date +%m)
MN=$[MN-100]
DN=1$(date +%d)
DN=$[DN-100]
DT="$MN月$DN日 ${WEEKDAYS[$WEEKDAY]}"
echo "$DT"
}
# usage: rand_digit
# 随机数字,0-9
rand_digit()
{
S="$(date +%N)"
echo "${S:5:1}"
}
# usage: seconds_of_date [<date> [<time>]]
# 获取指定日期的秒数(自1970年)
# 比如:seconds_of_date "2010-02-27" 返回 1267200000
seconds_of_date()
{
if [ "$1" ]; then
date -d "$1 $2" +%s
else
date +%s
fi
}
# usage: date_of_seconds <seconds>
# 根据秒数(自1970年)得到日期
# 比如:date_of_seconds 1267200000 返回 2010-02-27
date_of_seconds()
{
date -d "1970-01-01 UTC $1 seconds" "+%Y-%m-%d"
}
# usage: datetime_of_seconds <seconds>
# 根据秒数(自1970年)得到日期时间
# 比如:datetime_of_seconds 1267257201 返回 2010-02-27 15:53:21
datetime_of_seconds()
{
date -d "1970-01-01 UTC $1 seconds" "+%Y-%m-%d %H:%M:%S"
}
# usage: leap_year <yyyy>
# 判断是否闰年
# 如果yyyy是闰年,退出码为0;否则非0
# 典型示例如下:
# if leap_year 2010; then
# echo "2010 is leap year";
# fi
# if leap_year 2008; then
# echo "2008 is leap year";
# fi
# 摘自脚本:datetime_util.sh (2007.06.11)
# 注:这个脚本来自网络,略有修改(原脚本从标准输入获取年份,现改成通过参数指定)
# Shell program to read any year and find whether leap year or not
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
leap_year()
{
# store year
yy=$1
isleap="false"
#echo -n "Enter year (yyyy) : "
#read yy
# find out if it is a leap year or not
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of isleap
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
isleap="true"
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of isleap
else
# it is a leap year
isleap="true"
fi
#echo $isleap
if [ "$isleap" == "true" ]; then
# echo "$yy is leap year"
return 0
else
# echo "$yy is NOT leap year"
return 1
fi
}
# usage: validity_of_date <yyyy> <mm> <dd>
# 判断yyyy-mm-dd是否合法的日期
# 如果是,退出码为0;否则非0
# 典型示例如下:
# if validity_of_date 2007 02 03; then
# echo "2007 02 03 is valid date"
# fi
# if validity_of_date 2007 02 28; then
# echo "2007 02 28 is valid date"
# fi
# if validity_of_date 2007 02 29; then
# echo "2007 02 29 is valid date"
# fi
# if validity_of_date 2007 03 00; then
# echo "2007 03 00 is valid date"
# fi
# 摘自脚本:datetime_util.sh (2007.06.11)
# 注:这个脚本来自网络,略有修改(原脚本从标准输入获取年月日,现改成通过参数指定)
# Shell program to find the validity of a given date
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
validity_of_date()
{
# store day, month and year
yy=$1
mm=$2
dd=$3
# store number of days in a month
days=0
# get day, month and year
#echo -n "Enter day (dd) : "
#read dd
#echo -n "Enter month (mm) : "
#read mm
#echo -n "Enter year (yyyy) : "
#read yy
# if month is negative (<0) or greater than 12
# then it is invalid month
if [ $mm -le 0 -o $mm -gt 12 ]; then
#echo "$mm is invalid month."
return 1
fi
# Find out number of days in given month
case $mm in
1) days=31;;
01) days=31;;
2) days=28 ;;
02) days=28 ;;
3) days=31 ;;
03) days=31 ;;
4) days=30 ;;
04) days=30 ;;
5) days=31 ;;
05) days=31 ;;
6) days=30 ;;
06) days=30 ;;
7) days=31 ;;
07) days=31 ;;
8) days=31 ;;
08) days=31 ;;
9) days=30 ;;
09) days=30 ;;
10) days=31 ;;
11) days=30 ;;
12) days=31 ;;
*) days=-1;;
esac
# find out if it is a leap year or not
if [ $mm -eq 2 ]; then # if it is feb month then only check of leap year
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of days
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
days=29
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of days
else
# it is a leap year
days=29
fi
fi
#echo $days
# if day is negative (<0) and if day is more than
# that months days then day is invaild
if [ $dd -le 0 -o $dd -gt $days ]; then
#echo "$dd day is invalid"
return 3
fi
# if no error that means date dd/mm/yyyy is valid one
#echo "$dd/$mm/$yy is a vaild date"
#echo "$yy-$mm-$dd is a valid date"
#echo "valid"
return 0
}
# usage: days_of_month <mm> <yyyy>
# 获取yyyy年mm月的天数,注意参数顺序
# 比如:days_of_month 2 2007 结果是28
days_of_month()
{
# store day, month and year
mm=$1
yy=$2
# store number of days in a month
days=0
# get day, month and year
#echo -n "Enter day (dd) : "
#read dd
#echo -n "Enter month (mm) : "
#read mm
#echo -n "Enter year (yyyy) : "
#read yy
# if month is negative (<0) or greater than 12
# then it is invalid month
if [ $mm -le 0 -o $mm -gt 12 ]; then
#echo "$mm is invalid month."
echo -1
return 1
fi
# Find out number of days in given month
case $mm in
1) days=31;;
01) days=31;;
2) days=28 ;;
02) days=28 ;;
3) days=31 ;;
03) days=31 ;;
4) days=30 ;;
04) days=30 ;;
5) days=31 ;;
05) days=31 ;;
6) days=30 ;;
06) days=30 ;;
7) days=31 ;;
07) days=31 ;;
8) days=31 ;;
08) days=31 ;;
9) days=30 ;;
09) days=30 ;;
10) days=31 ;;
11) days=30 ;;
12) days=31 ;;
*) days=-1;;
esac
# find out if it is a leap year or not
if [ $mm -eq 2 ]; then # if it is feb month then only check of leap year
if [ $((yy % 4)) -ne 0 ] ; then
: # not a leap year : means do nothing and use old value of days
elif [ $((yy % 400)) -eq 0 ] ; then
# yes, it's a leap year
days=29
elif [ $((yy % 100)) -eq 0 ] ; then
: # not a leap year do nothing and use old value of days
else
# it is a leap year
days=29
fi
fi
echo $days
}
文件:test_datetime.sh
#!/bin/sh # TODO: 注意根据datetime.sh的实际位置更改 . /opt/shtools/commons/datetime.sh echo "当前时间(date):$(date)" echo "昨天(yesterday):$(yesterday)" echo "今天(today):$(today)" echo "现在(now):$(now)" echo "现在(curtime):$(curtime)" echo "上月(last_month):$(last_month)" echo "上月(last_month_packed):$(last_month_packed)" echo "上月第一天(first_date_of_last_month):$(first_date_of_last_month)" echo "上月最后一天(last_date_of_last_month):$(last_date_of_last_month)" echo "今天星期几(day_of_week):$(day_of_week)" echo "上个小时(last_hour):$(last_hour)" echo "当前的小时(the_hour):$(the_hour)" echo "当前的分钟(the_minute):$(the_minute)" echo "当前的秒钟(the_second):$(the_second)" echo "当前的年份(the_year):$(the_year)" echo "当前的月份(the_month):$(the_month)" echo "当前的日期(the_date):$(the_date)" echo "前天(days_ago 2):$(days_ago 2)" echo "明天(days_ago -1):$(days_ago -1)" echo "后天(days_ago -2):$(days_ago -2)" echo "十天前的日期(days_ago 10):$(days_ago 10)" echo "中文的日期星期(chinese_date_and_week):$(chinese_date_and_week)" echo "随机数字(rand_digit):$(rand_digit)" echo "随机数字(rand_digit):$(rand_digit)" echo "自1970年来的秒数(seconds_of_date):$(seconds_of_date)" echo "自1970年来的秒数(seconds_of_date 2010-02-27):$(seconds_of_date 2010-02-27)" echo "自1970年来的秒数(seconds_of_date 2010-02-27 15:53:21):$(seconds_of_date 2010-02-27 15:53:21)" echo "自1970年来的秒数对应的日期(date_of_seconds 1267200000):$(date_of_seconds 1267200000)" echo "自1970年来的秒数对应的日期时间(datetime_of_seconds 1267257201):$(datetime_of_seconds 1267257201)" if leap_year 2010; then echo "2010年是闰年"; fi if leap_year 2008; then echo "2008年是闰年"; fi if validity_of_date 2007 02 03; then echo "2007 02 03 日期合法" fi if validity_of_date 2007 02 28; then echo "2007 02 28 日期合法" fi if validity_of_date 2007 02 29; then echo "2007 02 29 日期合法" fi if validity_of_date 2007 03 00; then echo "2007 03 00 日期合法" fi echo "2010年2月的天数(days_of_month 2 2010):$(days_of_month 2 2010)" echo "2008年2月的天数(days_of_month 2 2008):$(days_of_month 2 2008)"
文件:test_datetime.txt
当前时间(date):六 2月 27 15:58:28 CST 2010 昨天(yesterday):2010-02-26 今天(today):2010-02-27 现在(now):2010-02-27 15:58:28.734817000 现在(curtime):2010-02-27 15:58:28 上月(last_month):2010-01 上月(last_month_packed):201001 上月第一天(first_date_of_last_month):2010-01-01 上月最后一天(last_date_of_last_month):2010-01-31 今天星期几(day_of_week):6 上个小时(last_hour):2010-02-27-14 当前的小时(the_hour):15 当前的分钟(the_minute):58 当前的秒钟(the_second):28 当前的年份(the_year):2010 当前的月份(the_month):2 当前的日期(the_date):27 前天(days_ago 2):2010-02-25 明天(days_ago -1):2010-02-28 后天(days_ago -2):2010-03-01 十天前的日期(days_ago 10):2010-02-17 中文的日期星期(chinese_date_and_week):2月27日 星期六 随机数字(rand_digit):5 随机数字(rand_digit):9 自1970年来的秒数(seconds_of_date):1267257508 自1970年来的秒数(seconds_of_date 2010-02-27):1267200000 自1970年来的秒数(seconds_of_date 2010-02-27 15:53:21):1267257201 自1970年来的秒数对应的日期(date_of_seconds 1267200000):2010-02-27 自1970年来的秒数对应的日期时间(datetime_of_seconds 1267257201):2010-02-27 15:53:21 2008年是闰年 2007 02 03 日期合法 2007 02 28 日期合法 2010年2月的天数(days_of_month 2 2010):28 2008年2月的天数(days_of_month 2 2008):29
本文提供了一组实用的Bash脚本函数,用于处理Linux系统中的日期和时间。这些函数覆盖了从简单的日期获取到复杂的日期计算等多个方面,并附带了一个示例脚本来展示其使用方法。
2379

被折叠的 条评论
为什么被折叠?



