BASH date command: can't re-combine date and time - linux

Linux v2.4/Bash v3.2/GNU utils/date command version 5.0
I'm struggling with the date command. In a BASH application, the user can set date and time separately, resulting in separate variables for date and time. Further on, these variables are re-combined but this appears not be palatable for the date command: I get a different date back. Time is the same, however. Testing code:
#!/bin/bash
dnow1="$(date)"
echo "1 $dnow1" # --> Sat Sep 14 16:31:48 EDT 2013
#split date and time
dldate="$(date -d "$dnow1" +"%d-%m-%Y")"
echo "2 $dldate" # --> 14-09-2013
dltime="$(date -d "$dnow1" +"%H:%M:%S")"
echo "3 $dltime" # --> 16:31:48
#try to re-combine date and time
string="${dldate} ${dltime}"
echo "4 $string" # --> 14-09-2013 16:31:48
dnow2="$(date -d "$string")"
echo "5 $dnow2" # --> Thu Mar 5 16:31:48 EST 2020
I must be missing something here. Can anyone enlighten me? Thanks!
Note:
I'm working an original XBOX that has few/low resources so there's no room for other solutions like Python. I'm a 'bashist' anyway so it must be BASH!
Edit: corrected time format. Thanks Mat.
As to "$(....)" I have made it a habit to double quote wherever possible.

When getting your date use this format instead:
#split date and time
dldate="$(date -d "$dnow1" +"%Y-%m-%d")"

From GNU date manual
The output of the date command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items like ‘IST’. When using date to generate a date string intended to be parsed later, specify a date format that is independent of language and that does not use time zone items other than ‘UTC’ and ‘Z’.

First of all using -d won't work in 14-09-2013 fashion, you can easily set date and time with one command and put it into variable. eg, just try this below on shell and then you can put into shell script.
date --date="Feb 2 2014 13:12:10"
Sun Feb 2 13:12:10 PST 2014

Related

Change Date Format in OSX Command

