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

The calculation method of time difference in Shell time Operation

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

Share

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

This article mainly explains "the method of calculating the time difference of Shell time operation". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Shell time operation and time difference calculation method"!

1. 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 01 01 echo $time1631126861 [timestamp]

b. Turn the increased time into seconds

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

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 $time11990-01-01 02:21:01

Second, the calculation method of time difference

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:

The code is as follows:

Axiom $(date)

Equivalent to: a = `date`

Double parenthesis operator:

The code is as follows:

Axiom $((1x 2))

Echo $a

Equivalent to:

The code is as follows:

A = `expr 1 + 2`

At this point, I believe that everyone on the "Shell time operation and time difference calculation method" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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