Date, sleep, and usleep commands, datesleepusleep

Source: Internet
Author: User
Tags usleep

Date, sleep, and usleep commands, datesleepusleep
1.1 date command

Date is used to obtain and set the Operating System Time, And hwclock is used to obtain the hardware time.

Date has an option "-d", which can be used to describe when to obtain the time. The description method is very open, but the keyword "now" cannot be used, others include 3 days ago, 3 days ago, 3 days ago, yesterday, And next Monday.

Set the display format of the date command in Linux: date [+ format], where"+"Obtains the format from the previous time.For example, date-d "yesterday" + "% Y" obtains the year of yesterday.

The format is as follows.

 

Symbol

Meaning

Description

Year

% Y

Year (last two)

Last two digits of year (00 .. 99)

% Y

Year

Year

Month

% M

Month

Month (01 .. 12)

 

 

 

Day

% J

Zhongtian

Day of year (001. 366)

% D

Yuzhong

Day of month (for example, 01)

% W

Zhou Zhongtian

Day of week (0 .. 6); 0 is Sunday

% U

Zhou Zhongtian

Day of week (1 .. 7); 1 is Monday

Week

% U

Mid-week (00-53)

Week number of year with Sunday as first day

% W

Mid-week (00-53)

Week number of year with Monday as first day

Hour

% H

Hour (24 hour)

Hour (00 .. 23)

% I

Hour (12 hour)

Hour (01 .. 12)

Minute

% M

Minute

Minute (00 .. 59)

Seconds

% S

Seconds

Second (00 .. 60)

% N

Nanoseconds

Ns of current minute

% S

Seconds

Total number of seconds from to the current time

Complete

Format

% T

Full time

Time; same as % H: % M: % S

% D

Complete Date Format

Date; same as % m/% d/% y

% F

Complete Date Format

Date; same as % Y-% m-% d

Special

Format

% N

Line feed

A newline

% T

Tab key

A tab

[Root @ xuexi ~] # Date + % F2016-09-25 [root @ xuexi ~] # Date + "% F % T" # Use double quotation marks or quotation marks to separate spaces on 10:48:34 [root @ xuexi ~] # Date + "% Y-% m-% d % H: % M: % S" 10:47:49

The date command can be used to calculate the latency (time difference between two time points ). If you want to calculate latency in seconds, you can use % s. However, if you want to calculate latency in milliseconds, microseconds, or even nanoseconds, the date result must be calculated and converted. The following is the second-level time difference calculation method. For details about the second-level time difference calculation method, see: millisecond-level time difference calculation and microsecond-level time difference calculation.

#!/bin/bashstart_time=$(date +"%s")find / -type f -name "*.db" &>/dev/nullend_time=$(date +"%s")time_diff=$(( start_time - end_time ))echo $time_diff
1.2 sleep and usleep

In shell, the sleep command is often used to specify the sleep time. sleep means that the current process enters sleep state. For example:

sleep 5

Sleep is measured in seconds by default. Therefore, sleep is used for 5 seconds. To sleep in milliseconds or microseconds, you can use decimal places. For example:

sleep 0.5

Sleep for half a second.

In addition, there is a dedicated microsecond-level sleep command usleep. For example:

usleep 1000

Sleep is 1000 microseconds, that is, 1 ms.

 

Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprinted please indicate the source: Success!

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.