Linux computing time difference

Source: Internet
Author: User
Linux computing time difference in order to maintain computing, you need to know the time difference between multiple slave nodes to obtain different time points. The following code does not check the input value and the current value in the same month. However, if we boast the month's backtracking data, there will be a big problem. # Datevariablesfor... linux computing time difference in order to calculate retention, you need to know the time difference between multiple slave nodes to get different time points. The following code does not check the input value and the current value in the same month. However, if we boast the month's backtracking data, there will be a big problem. # Date variables forif [-n "$1"]; then TODAY = 'date-d "" + "% Y % m % d" 'base = $ ($ TODAY-$1 )) elif [-z "$ {DAYAGO1}"]; then base = 0 fiarr = ($ base $ ($ base + 1) $ ($ base + 2 )) $ ($ base + 8) $ ($ base + 31) dag = (0 1 2 8 31) for I in {0 .. 4 }; do echo $ I echo $ {dag [$ I]} echo $ {arr [$ I]} export "DAYAGO $ {dag [$ I]}" = 'date-d" $ {arr [$ I]} days ago "+" % Y % m % d "'done to fix bugs, you can refer to the English article: http://stackoverflow.com/questions/3385003/shell-script-to-get-difference-in-two-datesThere 'S a solution that almost works: use the % s date format of GNU date, which prints the number of seconds since. these can be subtracted to find the time difference between two dates. echo $ (date-d 2010-06-01 + % s)-$ (date-d 2010-05-15 + % s)/86400) But the following displays 0 in some locations: echo $ (date-d 2010-03-29 + % s)-$ (date-d 2010-03-28 + % s)/86400) Because of daylight savings time, there are only 23 hours between those times. you need to add at least one hour (and at most 23) to be safe. echo $ (date-d 2010-03-29 + % s)-$ (date-d 2010-03-28 + % s) + 43200)/86400 )) or you can tell date to work in a timezone without DST. echo $ (date-u-d 2010-03-29 + % s)-$ (date-u-d 2010-03-28 + % s)/86400 )) (POSIX says to call the reference timezone is UTC, but it also says not to count leap seconds, so the number of seconds in a day is always exactly 86400 in a GMT + xx timezone .) the final result is almost perfect: # Date variables forif [-n "$1"]; then TODAY = 'date-d "" + "% Y % m % d" 'backday = $1 base = $ (date-d $ TODAY + % s) -$ (date-d $ BACKDAY + % s) + 43200)/86400) elif [-z "$ {DAYAGO1}"]; then base = 1 fiarr = ($ base) $ ($ base + 1) $ ($ base + 7 )) $ ($ base + 30) dag = (1 2 8 31) for I in {0 .. 3 }; do echo $ {dag [$ I]} echo $ {arr [$ I]} export "DAYAGO $ {dag [$ I]}" = 'date-d "$ {arr [$ I]} days ago "+" % Y % m % d "'done shell converts the time calculation to the second for the subtraction operation NOW = 'date +" % Y-% m- % d % H: % M: % S "'lastline =$ (ls-lt *" $ v_DIRNAME "| line | awk '{print $6, $7, $8 }') # obtain the file's last time: 14:30:00 Sys_data = 'date-d "$ CURTIME" + % s' # convert the current time to Linux time In_data = 'date-d "$ LASTLINE" + % s 'interval = 'expr $ Sys_data-$ In_data '# calculate the difference of two time periods
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.