Legal Date Format script

Source: Internet
Author: User
Tags month name

The most challenging of all the Legalizing input scripts of the date format script is the non-operational date. It aims to ensure that a given date exists in reality. If you ignore a leap year, this task is not difficult because the dates of each year are consistent. In this case, all we have to do is compare a given date with the number of days per month. However, in order to calculate the leap year, some additional logic is required. The following is a set of rules for calculating a leap year: 1. not a leap year 2. what can be divisible by 4 and 400 at the same time is a leap year 3. it can be divisible by four, but cannot be divisible by 400, but can be divisible by another 100, not a leap year 4. except for the above cases, all the four that can be divisible are the leap year code: 01validdate. sh02 #! /Bin/sh03 04 # validdate. sh -- the validity of a date, both to the leap year 05 06 exceedsDaysinMonth () 07 {08 # given a month, if the given day is less than or equal to the maximum number of days of the month, return # Otherwise, returns 110 11 case $ (echo $1 | tr '[: upper:] ''[: lower:]') in12 jan *) days = 31; feb *) days = 28; 13 mar *) days = 31; apr *) days = 30; 14 may *) days = 31; jun *) days = 30 ;; 15 jul *) days = 31; aug *) days = 31; 16 sep *) days = 30; oct *) days = 31; 17 nov *) days = 30; dec *) days = 31; 18 *) echo "$0: Unknown month name $1 "> & 2; exit 119 esac20 21 if [$2-lt 1-o $2-gt $ days]; then22 return 123 else24 return 0 # The number of days is valid 25 fi26} 27 28 isLeepYear () 29 {30 # if it is a leap year, the function returns 0; otherwise, returns 131 # the rule for determining the leap year is as follows: 4: 32 33 year = $134 35 if ["$ (year % 4)"-ne 0]; then36 return 137 elif ["$ (year % 400)"]; then38 return 039 elif ["$ (year % 100)"]; then40 return 141 else42 return 043 fi44} 45 46 # Start script main part 47 48 if [$ #-Ne 3]; then49 echo "Usage: $0 month/day"> & 250 echo "the correct input format is: august 3 1962 and 8 3 2002 "> & 251 exit 152 fi53 54 # normalization date, 55 56 newdate =" $ (normdate. sh "$ @") "57 58 if [$? -Eq 1]; then59 exit 1 # error status reported by normdate 60 fi61 62 month = "$ (echo $ newdate | cut-d \-f1)" # note, -d \ has two spaces between the backend and-f1. The backslash is used to escape the space 63 day = "$ (echo $ newdate | cut-d \-f2) "64 year =" $ (echo $ newdate | cut-d \-f3) "65 66 # since the date has been normalized, then test whether the value of the number of days is correct 67 68 if! ExceedsDaysinMonth $ month "$2"; then69 if ["$ month" = "Feb"-a "$2"-eq "29"]; then70 if! IsLeepYear $3; then71 echo "$0: $3 is not a leap year, so February has no 29 days. "> & 272 exit 173 fi74 else75 echo" $0: Incorrect days: $ month no $2 days. "> & 276 exit 177 fi78 fi79 80 echo" correct date: $ newdate "81 82 exit 0 run script: To run the script, you only need to give a date in the format of "month, day, and Year" in the command line. A month can be an abbreviation of three letters, a complete month or a number, but a year must be a four-digit number. Running result: 1validdate. sh August 3 19602 correct date: Aug 3 19603 validdate. sh 9 3 20014 correct date: Sep 3 20015 validdate. sh feb 29 20046 correct date: Feb 29 20047. /validdate. sh feb 29, 20068. /validdate. sh: Is 2006 not a leap year? So there were no 29 days in February. Analysis Script: similar to the idea in this script, we can also write a step on the legal time. The time format is in the 24-hour format and the am/pm suffix. Cut the value of time at the colon to ensure that the minute and second (if given) are between 0 and 60. If it is am/pm, check whether the first value is between 0 and 12, or is the 24-hour system between 0 and 24. (Fortunately, there are leap seconds in time and other small changes to maintain the calendar balance, so we can ignore them on a daily basis)

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.