Arithmetic operation on time variable in c-shell - shell

I need help using time in the c-shell
I want to know how much time it took to execute a script,so i want to do in the following way
1.set start_time=time
2 script part
3.set end_time=time
4. set diff=end_time-start_time
5.echo "It took $diff seconds"
but i couldn't get the time value using any command.
could any one suggest a command to read the time value in c-shell

I think you want the "date" command, with the format to give you raw seconds:
# start_time = `date +%s`
script part
# end_time = `date +%s`
# diff = $end_time - $start_time
echo "It took $diff seconds"

Related

Iterate through 2 dates using bash script on mac

I am trying to create a loop to iterate through 2 dates.
I have this so far, however it is going back in time. I want it to start with the start_date and end with the end_date.
I've tried to swap them around but it doesn't like that and adds +1day infinitely. Any help would be appreciated.
#!/usr/bin/env bash
end_date="21-12-21"
start_date="21-11-20"
# Set a counter variable
counter=0
#Increase the counter to get back in time
while [ "$end_date" != "$start_date" ]; do
end_date=$(date -v -${counter}d '+%y-%m-%d')
echo $end_date
counter=$((counter + 1))
done
This is what it produces
21-12-23
21-12-22
21-12-21
21-12-20
21-12-19
21-12-18
21-12-17
21-12-16
21-12-15
21-12-14
21-12-13
21-12-12
21-12-11
21-12-10
21-12-09
21-12-08
21-12-07
21-12-06
21-12-05
21-12-04
21-12-03
21-12-02
21-12-01
21-11-30
21-11-29
21-11-28
21-11-27
21-11-26
21-11-25
21-11-24
21-11-23
21-11-22
21-11-21
21-11-20
I'm also running this on a mac which seems to be quite fussy with the date formatting. This is the only way I have found it to work so far.

Difference in seconds between user input and current date

I need difference between two dates in seconds. I input datetime in format "YYYY-MM-DD HH:MM" and then calculate difference in seconds, but the run_time is in UTC timezone and current_time is calculated from system timezone.
#!/bin/bash
run_time=$1
run_time=$(date -d "$run_time" +"%s")
current_time="$(date +"%s")"
echo $run_time
echo $current_time
echo "$(($run_time-$current_time))"
How to retrieve absolute value of seconds between current datetime and another one?
Edit: actually that script has worked after restart of computer. No clue why previously it showed up different results. I have not changed anything in settings, nor computer time.
Right #Jetchisel, based on your comment a sample script could look like:
#!/bin/bash
STARTTIME_UTC=$1
STARTTIME_UTC=$(date -u -d "${STARTTIME_UTC}" +"%s")
echo "CURRENT DATE: $(date)"
CURRENTTIME="$(date +"%s")"
echo
echo "STARTTIME_UTC: ${STARTTIME_UTC} sec since"
echo "CURRENTIME: ${CURRENTTIME} sec since"
echo
echo "RUNTIME: $((${CURRENTTIME}-${STARTTIME_UTC})) sec"
As per original question
I input datetime in format "YYYY-MM-DD HH:MM"
the value of input time seconds in ${STARTTIME_UTC} will be set to zero (:00) whereby the value of current time seconds in ${CURRENTIME} will be set to what the clock provides. I.e., if it is 18 seconds before the next minute and the runtime would be almost zero, the script will report a difference of 42 seconds.

Difference between times using date function MACOSX

Calculate duration in HH:MM:SS format using difference in start and end time.
# Time Arithmetic
TIME1="00:30:20"
TIME2="00:30:50"
# Convert the times to seconds from the Epoch
SEC1=`date -j -f '%T' $TIME1 "+%s"`
#echo $SEC1
SEC2=`date -j -f '%T' $TIME2 "+%s"`
#echo $SEC2
# Use expr to do the math, let's say TIME1 was the start and TIME2 was the finish
DIFFSEC=`expr ${SEC2} - ${SEC1}`
#echo $DIFFSEC
echo Start ${TIME1}
echo Finish ${TIME2}
echo Took ${DIFFSEC} seconds.
# And use date to convert the seconds back to something more meaningful
result=`date -r $DIFFSEC "+%T"`
echo Result ${result}
Expected : Result 00:30:30 00:00:30(corrected)
Actual : Result 05:30:30
EDIT Actual TIME1 and TIME2 will be coming as params, and intension here is not to calculate time elapsed. It is a sample code i have used to demonstrate the issue. Strangely when TIME1=05:30:20 and TIME2=05:30:50 then also Result is : 05:30:30
Corrected Subject.
I finally settled with below, thanks anyways.
result=$(printf '%02d:%02d:%02d' $(($DIFFSEC/3600)) $(($DIFFSEC%3600/60)) $(($DIFFSEC%60)))

Error in bash time comparison

I have a bash script that creates a file with a timestamp as the name. Once some time passes, it is supposed to pick up that file and do something with it. I want it to pick it up after two hours, but for some reason, it is picking it up after 57 minutes (and 6 seconds). Can anyone point me to an error in my logic or assumptions?
Here are the details:
I have a variable set to 2 hours (7200 seconds):
SERVICE_DURATION=${SERVICE_DURATION:-7200} # seconds
I am setting the filename equal to the Unix timestamp concatenated with nanoseconds:
active_name=`date +%s%N`
echo "${1}" >> ${ACTIVE_DIR}/${active_name}
I then loop forever until the time is right:
while true
do
for fa in ${ACTIVE_DIR}/*
do
if [ $(basename ${fa}) -le $(($(date +%s%N) - ${SERVICE_DURATION} * 1000000000)) ]
then
exec 6< "${fa}"
read old_port <&6
read old_host <&6
read old_config <&6
exec 6<&-
logger -p daemon.info "Recycling port ${old_port}."
start_remote_service "${old_port}"
stop_remote_service "${old_port}" "${old_host}" "${old_config}" "${fa}"
sleep 2
fi
done
sleep 30
done
I can't see what is wrong with this. The filename ($(basename ${fa})) shouldn't be less than the current time minus the specified duration in nanoseconds ($(($(date +%s%N) - ${SERVICE_DURATION} * 1000000000))) until the duration has passed.
In order to keep the script from constantly checking, there is a sleep 30 at the end of the loop, so it could be that the time is somewhere between 56:36 and 57:06.
Any help would be appreciated. Thanks.

C shell doing arithmetic operations with large numbers

First of all: sorry for using c shell, blame my company not me. I hate the damn thing as much as most of you do now (at first I was like, hey this ain't so bad).
I am trying to subtract large numbers obtained from time stamps. Here is what I am trying:
set curTime = `date +%s%N`
#... some stuff
#curTime = `date +%s%N` - $curTime #get the diff
echo "time taken: $curTime"
However I guess the numbers are too big - before I tried with just seconds and it worked fine. Here's what I see in the log:
#curMilli = 1349996279792995000 - 1349996279170458000
#curMilli: Command not found.
As I said I do the exact same thing with date +%s and it's fine, so I'm assuming it's something about the largeness of the numbers.
How can I do this? Thanks a lot.
The article http://en.wikipedia.org/wiki/Bc_programming_language has a short section "Using bc in shell scripts". A test:
set curTime = `/bin/date +%s%N`
/bin/sleep 2
set prevTime = $curTime
set curTime = `/bin/date +%s%N`
set diff = `echo "$curTime - $prevTime;" | /usr/bin/bc`
echo $diff
will give (with the digits after the initial 20 variable):
2016204108
P.s: I wish I could vote you up twice for "I hate the damn thing as much as most of you do now (at first I was like, hey this ain't so bad)."

Resources