I am mediating some data from one file-format to another and am using bash to perform the mediation steps. Part of the process involves taking a timestamp from the filename to create a starttime and then adding 5 minutes to that to create the endtime. The filename gives me a date in this format YYYYmmddHHMMSS. I can easily add 500 to each starttime to get an endtime that works for all starttimes except 55 mins past the hour but this obviously not correct so I have tried to use the date --date= code to add the 5minutes like this:
date --date='$starttime +5 minutes' '+%Y%m%d%H%M%S'
for example:
date --date='20140220125500 +5 minutes' '+%Y%m%d%H%M%S'
However I get an error like this every time:
date: invalid date `20140220125500 +5 minutes'
Can anyone suggest what I need to change in my date statement or if there is a similar alternative syntax available?
This worked to me:
$ date --date='20140220 12:55:00 CET +5 minutes' '+%Y%m%d%H%M%S'
20140220130000
That is, the problem is that YYYYYMMDDHHMMSS alone is not understandable for date. Instead, give it on the YYYYYMMDD HH:MM:SS format.
As you are receiving the data on that YYYYYMMDDHHMMSS format, you can parse it with sed as follows:
$ file="YYYYmmddHHMMSS"
$ new_name=$(sed -e 's/^\(.\{8\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1 \2:\3:\4/' <<< "$file")
$ echo "$new_name"
YYYYmmdd HH:MM:SS
So in your case:
$ file="20140220125500"
$ new_name=$(sed -e 's/^\(.\{8\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1 \2:\3:\4/' <<< "$file")
$ echo "$new_name"
20140220 12:55:00
$ date --date="$new_name CET + 5 minutes"
Thu Feb 20 13:00:00 CET 2014
Note I used
$ date --date="$new_name CET +5 minutes" '+%Y%m%d%H%M%S'
^ ^
note the double quotes to have the var expanded
Related
I'm trying to figure out how to get the current hour rounded down to start of the hour and the next hour in bash?
For example, if I run my script:
./printHour.sh
and let's say the current time at execution is 13:04:12 - it would print
current hour is: 13:00:00
next hour is: 14:00:00
Progress so far: (but this gives 1hour ago so it does not work) - any ideas?
lastHour=$(date -d '1 hour ago' "+%H:%M:%S")
echo "current hour is: "$lastHour
You can use this utility function:
hrdt() { date -d "${1?} hour ago" '+%H:00:00'; }
Testing:
> hrdt
bash: 1: parameter not set
> hrdt 0
08:00:00
> hrdt 1
07:00:00
> hrdt 2
06:00:00
> hrdt 3
05:00:00
Could you please try following, written and tested as per shown samples, my date is GNU date version.
cat script.bash
#!/bin/bash
currentHour=$(date "+%H:00:00")
nextHour=$(date -d '+1 hour' "+%H:00:00")
echo "current hour is: $currentHour"
echo "next hour is: $nextHour"
When I run above script I get as follows:
current hour is: 06:00:00
next hour is: 07:00:00
Seems like you don't need anything special so this should do it:
date -d '1 hour ago' "+%H:00:00"
Why bother when you want exactly the hour where both %M and %S are expected to be zero?
You don't need date in this case; as seen below, built-in printf can generate formatted date-time strings too. Here -1 represents current time, and EPOCHSECONDS is a dynamic variable that expands to the number of seconds since epoch.
$ printf 'current hour is: %(%H)T:00:00\n' -1
current hour is: 17:00:00
$
$ printf 'next hour is: %(%H)T:00:00\n' $((EPOCHSECONDS + 3600))
next hour is: 18:00:00
Using awk,
$ awk ' BEGIN { st=systime();
print "current hour=" strftime("%F %H:00:00",st);
print "next hour=" strftime("%F %H:00:00",st+(60*60)) } '
current hour=2020-12-26 23:00:00
next hour=2020-12-27 00:00:00
$
I want to modify this command to subtract 30 days from current date automatically
$ awk -v t=$(date +%Y-%m-%d) -F "'" '$1 < t' myname.dat
When I try
$ awk -v t=$(date "--date=$(date) -30days" +%Y-%m-%d) -F "'" '$1 < t' myname.dat
I get the following error; date: illegal option
I want to do this without having to convert the dates to epoch time in the file.
#edit: The following will work with GNU date only:
You can always subtract seconds.
date --date=#$(($(date +%s) - 30 * 24 * 3600)) +%Y-%m-%d
If you are interested in subtracting 30 days form "now", just:
date --date="-30days" +%Y-%m-%d
date date formatting is so broad, it's good to specify the exact date with for example -I option, from man date:
-I[FMT], --iso-8601[=FMT]
output date/time in ISO 8601 format. FMT='date' for date only
(the default), 'hours', 'minutes', 'seconds', or 'ns' for date
and time to the indicated precision. Example:
2006-08-14T02:34:56-06:00
The following:
date --date="$(date -I) -30days" +%Y-%m-%d
works on my system as expected.
I have a file with date '2015-06-01-12', how can I get it to increment the hour in shell script? The result I want is '2015-06-01-13'. If its the 23rd hour it has to move forward a date and get 00 as hour.
I was able to do it to date but have so far had not any luck with incrementing hours.
currDate=2015-06-02
nextDate=`date '+%Y-%m-%d' -d "$currDate+1 days"`
echo $nextDate
It is reasonably easy if you keep your date as an epoch (number of seconds since January 1, 1970):
$ currDate=$( date +%s -d "2015-06-02 23:00:00" )
$ echo $currDate
1433300400
$ date +%Y-%m-%d-%H -d #$currDate
2015-06-02-23
$ nextDate=$(( $currDate + 3600 )) #adding an hour's worth of seconds
$ date +%Y-%m-%d-%H -d #$nextDate
2015-06-03-00
I want to get this time in milliseconds
01/Mar/2012:09:08:00
I thought doing the following would store the new date then I can convert the date into milliseconds
time=01/Mar/2012:09:08:00
newDate=date --set="$time";
What do I need to do to get this working?
You should convert '01/Mar/2012:09:08:00' to a valid date string '01 Mar 2012 09:08:00'
$ time=01/Mar/2012:09:08:00
$ time="${time//// }"
$ time="${time/:/ }"
$ newDate=`date -d "$time" +%s000`
$ echo $newDate
1330564080000
Use Timer() method to get time then put that time in formatnumber() method as like
FormatNumber(Timer(), 2)
I have the easiest way to convert date into milliseconds:
echo $(date +'%s')
In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes?
ie, 90000 -> 85000
This is a bit tricky. Date can do general manipulations, i.e. you can do:
date --date '-10 min'
Specifying hour-min-seconds (using UTC because otherwise it seems to assume PM):
date --date '11:45:30 UTC -10 min'
To split your date string, the only way I can think of is substring expansion:
a=114530
date --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
And if you want to just get back hhmmss:
date +%H%M%S --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
why not just use epoch time and then take 600 off of it?
$ echo "`date +%s` - 600"| bc; date
1284050588
Thu Sep 9 11:53:08 CDT 2010
$ date -d '1970-01-01 UTC 1284050588 seconds' +"%Y-%m-%d %T %z"
2010-09-09 11:43:08 -0500
Since you have a 5 or 6 digit number, you have to pad it before doing string manipulation:
$ t=90100
$ while [ ${#t} -lt 6 ]; do t=0$t; done
$ echo $t
090100
$ date +%H%M%S --utc -d"today ${t:0:2}:${t:2:2}:${t:4:2} UTC - 10 minutes"
085100
Note both --utc and UTC are required to make sure the system's timezone doesn't affect the results.
For math within bash (i.e. $(( and ((), leading zeros will cause the number to be interpreted as octal. However, your data is more string-like (with a special format) than number-like, anyway. I've used a while loop above because it sounds like you're treating it as a number and thus might get 100 for 12:01 am.
My version of bash doesn't support -d or --date as used above. However, assuming a correctly 0-padded input, this does work
$ input_time=130503 # meaning "1:05:03 PM"
# next line calculates epoch seconds for today's date at stated time
$ epoch_seconds=$(date -jf '%H%M%S' $input_time '+%s')
# the 600 matches the OP's "subtract 10 minutes" spec. Note: Still relative to "today"
$ calculated_seconds=$(( epoch_seconds - 600 )) # bc would work here but $((...)) is builtin
# +%H%M%S formats the result same as input, but you can do what you like here
$ echo $(date -r $calculated_seconds '+%H%M%S')
# output is 125503: Note that the hour rolled back as expected.
For MacOS users you can do the following:
$(date -v -10M +"%H:%M:%S")
Date time without a specific format:
$(date -v -10M)
For non-macOS users:
Date time without a specific format:
date --date '-10 min'