how to add number of days to custom date in bash shell script [duplicate] - bash

In GNU with the command date I can do it:
date -d "+4 day"
datei=20130101
i=5
date -d "$datei +$i day"
But i like know:
how can i do it in Solaris?
with the date command

Tcl has a good free-form date scanner, if you have Tcl installed (try which tclsh). A shell function:
tcldate() {
d=${1:-now} # the date string
f=${2:-%c} # the output format
echo "puts [clock format [clock scan {$d}] -format {$f}]" | tclsh
}
In action on an ancient Solaris 8 box with bash 2.03 and tcl 8.3.3
$ tcldate
Tue Jul 23 13:27:17 2013
$ i=4
$ tcldate "$i days"
Sat Jul 27 13:27:34 2013
$ tcldate "$i days" "%Y-%m-%d"
2013-07-27
$ tcldate "20130101 + $i days" "%Y-%m-%d"
2013-01-05
This even handles daylight savings transitions:
$ tcldate "2014-03-09 00:30 + 1 hour" "%D %T %Z"
03/09/14 01:30:00 EST
$ tcldate "2014-03-09 00:30 + 2 hour" "%D %T %Z"
03/09/14 03:30:00 EDT
$ tcldate "2013-11-03 00:30 + 1 hour" "%D %T %Z"
11/03/13 01:30:00 EDT
$ tcldate "2013-11-03 00:30 + 2 hour" "%D %T %Z"
11/03/13 01:30:00 EST

Related

Getting Epochal Time from Date String

I have a process that outputs a date like this (note the padding in front of the day (4):
Mon Jun 4 17:53:42 2018
I want to get the epochal time, so I've tried:
echo "Mon Jun 4 17:53:42 2018" | xargs -0 date -j -f "%a %b %d %T %Y" +%s
This does work, but I am left with the annoying:
Warning: Ignoring 1 extraneous characters in date string (
)
I can live with the warning, but I've tried removing the double spacing with sed:
echo "Mon Jun 4 17:53:42 2018" | sed -E 's/\ +/\ /g'
which returns:
Mon Jun 4 17:53:42 2018
But when I try to pipe this to the date format, I get the warning again (as though sed is not doing its thing).
The problem was not whitespace, but a newline. I removed sed and replaced with tr -d "\n" to remove the offending character:
echo "Mon Jun 4 17:53:42 2018" | tr -d "\n" | xargs -0 date -j -f "%a %b %d %T %Y" +%s

string "Fri Dec 16 16:12:24 CST 2016" to timestamp by shell

I have some string (read from a file) ,such as "Fri Dec 16 16:12:24 CST 2016", and How to convert this string to timestamp, by shell?
I need timestamp,such as Fri Dec 16 16:27:28 CST 2016 -> 1481876854
shell cmd date print Fri Dec 16 16:12:24 CST 2016,so I think date have some method to do it.
but I can't find a solution,Can someone help me?
macos 10.12.2
For the FreeBSD date, you can use the -j flag with format as -f "%a %b %d %T %Z %Y"
date -j -f "%a %b %d %T %Z %Y" "Fri Dec 16 16:27:28 CST 2016" +"%s"
1481876854
If you are using GNU based date command, you can convert to EPOCH timestamp using the -d flag, i.e.
date -d"Fri Dec 16 16:27:28 CST 2016" +%s
1481876854
Based on your spec (time format) and if date doesn't work or is not available, the heavy tool
echo "Fri Dec 16 16:27:28 CST 2016" \
| gawk -F '[[:blank:]:]' 'BEGIN{
split( "Jan Feb Mar Apr May Jun Jul Agu Sep Oct Nov Dec", Temp, " ")
for (t in Temp) Month[ Temp[t]] = t < 10 ? "0" t : t
}
{
# mktime assume ISO C time format: "YYYY MM DD HH MM SS [DST]"
ISOTime = sprintf( "%d %s %s %s %s %s\n", $8, Month[ $2], $3 < 10 ? "0" $3 : $3, $4, $5, $6)
print mktime( ISOTime)
}
'

Loop minutes and echo with bash

I want to iterate all the minutes in a month (the purpose will be to generate a CSV file).
But when I try this:
d="2016-09-01 00:00:00"
while [ "$d" != "2016-09-30 00:00:00" ]; do
echo $d
d=$(date --utc "+%Y-%m-%d %H:%M:00" -d "$d + 1 minute" )
done
Both the hour and minute are being incremented:
2016-09-01 00:00:00
2016-09-01 01:01:00
2016-09-01 02:02:00
2016-09-01 03:03:00
2016-09-01 04:04:00
2016-09-01 05:05:00
2016-09-01 06:06:00
2016-09-01 07:07:00
2016-09-01 08:08:00
What am I doing wrong and how to correctly loop minutes?
I would work with Unix timestamps instead.
d=$(date --utc +%s -d "2016-09-01 00:00:00")
end=$(date --utc +%s -d "2016-09-30 00:00:00")
while [ "$d" != "$end" ]; do
date --utc "+%Y-%m-%d %H:%M:00" -d "#$d"
d=$(( d + 60 ))
done
You can add timezone UTC after your date string variable $d to get the right output:
d="2016-09-01 00:00:00"
for ((i=0; i<=60; i++)); do
date --utc -d "$d UTC + $i minute"
done
Thu Sep 1 00:00:00 UTC 2016
Thu Sep 1 00:01:00 UTC 2016
Thu Sep 1 00:02:00 UTC 2016
Thu Sep 1 00:03:00 UTC 2016
Thu Sep 1 00:04:00 UTC 2016
Thu Sep 1 00:05:00 UTC 2016
...
...
Thu Sep 1 00:55:00 UTC 2016
Thu Sep 1 00:56:00 UTC 2016
Thu Sep 1 00:57:00 UTC 2016
Thu Sep 1 00:58:00 UTC 2016
Thu Sep 1 00:59:00 UTC 2016
Thu Sep 1 01:00:00 UTC 2016
Note use of UTC after $d.
Using + after a time component in the date string is used for ' time zone correction' not for doing what you want to do. Interestingly, inverting date and time works:
$ date "+%Y-%m-%d %H:%M:00" -d "21:31:00 2016-09-03 + 1 minute "
2016-09-03 21:32:00
while the other way around messes with timezones and offsets so the result might depend on your local configuration:
$ TZ=Europe/London date "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-03 21:33:00
$ TZ=Europe/Brussels date "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-03 22:33:00
$ TZ=Asia/Singapore date "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-04 04:33:00

