Universal date functions

Source: Internet
Author: User
Universal date functions-general Linux technology-Linux programming and kernel information. For more information, see the following. #! /Bin/sh

#### Author: RUIFOX
#### Function: Obtain the date before or after any day of a date

#### Parameter 1 is? Get help

If ["$1" = "? "]
Then
Echo "function: get any day before or after a date
Usage: date_g [-B |-a] [n (days)] [Now (yyyymmdd)]
-B indicates the date before.
-A indicates the next date. The default value is-a 1, that is, the next day.
N indicates the number of days in the interval. The default value is 1.
Now indicates the base point date. The default value is the current date.
For example, date_g-a 365 20050210, and the result is 20060210"
Exit
Fi

#### Parameter 2 indicates the number of days separated. The default value is 1 day.
Pre = 1
If ["$2 "! = ""]
Then
Pre = $2
Fi

#### Parameter 3 indicates the base point date. The default value is the current system date.
#### If you want to enter a specific base point Date, parameter 2 cannot be omitted
If ["$3 "! = ""]
Then
Len = 'expr length "$3 "'
If [$ Len! = 8]
Then
Echo "invalid base point Date [$ Len]! "
Exit
Fi
Year = 'echo $3 | awk' {print substr ($, 4 )}''
Month = 'echo $3 | awk' {print substr ($, 2 )}''
Day = 'echo $3 | awk' {print substr ($, 2 )}''
DateP = $3
Else
Month = 'date + % m'
Day = 'date + % d'
Year = 'date + % y'
DateP = 'date + '% Y % m % d''
Fi

#### Parameter 1 indicates counting forward or backward
If ["$1" = "-a"]
Then
Day = 'expr $ day + $ pre'
Ss = ""
Else
Day = 'expr $ day-$ pre'
Ss = "before"
Fi

#### Function mon_max_day to obtain the maximum number of days per month
#### Parameter 1: number of months (1-12)
#### Output: Maximum number of days displayed
Mon_max_day ()
{
Day = 0
If [$1-gt 0-a $1-lt 13]
Then
Case $1 in
1 | 01 | 3 | 03 | 5 | 05 | 7 | 07 | 8 | 08 | 10 | 12) day = 31 ;;
4 | 04 | 6 | 06 | 9 | 09 | 11) day = 30 ;;
2 | 02)
If ['expr $ year % 4'-eq 0]; then
If ['expr $ year % 400 '-eq 0]; then
Day = 29
Elif ['expr $ year % 100 '-eq 0]; then
Day = 28
Else
Day = 29
Fi
Else
Day = 28
Fi ;;
Esac
Fi
Printf $ day
}

#### Start the main program
Max = 'mon _ max_day $ month'

#### Forward
While [$ day-le 0]
Do
Month = 'expr $ month-1'
If [$ month-eq 0]
Then
Month = 12
Year = 'expr $ year-1'
Fi
Max = 'mon _ max_day $ month'
Day = 'expr $ day + $ Max'
Done

#### Backward view
While [$ day-gt $ Max]
Do
Day = 'expr $ day-$ Max'
Month = 'expr $ month + 1'
If [$ month-eq 13]
Then
Month = 1
Year = 'expr $ year + 1'
Fi
Max = 'mon _ max_day $ month'
Done


DateA = 'printf "% 02d % 02d % 02d" $ year $ month $ Day'
# Echo "the date of the base point is [$ DateP], and the date of [$ Pre] day $ Ss is [$ DateA]"
Echo "$ DateA"
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.