I have rather basic task but wasn't able to find proper solution to it.
I want to iterate date interval since 2008 to current and need epoch values for each iteration of the loop. I am interested in iterating both years, halves and months as well.
I wrote such script
#!/bin/bash
initial_date=`date -d "2008-02-04 00:00:00 UTC" +%s`
end_date=`date +%s`
n=0
until [ $initial_date -gt $end_date ]; do
echo $initial_date
let n+=1
readable_date=`date +"%Y-%m-%d %T" -d "1970-01-01 $initial_date sec"`
echo $readable_date
readable_date=`date -d "$readable_date + $n year"`
initial_date=`date -d "$readable_date" +%s`
done
but its output is rather weird to me:
1202083200
2008-02-04 00:00:00
1233702000
2009-02-03 23:00:00
1265230800
2010-02-03 21:00:00
1296756000
2011-02-03 18:00:00
1328277600
2012-02-03 15:00:00
1359885600
2013-02-03 11:00:00
1391403600
2014-02-03 06:00:00
1422918000
2015-02-02 23:00:00
1454425200
2016-02-02 15:00:00
Why the year is not incremented properly? Shifted hours seems to me a side-effect of unix>>UTC>>unix conversion. Is there any direct (without reconversion) method for doing this?
P.S.
And yes, I checked this, this and this question and found no clear way of doing this. All they are based on incrementing number and converting date with the help of it which doesn't seem precise to me.
And yes, I thought about adding 60*60*24*30*365
to initial date but would it be correct? This approach doesn't consider leap years, months comprised of 31 days and so on.
date -d "$START + $yearnum year" +"%s %Y-%m-%d %T %Z"
– Costas Aug 06 '16 at 17:40$X + $Y years +%s
where relative items can be used together with%s
format. – Suncatcher Aug 07 '16 at 10:22TZ=UTC
– Suncatcher Aug 07 '16 at 10:34