I am preparing a script in bash
to handle inserting some data into a table, but I need to modify the date so that it has the current time, the same time in UTC
and the same time in UTC
minus half an hour.
For example:
- Script start time: 12:00
- Period start time: 09:30 (UTC - 30 MINUTES)
- Period end time: 10:00 (UTC)
So far the code I have written for this part is this:
FECHA_ACTUAL=`date +"%Y-%m-%d %H:%M:%S"`
FECHA_FIN=`date -u +"%Y-%m-%d %H:%M:%S"`
FECHA_INICIO=`date -d "$FECHA_FIN - 30 minutes" +"%Y-%m-%d %H:%M:%S"`
echo $FECHA_ACTUAL
echo $FECHA_INICIO
echo $FECHA_FIN
I found the part on date -d "$FECHA_FIN - 30 minutes"
the internet but it doesn't work for me.
How could I do it?
Try putting the following:
I just went through some of the very interesting documentation provided by the "Relative items in date strings" section throughout the manual at
info
and also did some testing with what you've tried.What @david mentions is what is described in that section about relative tenses, with own translation.
So several examples come to mind, for example the one in the answer given by @david
which would be equivalent to
By the multiplier "-30 <units>" equivalent to putting "30 <units> ago".
But we could also think about the shape of your example.
And this example works fine, but if you noticed the error that appeared in your code, it showed something like
Invalid date
, then it is most likely due to the format. Try to put the format of the end date with the letter "Z" at the end , letter "Z" of "zero time" or "Zulu time" as ISO 8601 indicates for the time in UTC. "Zulu" in the radio alphabet, used by the army, is to indicate the letter "Z".So your code could look like