- [Instructor] Getting and setting dates and delays.
In the previous video we've learned about grabbing
information about the terminal.
In this video we're going to learn
how to work with dates and time delays.
Many applications require printing dates
in different formats, setting date and time
and performing manipulations based on date and time.
Delays are commonly used to provide a wait time
such as one second during the program execution.
Scripting context such as monitoring a task
every five seconds demand the understanding
of writing delays in a program.
Dates can be printed in a variety of formats.
We can also set dates from the command line.
In Unix like systems dates are stored
as an integer, which denotes the number of seconds
since January the first, 1970 at midnight UTC.
This is called epoch or Unix time.
Let's see how to read dates and set them.
It's possible to read the dates in different formats
and also to set the date.
This can be accomplished with these steps.
You can read the date as.
The epoch time can be printed as.
We can find out epoch from a given formatted date string.
You can use dates in multiple date formats as input.
Usually you don't need to bother about the date
string format that you use if you're collecting
the date from a system log or any standard application
generated output.
Convert the date string into epoch as.
The minus, minus date option can be used
to provide a date string as input.
However, we can use any date formatting options
to print the output.
Feeding the input date from a string
can be used to find out the weekday given the date.
For example.
Use a combination of format strings
prefixed with plus as an argument for the date command
to print the date in the format of your choice.
For example.
We can set the date and time as.
For example.
Sometimes we need to check the time taken
by a set of commands.
We can display using this code.
An alternate method would be to use time script path
to get the time that it took to execute the script.
While considering dates and time epoch is defined
as the number of seconds overlapped
since midnight proleptic Coordinated Universal Time, UTC,
of January the first, 1970.
Not counting leap seconds.
Epoch time is very useful when you need to calculate
the difference between two dates or times.
You may find out the epoch times for two given time stamps
and take the difference between the epoch values.
Therefore you can find out the total number
of seconds between two dates.
To write a date format to get the output as required,
use this table.
Now we'll see some features of dates and delays.
Producing time intervals is very essential
when writing monitoring scripts that execute in a loop.
Let's see how to generate time delays.
Producing delays in a script.
To delay execution in a script
for a particular period of time
use sleep colon dollar sleepno underscore of
underscore seconds.
For example, the following script
counts from zero to 40 using tput and sleep.
In this preceding example a variable count
is initialized to zero and is incremented
on every loop execution.
The echo statement prints the text.
We use tput sc to store the cursor position.
On every loop execution we write the new count
in the terminal by restoring in a cursor
position for the number.
The cursor position is restored using tput rc.
This clears the text from the current cursor position
to the end of the line so that the older number
can be cleared and the count can be rewritten.
A delay of one second is provided in the loop
by using the sleep command.
Awesome, we've successfully learned
about getting and setting dates and delays.
In the next video we'll learn how to debug the script.