Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Shell time operation and time difference calculation method

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The content of this article mainly focuses on how to analyze the time operation and time difference calculation method of Shell. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

Recently, when dealing with Shell scripts, I encountered a problem of processing time. The addition and subtraction of time, and the calculation of time difference.

one. Time addition and subtraction

The way to deal with it here is to convert the basic time into a timestamp, and then you need to add or change the time to seconds.

For example, 1990-01-01 01:01:01 plus 1 hour and 20 minutes

Treatment method:

a. Convert the base time to a timestamp

Time1=$ (date +% s-d '1990-01-01 01purl 01purl 01')

Echo $time1

631126861 [timestamp]

b. Turn the increased time into seconds

[root localhost ~] # time2=$ ((1 / 60 / 60 / 20 / 60))

[root localhost ~] # echo $time2

4800

c. The two times are added together to calculate the result time.

Time1=$ (($time1+$time2))

Time1=$ (date +% Y-%m-%d\% H:%M:%S-d "1970-01-01 UTC $time1 seconds")

Echo $time1

1990-01-01 02:21:01

two. Time difference calculation method

For example, the 11:11:11 time difference between 2010-01-01 and 2009-01-01

Principle: also convert to timestamp, and then calculate days, hours, minutes, seconds

Time1=$ (($(date +% s-d '2010-01-01')-$(date +% s-d '2009-01-01 11))

Echo time1

Change time1 / 60 seconds into minutes.

Supplementary note:

Shell single parenthesis operation symbol:

Axiom $(date)

Equivalent to: a=date

Double parenthesis operator:

Axiom $((1x 2))

Echo $a

Equivalent to:

A=expr 1 + 2

Thank you for your reading. I believe you have some understanding of "how to analyze Shell time operation and time difference calculation method". Go to practice quickly, if you want to know more related knowledge points, you can pay attention to the website! The editor will continue to bring you better articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report