I need to do date calculations in a shell script. Unfortunately my operating system (SunOS) does not provide a very handy date function: it does not support the -d option which is exactly what I need.
Roaming on the web to find an alternate solution i found something that looks to be powerful enough with ksh93 printf builtin function. It supports syntaxes like that:
yesterday=${ printf "%(%Y%m%d)T" yesterday; }
friday=${ printf "%(%Y%m%d)T" "3 days ago"; }
In my case, i need to calculate 2 days before a calculated date, in my understanding it should be written like that (mydate is formatted as "YYYYMMDD"):
dayinthepast=${ printf "%(%Y%m%d)T" "3 days before $mydate"; }
or
dayinthepast=${ printf "%(%Y%m%d)T" "$mydate - 3 days"; }
But it does not work.
The more surprising is that the second syntax is recognized but it takes the minus as a plus and add 3 days to the given date.
I have read a useful blog covering the ksh93 printf builtin syntax but it does not cover my case. I'm giving the link here (thanks for the author): ksh93-date-manipulation
Any help would be appreciated.
Thanks
this worked for me on AIX 7.1:
$ ksh93
$ MY_DATE=20210601
$ printf "%(%Y%m%d)T" "${MY_DATE} 3 days ago"
20210529
got the hint from the link - ksh93-date-manipulation - there's a paragraph there that mentions "23 days ago"
Related
I have two dates in forms like: YYYYMMDDHH and want to calculate the differences (in hours) between these two dates. For example
start_date=1996010100
end_date=1996010122
which stands for two dates: 1996-01-01 00:00:00 and 1996-01-01 22:00:00. I want to use date to calculate the difference in hours, the result shall be 22 hours. I tried with
START=$(date -d "$start_date" +"%s")
END=$(date -d "$end_date" +"%s")
HOURS=$(bc -l <<< "($END - $START) / 3600")
but it failed...
So how can I do this? Thanks!
For performance reasons we want to limit the number of sub-process calls we need to invoke:
use bash substring functionality to convert inputs into usable date/time strings
use bash math to replace bc call
bash substring functionality to break the inputs into a usable date/time format, eg:
# convert to usable date/time format:
$ start_date=1996010100
$ echo "${start_date:0:4}-${start_date:4:2}-${start_date:6:2} ${start_date:8:2}:00:00"
1996-01-01 00:00:00
# convert to epoch/seconds:
$ start=$(date -d "${start_date:0:4}-${start_date:4:2}-${start_date:6:2} ${start_date:8:2}:00:00" +"%s")
$ echo $start
820476000
Applying to ${end_date} and using bash math:
$ end_date=1996010122
$ end=$(date -d "${end_date:0:4}-${end_date:4:2}-${end_date:6:2} ${end_date:8:2}:00:00" +"%s")
$ echo $end
820555200
$ hours=$(( (end - start) / 3600))
$ echo $hours
22
This leaves us with 2 sub-process calls ($(date ...)). While other languages/tools (awk, perl, etc) can likely speed this up a bit, if you need to store the result in a bash variable then you're looking at needing at least 1 sub-process call (ie, hours=$(awk/perl/??? ...)).
If performance is really important (eg, needing to perform this 1000's of times) take a look at this SO answer that uses a fifo, background date process and io redirection ... yeah, a bit more coding and a bit more convoluted but also a bit faster for large volumes of operations.
busybox date can do the trick
start_date=1996010100
end_date=1996010122
START=$(busybox date -D "%Y%m%d%H" -d "$start_date" +"%s")
END=$(busybox date -D "%Y%m%d%H" -d "$end_date" +"%s")
HOURS=$(bc -l <<< "scale=0;($END - $START) / 3600")
echo $HOURS
If it's possible for you to use a more fully-featured scripting language like Python, it'll provide a much more pleasant and understandable date parsing experience, and is probably installed by default (datetime is also a standard Python library)
Structured with shell vars
start_date=1996010100
end_date=1996010122
python -c "import datetime ; td = datetime.datetime.strptime('${end_date}', '%Y%m%d%H') - datetime.datetime.strptime('${start_date}', '%Y%m%d%H') ; print(int(td.total_seconds() / 3600))"
Structured to read dates and format code from stdin
echo '%Y%m%d%H' 1996010100 1996010122 | python -c "import datetime,sys ; fmt, date_start, date_end = sys.stdin.read().strip().split() ; td = datetime.datetime.strptime(date_end, fmt) - datetime.datetime.strptime(date_start, fmt) ; print(int(td.total_seconds() / 3600))"
Should work with both Python 3 and Python 2.7
format codes available here (1989 C standard)
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
which stands for two dates: 1996-01-01 00:00:00
So convert it to that form if it stands for it.
start_date=1996010100
start_date=$(sed -E 's/(....)(..)(..)(..)/\1-\2-\3 \4:00:00/' <<<"$start_date")
start=$(date -d "$start_date" +"%s")
and the same with end.
the most simple way is to install "dateutils" using this command
sudo apt-get install dateutils
Run these commands to get the difference in seconds:
dateutils.ddiff -i '%Y%m%d%H%M%S' 20200817040001 20210817040101
output:
31536060s
next step: Simply divide by 86400 to get the number of days or similarly for hours and minutes :)
I want to know the minute of the day in a bash shell script, and at the moment I can only think to do this by piping two date commands with the hour of day and minute of hour to bc in this way:
mod=$(echo 60*$(date +%H)+$(date +%M) | bc)
echo $mod
This works, but is very clunky and not very elegant, is there a nicer way? I didn't see an option of minute of day in the date command.
I'm using bash version
4.4.20(1)-release
As a more efficient approach (with bash 4.4 or later), albeit not necessarily a shorter one:
printf -v dateMath '%( (10#%H*60)+10#%M )T' -1
mod=$(( dateMath ))
...as a less-efficient one-liner (but still much faster than using date and bc), you could also write this as:
mod=$(( $(printf '%( (10#%H*60)+10#%M )T' -1) ))
I am trying to get the time difference between two dates as given below in Bash script. However I am not successful
head_info: 05-31-2017:04:27:37
tail_info: 05-31-2017:04:30:57
the problem is that after Reformation above time and while trying to calculate in seconds due to space, it is ignoring time.
This is my script:
fm_head_info=(${head_info:6:4}"-"${head_info:0:2}"-"${head_info:3:2}" \
"${head_info:11:8})
fm_tail_info=(${tail_info:6:4}"-"${tail_info:0:2}"-"${tail_info:3:2}" \
"${tail_info:11:8})
$ fm_head_info
-bash: 2017-05-31: command not found
Thank you
Let's define your shell variables:
$ tail_info=05-31-2017:04:30:57
$ head_info=05-31-2017:04:27:37
Now, let's create a function to convert those dates to seconds-since-epoch:
$ date2sec() { date -d "$(sed 's|-|/|g; s|:| |' <<<"$*")" +%s; }
To find the time difference between those two date in seconds:
$ echo $(( $(date2sec "$tail_info") - $(date2sec "$head_info") ))
200
As written above, this requires bash (or similar advanced shell) and GNU date. In other words, this should work on any standard Linux. To make this work on OSX, some changes to the date command will likely be necessary.
How it works
Starting with the innermost command inside the function date2sec, we have:
sed 's|-|/|g; s|:| |' <<<"$*"
In the argumnet to the function, this replaces all - with / and it replaces the first : with a space. This converts the the dates from the format in your input to one that the GNU date function will understand. For example:
$ sed 's|-|/|g; s|:| |' <<<"05-31-2017:04:30:57"
05/31/2017 04:30:57
With this form, we can use date to find seconds-since-epoch:
$ date -d "05/31/2017 04:30:57" +%s
1496230257
And, for the head_info:
$ date -d "05/31/2017 04:27:37" +%s
1496230057
Now that we have that, all that is left is to subtract the times:
$ echo $(( 1496230257 - 1496230057 ))
200
Your immediate issue is the inclusion of erroneous (...) surrounding your string indexed assignment and your questionable quoting. It looks like you intended:
fm_head_info="${head_info:6:4}-${head_info:0:2}-${head_info:3:2} ${head_info:11:8}"
fm_tail_info="${tail_info:6:4}-${tail_info:0:2}-${tail_info:3:2} ${tail_info:11:8}"
Your use of string indexes is correct, e.g.
#!/bin/bash
head_info=05-31-2017:04:27:37
tail_info=05-31-2017:04:30:57
fm_head_info="${head_info:6:4}-${head_info:0:2}-${head_info:3:2} ${head_info:11:8}"
fm_tail_info="${tail_info:6:4}-${tail_info:0:2}-${tail_info:3:2} ${tail_info:11:8}"
echo "fm_head_info: $fm_head_info"
echo "fm_tail_info: $fm_tail_info"
Example Use/Output
$ bash headinfo.sh
fm_head_info: 2017-05-31 04:27:37
fm_tail_info: 2017-05-31 04:30:57
You can then do something similar with the differences in date -d "$var" +%s as John shows in his answer to arrive at the time difference. Note, string indexes are limited to bash, while a sed solution (absent the herestring) would be portable on all POSIX shells.
I want to write a bash script that will run on a given but process data with next days date, My current approach is to get the unix time stamp and add a days worth of seconds to it, but I cant get it working, and haven't yet found what I'm looking for online.
Here's what I've tried, I feel like the problem is that its a string an not a number, but I dont know enough about bash to be sure, is this correct? and how do I resolve this?
today="$(date +'%s')"
tomorrow="$($today + 86400)"
echo "$today"
echo "$tomorrow"
If you have gnu-date then to get next day you can just do:
date -d '+1 day'
Some of the answers for this question depend on having GNU date installed. If you don't have GNU date, you can use the built-in date command with the -v option.
The command
$ date -v+1d
returns tomorrow's date.
You can use it with all the standard date formatting options, so
$ date -v+1d +%Y-%m-%d
returns tomorrow's date in the format YYYY-MM-DD.
$(...) is command substitution. You're trying to run $today + 86400 as a command.
$((...)) is arithmetic expansion. This is what you want to use.
tomorrow=$(( today + 86400 ))
Also see http://mywiki.wooledge.org/ArithmeticExpression for more on doing arithmetics in the shell.
I hope that this will solve your problem here.
date --date 'next day'
Set your timezone, then run date.
E.g.
TZ=UTC-24 date
Alternatively, I'd use perl:
perl -e 'print localtime(time+84600)."\n"'
echo $(date --date="next day" +%Y%m%d)
This will output
20170623
I like
input_date=$(date '+%Y-%m-%d')
tomorrow_date=$(date '+%Y-%m-%d' -d "$input_date + 1 day")
echo "$tomorrow_date"
It returns the date of the day after $input_date, in format YYYY-MM-DD
You can try below
#!/bin/bash
today=`date`
tomorrow=`date --date="next day"`
echo "$today"
echo "$tomorrow"
I am writing a shell script that needs to do some date string manipulation. The script should work across as many *nix variants as possible, so I need to handle situations where the machine might have the BSD or the GNU version of date.
What would be the most elegant way to test for the OS type, so I can send the correct date flags?
EDIT:
To clarify, my goal is to use date's relative date calculation tools which seem distinct in BSD and GNU.
BSD example
date -v -1d
GNU example
date --date="1 day ago"
You want to detect what version of the date command you're using, not necessarily the OS version.
The GNU Coreutils date command accepts the --version option; other versions do not:
if date --version >/dev/null 2>&1 ; then
echo Using GNU date
else
echo Not using GNU date
fi
But as William Pursell suggests, if at all possible you should just use functionality common to both.
(I think the options available for GNU date are pretty much a superset of those available for the BSD version; if that's the case, then code that assumes the BSD version should work with the GNU version.)
Use portable flags. The standard is available here
For the particular problem of printing a relative date, it is probably easier to use perl than date:
perl -E 'say scalar localtime(time - 86400)'
(note that this solution utterly fails on 23 or 25 hour days, but many perl solutions are available to address that problem. See the perl faq.)
but you could certainly use a variation of Keith's idea and do:
if date -v -1d > /dev/null 2>&1; then
DATE='date -v -1d'
else
DATE='date --date="1 day ago"'
fi
eval "$DATE"
or just
DATE=$(date -v -1d 2> /dev/null) || DATE=$(date --date="1 day ago")
Another idea is to define a function to use:
if date -v -1d > /dev/null 2>&1; then
date1d() { date -v -1d; }
else
date1d() { date --date='1 day ago'; }
fi
It is recommended to use feature-based defection rather than the os-based or platform-based detection.
Following sample code show how it works:
if is-compatible-date-v; then
date -v -1d
elif is-compatible-date-d; then
date --date="1 day ago"
fi
function is-compatible () {
"$#" >/dev/null 2>&1
}
function is-compatible-date-v () {
is-compatible date -v +1S
}
function is-compatible-date-d () {
is-compatible date -d '1 second'
}
I was having the exact need to writing some shellcode to deal with date, and hoping the code be able to run on both BSD and GNU version of date.
I end up with a set of scripts that provides a uniform interface for both BSD and GNU version of date.
Example:
Follow command will output a date that is 21 days later than 2008-10-10, and it works with both BSD and GNU version of date.
$ xsh x/date/adjust +21d 2008-10-10
2008-10-31
The scripts can be found at:
https://github.com/xsh-lib/core/tree/master/functions/date
It's a part of a library called xsh-lib/core. To use them you need both repos xsh and xsh-lib/core, I list them below:
https://github.com/alexzhangs/xsh
https://github.com/xsh-lib/core
Hope this will help people with the same need.
To just answer your question, probably "uname -o" is what you want. In my case it's "GNU/Linux". You can decide for yourself if detecting the os type is worthless or not.