{"id":864,"date":"2008-04-18T23:07:03","date_gmt":"2008-04-19T03:07:03","guid":{"rendered":"http:\/\/blogs.sungeek.net\/unixwiz\/?p=864"},"modified":"2008-04-18T23:09:50","modified_gmt":"2008-04-19T03:09:50","slug":"how-to-calculate-yesterday","status":"publish","type":"post","link":"https:\/\/blogs.sungeek.net\/unixwiz\/2008\/04\/18\/how-to-calculate-yesterday\/","title":{"rendered":"How to calculate yesterday"},"content":{"rendered":"<p>I was working on a shell script last night and needed to calculate the value for yesterday. I did not have access to GNU date, so using that is out of the question. All I could use was what was available to me in a default install of Solaris 10. So I decided to use Perl as such (note that the YESTERDAY should all be on one line):<\/p>\n<p><code lang=\"bash\"><br \/>\n#!\/bin\/bash<br \/>\nYESTERDAY=$(perl -e '@y=localtime(time()-86400);<br \/>\n     printf \"%04d%02d%02d\",$y[5]+1900,$y[4]+1,$y[3];$y[3];')<br \/>\n<\/code><br \/>\nWhat this will do is store the value of yesterday in a shell variable called YESTERDAY<br \/>\nNow I have not done perl in a long while so here is an explanation of what it does:<br \/>\n1. Runs the perl function time which will find the current time, then subtract 86400 from it (24 hours).<br \/>\n2. Next it is run through the localtime function which creates an array that has the following values:<\/p>\n<table border=1>\n<tr>\n<th>Array Element<\/th>\n<th>Value<\/th>\n<\/tr>\n<tr>\n<td align=center>0<\/td>\n<td>Seconds<\/td>\n<\/tr>\n<tr>\n<td align=center>1<\/td>\n<td>Minutes<\/td>\n<\/tr>\n<tr>\n<td align=center>2<\/td>\n<td>Hour<\/td>\n<\/tr>\n<tr>\n<td align=center>3<\/td>\n<td>Day of Month<\/td>\n<\/tr>\n<tr>\n<td align=center>4<\/td>\n<td>Month of year (0=January)<\/td>\n<\/tr>\n<tr>\n<td align=center>5<\/td>\n<td>Year (starting at 1900)<\/td>\n<\/tr>\n<tr>\n<td align=center>6<\/td>\n<td>Day of week (0=sunday)<\/td>\n<\/tr>\n<tr>\n<td align=center>7<\/td>\n<td>Day of Year (0..364 or 0..365 if leap)<\/td>\n<\/tr>\n<tr>\n<td align=center>8<\/td>\n<td>Is Daylight savings time active<\/td>\n<\/tr>\n<\/table>\n<p>So in my little script above, we are looking for fields 5, 4 and 3. I add 1900 to the value of 5 (in this case 5 = 108). I add 1 to the value of 4 to get the current month (3+1 = 4 = April). The values are then pushed through printf so that we have a 4 digit year with leading 0&#8217;s, a 2 digit month with leading 0&#8217;s and a 2 digit day with leading 0&#8217;s. So the value of my YESTERDAY variable will now show 20080418. <\/p>\n<p>Hope this helps some one else.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a shell script last night and needed to calculate the value for yesterday. I did not have access to GNU date, so using that is out of the question. All I could use was what was available to me in a default install of Solaris 10. So I decided to use &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blogs.sungeek.net\/unixwiz\/2008\/04\/18\/how-to-calculate-yesterday\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to calculate yesterday&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,16,2],"tags":[484,458,444],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-random-stuff","category-shell-scripts","category-solaris","tag-random-stuff","tag-shell-scripts","tag-solaris"],"_links":{"self":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":0,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sungeek.net\/unixwiz\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}