bash中与时间相关的处理

#!/bin/sh

# Copyright (c) 2010 codingstandards. All rights reserved.
# file: datetime.sh
# description: Bash中关于日期时间操作的常用自定义函数
# license: LGPL
# author: codingstandards
# email: [email protected]
# version: 1.0
# date: 2010.02.27
# modified by [email protected] on 201709202300 


#
# date 命令的一个扩展是 -d 选项,该选项非常有用:
# 使用这个功能强大的选项,通过将日期作为引号括起来的参数提供,您可以快速地查明一个特定的日期。
# -d 选项还可以告诉您,相对于当前日期若干天的究竟是哪一天,从现在开始的若干天或若干星期以后,或者过去。
# 通过将这个相对偏移使用引号括起来,作为 -d 选项的参数,就可以完成这项任务。
#  date -d '-100 days' (100天以前的日期)
#

get_date_of_yesterday()
{
    # 2017-09-19
    date --date='1 day ago' +%Y-%m-%d
}

get_date_of_today()
{
    # 2017-09-20
    date +%Y-%m-%d
}

get_curtime()
{
    # 也可写成:date '+%F %T'; 2017-09-20 23:00:56
    date '+%Y-%m-%d %H:%M:%S'
}

get_last_month()
{
    # 2017-08
    date --date='1 month ago' '+%Y-%m'
}

get_last_month_packed()
{
    # 201708
    date --date='1 month ago' '+%Y%m'
}

get_first_day_of_last_month()
{
    # 2017-08-01 
    date --date='1 month ago' '+%Y-%m-01'
}

get_last_day_of_last_month()
{
    # 2017-08-31
    date --date="$(date +%e) days ago" '+%Y-%m-%d'
}

get_day_of_today()
{
    # 3
    date +%w
}

get_last_hour()
{
    # 2017-09-20-22
    date --date='1 hour ago' +%Y-%m-%d-%H
}

get_hour()
{
    #date +%H   # hour (00..23)
    date +%k    # hour ( 0..23)
}

get_minute()
{
    MM=$(date +%M)  # minute (00..59)
    echo $[1$MM-100]
}

get_second()
{
    SS=$(date +%S)  # second (00..60); the 60 is necessary to accommodate a leap  second
    echo $[1$SS-100]
}

get_year()
{
    date +%Y
}

get_month()
{
    M=$(date +%m) # month (01..12)
    echo $[1$M-100]
}

get_date()
{
    date +%e    # day of month, blank padded ( 1..31)
}

get_days_ago()
{
    date --date="$1 days ago" +%Y-%m-%d # days_ago 0就是今天,days_ago 1就是昨天,days_ago 2就是前天,days_ago -1就是明天
}


get_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 [ [
# 获取指定日期的秒数(自1970年)
# 比如:seconds_of_date "2010-02-27" 返回 1267200000
get_seconds_of_date()
{
 if[ "$1" ]; then
 date -d "$1" +%s
 else
 date +%s
 fi
}

# usage: date_of_seconds
# 根据秒数(自1970年)得到日期
# 比如:date_of_seconds 1267200000 返回 2010-02-27
get_date_of_seconds()
{
 date -d "1970-01-01 UTC $1 seconds" "+%Y-%m-%d"
}

# usage: datetime_of_seconds
# 根据秒数(自1970年)得到日期时间
# 比如:datetime_of_seconds 1267257201 返回 20100227
get_datetime_of_seconds()
{
 date -d "1970-01-01 UTC $1 seconds" "+%Y%m%d"
}

# usage: leap_year
# 判断是否闰年
# 如果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
# 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.
# -------------------------------------------------------------------------
is_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)) -ne0 ] ; then
 :# not a leap year : means do nothing and use old value of isleap
 elif [ $((yy % 400)) -eq0 ] ; then
 # yes, it's a leap year
 isleap="true"
 elif [ $((yy % 100)) -eq0 ] ; 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是否合法的日期 # 如果是,退出码为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 # 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 ( # 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 ( # 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 # 获取yyyy年mm月的天数,注意参数顺序 # 比如:days_of_month 2 2007 结果是28 get_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 ( # 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 }

你可能感兴趣的:(bash笔记)