Is there an option of date function?
How do I get LOAD_TEST_START to be 2 minutes forward?
How do I get LOAD_TEST_END to be 2 minutes back?
LOAD_TEST_START=$(date -u +%FT%TZ)
LOAD_TEST_END=$(date -u +%FT%TZ)
This answer helped me and works on the linux-version of date.
LOAD_TEST_START=$(date +%FT%TZ -d "2 minutes ago")
LOAD_TEST_END=$(date +%FT%TZ -d "2 minutes")
On macos/FreeBSD one can use
LOAD_TEST_START=$(date -v+2M +%FT%TZ)
LOAD_TEST_END=$(date -v-2M +%FT%TZ)
Not sure if this is what you want, but you can get the current epoch value and then add/subtract 120 seconds (2 minutes) from it and convert the results back to date.
dateval=`date +%s`
echo "orig "`date -d #$dateval +%FT%T%Z`
start=`expr $dateval + 120`
end=`expr $dateval - 120`
LOAD_TEST_START=`date -d #$start +%FT%T%Z`
LOAD_TEST_END=`date -d #$end +%FT%T%Z`
echo "start "$LOAD_TEST_START
echo "end "$LOAD_TEST_END
Related
Well I'm trying to get the difference between two dates in Seconds.MilliSeconds The dates are in Zulu format
I have tried these two approaches doesn't work out for me
ms1=$(date -d '2022-04-22T03:47:56.744551446Z' +%N)
ms2=$(date -d '2022-04-22T03:47:57.095419744Z' +%N)
msdiff=$((ms1 - ms2))
echo "$msdiff"
$ dateutils.ddiff 2022-04-22T03:47:56.744551446Z 2022-04-22T03:47:57.095419744Z -f '%N'
0s
Is there any better way to get the difference in Seconds.MilliSeconds In linux for Z format time zones
Suggesting
ms1=$(date -d '2022-04-22T03:47:56.744551446Z' +%s.%N)
ms2=$(date -d '2022-04-22T03:47:57.095419744Z' +%s.%N)
msdiff=$(awk "BEGIN{print $ms2 - $ms1}")
echo "msdiff=$msdiff"
Output:
msdiff=0.350868
Operating System: Red Hat Enterprise Linux Server 7.2 (Maipo)
I want to round the time to the nearest 5 minutes, only up, not down, for example:
08:09:15 should be 08:10:00
08:11:26 should be 08:15:00
08:17:58 should be 08:20:00
I have been trying with:
(date -d #$(( (($(date +%s) + 150) / 300) * 300)) "+%H:%M:%S")
This will round the time but also down (08:11:18 will result in 08:10:00 and not 08:15:00)
Any idea how i can achieve this?
You may use this utility function for your rounding up:
roundDt() {
local n=300
local str="$1"
date -d #$(( ($(date -d "$str" '+%s') + $n)/$n * $n)) '+%H:%M:%S'
}
Then invoke this function as:
roundDt '08:09:15'
08:10:00
roundDt '08:11:26'
08:15:00
roundDt '08:17:58'
08:20:00
To trace how this function is computing use -x (trace mode) after exporting:
export -f roundDt
bash -cx "roundDt '08:11:26'"
+ roundDt 08:11:26
+ typeset n=300
+ typeset str=08:11:26
++ date -d 08:11:26 +%s
+ date -d #1535631300 +%H:%M:%S
08:15:00
GNU date can calculate already. It is explained in the manual in the chapter "Relative items in date strings". So you need just one date call.
d=$(date +%T) # get the current time
IFS=: read h m s <<< "$d" # parse it in hours, minutes and seconds
inc=$(( 300 - (m * 60 + s) % 300 )) # calculate the seconds to increment
date -d "$d $inc sec" +%T # output the new time with the offset
Btw: +%T is the same as +%H:%M:%S.
I use these to judge Sunday in shell:
week=`date -d $day +%w`
month=`date -d "-1 day ago $day" '+%d'`
if [ "$week" = "0" ];then
if [ "$month" = "01" ];then
But when $day is 20171103 , It was regarded as Sunday , then the 20171105 was missed.
Then I run "date -d 20171105 +%w" in the server ,it shows 0, and the if caluse was true. Then I check the Time Zone , Time Zone was right ,+0800
Are there any better methods in shell that can solve this problem? More safer and robust to judge whether the string like YYYYMMDD is Sunday?
Why not just use date %u for day of the week?
There are plenty of options in the man page.
I'm trying to do a simple for{} loop and I encountered problem with assign a variable inside loop. I will try to explain it on example:
DIFF=(`date +%s -d 20120203`-`date +%s -d 20120126`)/86400 # Difference between 2 dates.
echo $DIFF
end_date_change='20120126'
for i in {1..$DIFF} #Difference between 2 dates inside loop
do
end_date_change=$(end_date_change -d "-1 day") # Now, after every loop I would like to decrease date
echo $end_date_change
done
So the output should looks:
20120126
20120127
20120128
20120201
20120202
20120203
Anyone? Thanks in advance
In a Linux script: I have a file that has two time entries for each message within the file. A 'received time' and a 'source time'. there are hundreds of messages within the file.
I want to calculate the elapsed time between the two times.
2014-07-16T18:40:48Z (received time)
2014-07-16T18:38:27Z (source time)
The source time is 3 lines after the received time, not that it matters.
info on the input data:
The input has a lines are as follows:
TimeStamp: 2014-07-16T18:40:48Z
2 lines later: a bunch of messages in one line and within each line, multiple times is:
sourceTimeStamp="2014-07-16T18:38:27Z"
If you have GNU's date (not busybox's), you can give difference in seconds with:
#!/bin/bash
A=$(date -d '2014-07-16T18:40:48Z' '+%s')
B=$(date -d '2014-07-16T18:38:27Z' '+%s')
echo "$(( A - B )) seconds"
For busybox's date and ash (modern probably / BusyBox v1.21.0):
#!/bin/ash
A=$(busybox date -d '2014-07-16 18:40:48' '+%s')
B=$(busybox date -d '2014-07-16 18:38:27' '+%s')
echo "$(( A - B )) seconds"
you should be able to use date like this (e.g.)
date +%s --date="2014-07-16T18:40:48Z"
to convert both timestamps into a unix timestamp. Getting the time difference between them is then reduced to a simple subtraction.
Does this help?
I would use awk. The following script searches for the lines of interest, converts the time value into a UNIX timestamp and saves them in the start, end variables. At the end of the script the difference will get calculated and printed:
timediff.awk:
/received time/ {
"date -d "$1" +%s" | getline end
}
/source time/ {
"date -d "$1" +%s" | getline start
exit
}
END {
printf "%s seconds in between", end - start
}
Execute it like this:
awk -f timediff.awk log.file
Output:
141 seconds in between