Perl custom functions for Processing Dates

Source: Internet
Author: User
Tags erro
# Recently, I used to deal with time issues. I simply wrote several functions. #! /Usr/bin/perl-W #================================ ========================================================== ========## copyright (c) 2009, # All Rights Reserved. # Abstract: some common time processing functions # Author: 2009-12-15 xingwang # version: 0.01 #=================================================== ========================================================== = use strict; # ---------------------------- # & subtime (date, n) # Calculate the N days before or after a day # parameter: date, N days before (N), or N days after (-N) #--------------------- --------- Sub subtime {My ($ date, $ number) =@_; Use http: date; my $ n = str2time ($ date ); $ n + = 86400 * $ number; my ($ S, $ mm, $ H, $ D, $ M, $ y) = (localtime ($ n, 2, 3, 4, 5]; $ Y + = 1900; $ M ++; if ($ date = ~ //) {Return sprintf ("% 4D-% 02d-% 02d % 02d: % 02d: % 02d", $ y, $ M, $ D, $ H, $ mm, $ S);} else {return sprintf ("% 4D-% 02d-% 02d", $ y, $ M, $ D );}} print & submonth ($ argv [0], $ argv [1]), "\ n"; # ---------------------------- # & submonth (date, n) # Calculate the first n months of a day or the last n months # parameter: date, previous n months (n) or next n months (-N) # -------------------------- sub submonth {My ($ date, $ number) = @ _; Use http: date; my $ n_temp = str2time ($ date); my ($ m_temp, $ y_temp) = (loc Altime ($ n_temp) [1900]; $ y_temp + =; $ m_temp ++; my % hs_monthdays = ('1' => '31 ', '3' => '31', '5' => '31', '7' => '31', '8' => '31 ', '10' => '31', '12' => '31', '4' => '30', '6' => '30 ', '9' => '30', '11' => '30 '); $ hs_monthdays {'2'} = ($ y_temp % 4 = 0 & $ y_temp % 100! = 0) | $ y_temp % 400 = 0? 29: 28; my $ n = str2time ($ date); my $ monthdays = $ hs_monthdays {$ m_temp}; $ n-= 86400 * $ monthdays * $ number; my ($ S, $ mm, $ H, $ D, $ M, $ y) = (localtime ($ n) [0, 1, 2, 3, 4, 5]; $ y ++ = 1900; $ M ++; if ($ date = ~ //) {Return sprintf ("% 4D-% 02d-% 02d % 02d: % 02d: % 02d", $ y, $ M, $ D, $ H, $ mm, $ S);} else {return sprintf ("% 4D-% 02d-% 02d", $ y, $ M, $ D );}} # ---------------------------- # & nowtime () # current time # ------------------------------ sub nowtime {My ($ S, $ min, $ H, $ D, $ M, $ Y) = (localtime (Time () [0, 1, 2, 3, 4, 5]; $ Y + = 1900; $ M ++; return sprintf ("% 4D-% 02d-% 02d % 02d: % 02d: % 02d", $ y, $ M, $ D, $ H, $ min, $ s );}#------------------------------ # & Nowdate () # current date # ------------------------------ sub nowdate {My ($ D, $ M, $ y) = (localtime (Time () [3, 4, 5]; $ y ++ = 1900; $ M ++; return sprintf ("% 4D-% 02d-% 02d", $ y, $ M, $ D );} # -------------------------------------------------- # & formatdate () # format date # input format: # yyyy-mm-dd, yyyy-m-D, yyyymmdd, yyyy/mm/DD, # yyyy/M/d, yyyy-mm, yyyy-M, yyyymm, yyyy/ mm, yyyy/M # ------------------------------------------------------ sub fo Rmatdate {my $ date = shift; my $ type = shift; $ date = ~ /^ (\ D {4}) [-\/] (\ D {2} | \ D) [-\/] (\ D {2} | \ D) $/g; # yyyy-mm-dd yyyy/mm/DD $ date = ~ /^ (\ D {4}) (\ D {2}) (\ D {2}) $/; # yyyymmdd $ date = ~ /^ (\ D {4}) (\ D {2}) $/; # yyyymm $ date = ~ /^ (\ D {4}) [-\/] (\ D {2} | \ D) $/g; # yyyy-mm yyyy/mmdie "format date erro: the date you input ($ date) is not correct. "Unless ($1 or $2); if ($ type eq 'yyymmm') {return sprintf (" % 04d % 02d ", $1, $2 );} elsif ($ type eq 'yyyymmdd' and $3) {return sprintf ("% 04d % 02d % 02d", $1, $2, $3 );} elsif ($ type eq 'yyyy-mm-dd' and $3) {return sprintf ("% 04d-% 02d-% 02d", $1, $2, $3);} elsif ($ type eq 'yyyy-mm') {return spri Ntf ("% 04d-% 02d", $1, $2);} elsif ($ type eq 'alldays' or $ type eq 'endday ') {my $ hs_monthdays = {'01 '=> '31', '03' => '31', '05' => '31 ', '07 '=> '31', '08' => '31', '10' => '31', '12' => '31 ', '04 '=> '30', '06' => '30', '09' => '30', '11' => '30 '}; $ hs_monthdays-> {'02'} = ($ 1% 4 = 0 & $ 1% 100! = 0) | $ 1% 400 = 0? 29: 28; return $ hs_monthdays-> {$2};} else {die "format date erro: return date (date = $ date, type = $ type) is not correct. "}}
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.