How to convert time output from SQL*Plus TIMING to a Linux TIMESTAMP? [duplicate]

Looking for a bash line to take a RSS date format such as "Fri, 13 Sep 2013 17:16:45 GMT" and convert it into milliseconds?
I've tried things as below they they do not produce in milliseconds. I'm running Mac OS X Snow Leopard 10.6.8.
524 date +%s -d "Fri, 13 Sep 2013 17:16:45 GMT"
525 date +%s -d "Fri 13 Sep 2013 17:16:45 GMT"
526 date +%s -d "Fri 13 Sep 2013 17:16:45"
527 date +%s -f "Fri, 13 Sep 2013 17:16:45 GMT"
528 date +%s -f "Fri, 13 Sep 2013 17:16:45 GMT"
514 date +%s -d "Fri, 13 Sep 2013 17:16:45 GMT"
515 date +%s -d "Fri, 13 Sep 2013 17:16:45"
516 date +%s -ud "Fri, 13 Sep 2013 17:16:45 GMT"
517 date +%s -ud "Fri, 13 Sep 2013 17:16:45"
512 date -d "Fri, 13 Sep 2013 17:16:45 GMT" "+%s"
Does the RSS date have fractional seconds?
If not, using BSD date (i.e. Mac OS X):
echo $(date -j -f "%a, %d %b %Y %H:%M:%S" "Fri, 13 Sep 2013 17:16:45" +%s)000
or, according to the Mac OS X manpage:
echo $(date -j -f "%a, %d %b %Y %H:%M:%S %Z" "Fri, 13 Sep 2013 17:16:45 GMT" +%s)000
If you have GNU date, the following rather simpler expression will work:
echo $(date +%s -d "Fri, 13 Sep 2013 17:16:45 GMT")000
Or you could use this, which will work with fractional seconds in the original time string:
echo $(($(date +%s%N -d "Fri, 13 Sep 2013 17:16:45.126 GMT")/1000000))
The date command gives you time in [s] resolution. Just append 3 zeroes, if that resolution is OK for you.
date +%s -d "Fri, 13 Sep 2013 17:16:45 GMT" | sed 's/..*/&000/'
If it's not, I'm afraid you'll have to write a C program, using the system call gettimeofday(). Let's call it gettimeofday.c:
#include <sys/time.h>
#include <stdio.h>
int main(void)
{
struct timeval t;
gettimeofday(&t, NULL);
printf("%d%d\n", t.tv_sec, t.tv_usec / 1000);
return 0;
}
To compile it you need gcc and make:
make gettimeofday
And then:
./gettimeofday
On second thoughts, my little program is totally useless, since you want to convert a given date to Unix time. But I'll leave it here, because it's so nice. :-)
You can use awk to add three zeros at the end of the output to make it in ms.
date +%s -d "Fri, 13 Sep 2013 17:16:45 GMT" | awk '{print $0"000"}'
or use sed to do the same :
date +%s -d "Fri, 13 Sep 2013 17:16:45 GMT" | sed -e "s|.*|&000|g"
The above don't work in Mac OS X Lion. You have to install GNU date which is in the coreutil package in MacPorts. You will then get gdate.
Instead you can use python to do this :
python -c'import time; print "%f" % (time.mktime(time.gmtime())*1000.)'
If you get an date: illegal time format error message on Mac OS X try using LANG=C date:
- echo $(date -j -f "%a, %d %b %Y %H:%M:%S %Z" "Fri, 13 Sep 2013 17:16:45 GMT" +%s)000 # date: illegal time format
+ echo $(LANG=C date -j -f "%a, %d %b %Y %H:%M:%S %Z" "Fri, 13 Sep 2013 17:16:45 GMT" +%s)000

How to get epoch time in shell script (for ksh)?

How to get epoch time in shell script (for ksh)?
I am interested in getting epoch time for the start of day (so e.g. now is July 28th, 2011 ~ 14:25:00 EST, I need time at midnight).
If you have GNU date,
epoch=$( date -d 00:00 +%s )
Otherwise, if you have tclsh,
epoch=$( echo 'puts [clock scan 00:00]' | tclsh )
Otherwise,
epoch=$( perl -MTime::Local -le 'print timelocal(0,0,0,(localtime)[3..8])' )
ksh's printf '%(fmt)T' supports time calculating. For example:
$ printf '%T\n' now
Mon Mar 18 15:11:46 CST 2013
$ printf '%T\n' '2 days ago'
Sat Mar 16 15:11:55 CST 2013
$ printf '%T\n' 'midnight today'
Mon Mar 18 00:00:00 CST 2013
$

Resources