I am storing a Curl command output in a variable which comes as a date. (any previous date)
date=`curl ...`
The output is:
03-22-2021
Now I want to change this date format to below
March 22, 2021
I am running below commands:
currDate="03-22-2021"
formattedDate=`date -d "${currDate}" +%B %d, %Y`
echo $formattedDate
But I am getting this error
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
is it possible to use such a date format? How to do it?
I have resolved my above question as per below for OSX
currDate="03-22-2021"
formattedDate=`date -jf "%m-%d-%Y" "${currDate}" +"%B %d, %Y"`
echo $formattedDate
Output is:-
March 22, 2021
is it possible to use such a date format?
No.
How to do it?
Convert the format to something GNU date can eat. Then pass it.
I typically look at man date:
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even
"next Thursday". [...]
I recommend to use YYYY-MM-DD hh-mm-ss format and add also timezone information.
tmp=03-22-2021
date -d "${tmp:6:4}-${tmp:0:2}-${tmp:3:2} 12:00:00 UTC" "+%B %d, %Y"
Do not use backticks ` . Use $(..) instead.
Check your scripts with http://shellcheck.net
The format argument to date has to be one argument, not +%B %d, %Y three.
For explanation of ${tmp:5:4} expansion see variable expansion section in bash manual.
For anything more complicated with dates in shell, I recommend dateutils package with strptime command.
in OSX Command
On OSX install GNU date or install dateutils. The BSD date available on OSX is not capable of reading user input.

Shell script to convert a date string to specific timestamp format

I have a date time string like this 17-Mar-2020.22:22 -0300.I want to get the output as 2020-03-17 22:12 .
The timezone offset is causing an issue as without that I am able to convert
For example DATE=17-Mar-2020.22:22
date -jf "%d-%b-%Y.%H:%M" $DATE "+date \"%Y-%m-%d %H:%M\""
But this timezone offset is causing an issue.Can someone please help?
Because the input is in an unusual format, it needs to be first formatted to a format date can eat. I use the format indicated in man page "2004-02-29 16:21:42" or "Sun, 29 Feb 2004 16:21:42 -0800". GNU date allows for very flexible input format - don't trust it.
Below I used sed to match the input and reformat it.
input='17-Mar-2020.22:22 -0300'
input2="$(sed 's/\([0-9]*\)-\([^-]*\)-\([^\.]*\)\.\([0-9]*\):\([0-9]*\)/\1 \2 \3 \4:\5:00/' <<<"$input")"
# input2="17 Mar 2020 22:22:00 -0300"
date --date="$input2" +"%Y-%m-%d %H:%M"
# 2020-03-18 02:22
date -jf
GNU date differs a lot from BSD date. They are completely different. BSD date supports the -j and -f options, GNU date does not.
There are dateutils and they include strptime utility that allows to do what you wanted to do with date:
strptime -e -f "%Y-%m-%d %H:%M\n" -i "%d-%b-%Y.%H:%M %Z" "17-Mar-2020.22:22 -0300"

Use linux date command in cronjob

I have a problem using the linux 'date' command within a cronjob.
Linux version: CentOS Linux 7 (Core)
What i need is the date of 9 days ago as a parameter to my test.sh script.
Today is 10-08-2016, the job that needs to be executed is:
/scripts/test.sh 2016-08-01
My code in crontab:
DATEVAR=$(date +%F --date="9 days ago")
0 12 * * tue ~/scripts/test.sh $($DATEVAR)
So the linux command is 'date +%F --date="9 days ago"' but i need this to be executed and set as parameter.
What it does now is run the script with as parameter '$(date':
~/scripts/test.sh $(date
I have tried setting the DATEVAR with the following things without succes:
DATEVAR='date +%F --date="9 days ago"'
DATEVAR=date +%F --date="9 days ago"
DATEVAR=$(date +%F --date="9 days ago")
DATEVAR=(shell date +%F --date="9 days ago")
Does anyone know if this is possible and how my DATEVAR can be set with the result of executing the 'date' command?
This should work:
0 12 * * tue _DV=`date +\%F --date="9 days ago"`; ~/scripts/test.sh $_DV
Unfortunately, there are several issues to overcome here. First, the fact that cron now supports "environment settings" can be very misleading. Folks with shell script experience might easily assume that the full power of the shell can be used here -- it cannot. These settings are as dumb as they come: they are strict verbatim replacements. In my mind, a better moniker would be "placeholder assignment."
Second, the date string you wish to utilize is somewhat complex. Specifically, it contains an embedded quoted string ("9 days ago") and a special crontab character (surprise!): the '%' in the expression '+%F'. Cron replaces '%' with newlines -- a nice feature but surprising if you're unaware or forget about it. And quotes don't survive the assignment phase.
Knowing this, an alternative to the above entry that uses "placeholder assignment" is:
DT1 = date +%F
DT2 = 9 days ago
0 12 * * tue _DV=`$DT1 --date="$DT2"`; ~/scripts/test.sh $_DV
Here we're capturing pieces that we want to be directly substituted without any interpolation.
There -- two solutions for the price of one!
I am not sure you can do it inside a cron job.
A simple workaround is to wrap your call to test.sh into another script without argument. In it, simply write:
DATEVAR=$(date +%F --date="9 days ago")
~/scripts/test.sh $($DATEVAR)
An call this one in your cronjob.

Find Difference in timestamps in seconds

I have 2 files with timestamps in the format of MMDDYYYY-HHMMSS.
For eg. 04192012-000623 and 04192012-000854.
I need to be able to find the difference between the 2 in seconds.
Special cases to check for
the dates straddling midnight. For eg: 04172012-115500 & 04182012-000200.
it shouldn't matter which file comes in first, etc.
I am running ksh with no access to the date -d flag. Can anyone point me in the right direction on how to shell script this? (It is going to be a part of a larger shell script so no other languages please)
This is intended to be run on both solaris and linux ksh. Thanks in advance.
As a starter (from my provided link above):
#! /usr/bin/ksh
echo enter first time stamp
read TIME1
echo enter second time stamp
read TIME2
H1=${TIME1%:+([0-9])}
M1=${TIME1#+([0-9]):}
H2=${TIME2%:+([0-9])}
M2=${TIME2#+([0-9]):}
H1=${H1#0}
M1=${M1#0}
H2=${H2#0}
M2=${M2#0}
((MAM1=H1*60+M1))
((MAM2=H2*60+M2))
((MAM1>MAM2)) && ((MAM2=MAM2+1440))
((diff=MAM2-MAM1))
echo diff = $diff
exit 0
$ ./timestamp
enter first time stamp
17:30
enter second time stamp
18:05
diff = 35
$ ./timestamp
enter first time stamp
23:59
enter second time stamp
00:01
diff = 2
$
if this is just for fun, good luck :). if it's for actual practical purposes, see my strptime wrapper at How to get the difference between now and a different date (in minutes) using ksh (or another shell script)?, it should be easily adaptable.
This solution uses gnu-date and bashisms, since I don't know ksh.
d1=04192012-000623
dd1="${d1:4:4}/${d1:0:2}/${d1:2:2} ${d1:9:2}:${d1:11:2}:${d1:13:2}"
d1=04192012-000854
dd2="${d1:4:4}/${d1:0:2}/${d1:2:2} ${d1:9:2}:${d1:11:2}:${d1:13:2}"
echo $(($(date -d "$dd1" +%s) - $(date -d "$dd2" +%s)))
Maybe ${var:from:len} is not available in ksh to cut parts from strings, then you have to replace it with something else, maybe sed.
stat -c followed by one of the following
%X time of last access, seconds since Epoch
%Y time of last data modification, seconds since Epoch
%Z time of last status change, seconds since Epoch

Get current time in seconds since the Epoch on Linux, Bash

I need something simple like date, but in seconds since 1970 instead of the current date, hours, minutes, and seconds.
date doesn't seem to offer that option. Is there an easy way?
This should work:
date +%s
Just to add.
Get the seconds since epoch(Jan 1 1970) for any given date(e.g Oct 21 1973).
date -d "Oct 21 1973" +%s
Convert the number of seconds back to date
date --date #120024000
The command date is pretty versatile. Another cool thing you can do with date(shamelessly copied from date --help).
Show the local time for 9AM next Friday on the west coast of the US
date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Better yet, take some time to read the man page
http://man7.org/linux/man-pages/man1/date.1.html
Pure bash solution
Since bash 5.0 (released on 7 Jan 2019) you can use the built-in variable EPOCHSECONDS.
$ echo $EPOCHSECONDS
1547624774
There is also EPOCHREALTIME which includes fractions of seconds.
$ echo $EPOCHREALTIME
1547624774.371210
EPOCHREALTIME can be converted to micro-seconds (μs) by removing the decimal point. This might be of interest when using bash's built-in arithmetic (( expression )) which can only handle integers.
$ echo ${EPOCHREALTIME/./}
1547624774371210
In all examples from above the printed time values are equal for better readability. In reality the time values would differ since each command takes a small amount of time to be executed.
So far, all the answers use the external program date.
Since Bash 4.2, printf has a new modifier %(dateformat)T that, when used with argument -1 outputs the current date with format given by dateformat, handled by strftime(3) (man 3 strftime for informations about the formats).
So, for a pure Bash solution:
printf '%(%s)T\n' -1
or if you need to store the result in a variable var:
printf -v var '%(%s)T' -1
No external programs and no subshells!
Since Bash 4.3, it's even possible to not specify the -1:
printf -v var '%(%s)T'
(but it might be wiser to always give the argument -1 nonetheless).
If you use -2 as argument instead of -1, Bash will use the time the shell was started instead of the current date. This can be used to compute elapsed times
$ printf -v beg '%(%s)T\n' -2
$ printf -v now '%(%s)T\n' -1
$ echo beg=$beg now=$now elapsed=$((now-beg))
beg=1583949610 now=1583953032 elapsed=3422
With most Awk implementations:
awk 'BEGIN {srand(); print srand()}'
This is an extension to what #pellucide has done, but for Macs:
To determine the number of seconds since epoch (Jan 1 1970) for any given date (e.g. Oct 21 1973)
$ date -j -f "%b %d %Y %T" "Oct 21 1973 00:00:00" "+%s"
120034800
Please note, that for completeness, I have added the time part to the format. The reason being is that date will take whatever date part you gave it and add the current time to the value provided. For example, if you execute the above command at 4:19PM, without the '00:00:00' part, it will add the time automatically. Such that "Oct 21 1973" will be parsed as "Oct 21 1973 16:19:00". That may not be what you want.
To convert your timestamp back to a date:
$ date -j -r 120034800
Sun Oct 21 00:00:00 PDT 1973
Apple's man page for the date implementation:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/date.1.html
use this bash script (my ~/bin/epoch):
#!/bin/bash
# get seconds since epoch
test "x$1" == x && date +%s && exit 0
# or convert epoch seconds to date format (see "man date" for options)
EPOCH="$1"
shift
date -d #"$EPOCH" "$#"

Resources