<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Date arithmetic within shell in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481641#M798764</link>
    <description>Can anyone help me?&lt;BR /&gt;&lt;BR /&gt;How do I perform arithmetic on dates within a shell script?  I need to set a variable equal to something like "Now - 6 days", and, as if that wasn't enough, the final format of this date variable needs to be "MM/DD/YY HH:MM:SS".  This is because the variable gets passed as a parameter to an executable which expects it in that format.&lt;BR /&gt;&lt;BR /&gt;TIA,&lt;BR /&gt;&lt;BR /&gt;Jim</description>
    <pubDate>Thu, 11 Jan 2001 01:17:25 GMT</pubDate>
    <dc:creator>James A. Donovan</dc:creator>
    <dc:date>2001-01-11T01:17:25Z</dc:date>
    <item>
      <title>Date arithmetic within shell</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481641#M798764</link>
      <description>Can anyone help me?&lt;BR /&gt;&lt;BR /&gt;How do I perform arithmetic on dates within a shell script?  I need to set a variable equal to something like "Now - 6 days", and, as if that wasn't enough, the final format of this date variable needs to be "MM/DD/YY HH:MM:SS".  This is because the variable gets passed as a parameter to an executable which expects it in that format.&lt;BR /&gt;&lt;BR /&gt;TIA,&lt;BR /&gt;&lt;BR /&gt;Jim</description>
      <pubDate>Thu, 11 Jan 2001 01:17:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481641#M798764</guid>
      <dc:creator>James A. Donovan</dc:creator>
      <dc:date>2001-01-11T01:17:25Z</dc:date>
    </item>
    <item>
      <title>Re: Date arithmetic within shell</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481642#M798765</link>
      <description>Hi Jim:&lt;BR /&gt;&lt;BR /&gt;A C-program. Perl, awk or a shell function -- any of which you develop is needed for date offsets greater than 24-hours.&lt;BR /&gt;&lt;BR /&gt;See this thread for an awk program (Al Langen's contribution) that does what you want:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x57eff841489fd4118fef0090279cd0f9,00.html" target="_blank"&gt;http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x57eff841489fd4118fef0090279cd0f9,00.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;For a very clever computation of a dates +- 24-hours using the TZ variable, follow Rich Garland's contributions through the discussion referenced in this thread:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0xf9fd5f260cafd4118fef0090279cd0f9,00.html" target="_blank"&gt;http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0xf9fd5f260cafd4118fef0090279cd0f9,00.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 11 Jan 2001 01:50:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481642#M798765</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-01-11T01:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Date arithmetic within shell</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481643#M798766</link>
      <description>Thanks James!  I too found Al's code just after posting my plea for help.&lt;BR /&gt;&lt;BR /&gt;I've tweaked it slightly so that it can handle subtracting any number of days from the date passed into it.&lt;BR /&gt;&lt;BR /&gt;x=`date +%x`&lt;BR /&gt;z=6;export z&lt;BR /&gt;y=`echo $x|awk whatever.awk`&lt;BR /&gt;&lt;BR /&gt;Returns a valid value for any value of z, where z does not result in a date from prior to the previous year.  That is, if x="01/10/01", a z&amp;gt;=376 will return an invalid date.&lt;BR /&gt;&lt;BR /&gt;BEGIN{ FS="/"&lt;BR /&gt;split( "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month," ");&lt;BR /&gt;split( "31 28 31 30 31 30 31 31 30 31 30 31", days, " ");&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;year = $3&lt;BR /&gt;leapyear = year % 4;&lt;BR /&gt;if( leapyear == 0 )&lt;BR /&gt;days[2] = 29&lt;BR /&gt;else&lt;BR /&gt;days[2] = 28;&lt;BR /&gt;doy = 0;&lt;BR /&gt;for ( i = 1; i&amp;lt; $1; i++ )&lt;BR /&gt;doy = doy + days[i]&lt;BR /&gt;doy = doy + $2;&lt;BR /&gt;if ( doy &amp;lt;= ENVIRON["z"] )&lt;BR /&gt;{&lt;BR /&gt;doy = doy + 365;&lt;BR /&gt;year = year - 1;&lt;BR /&gt;leapyear = year %4;&lt;BR /&gt;if( leapyear == 0 )&lt;BR /&gt;{&lt;BR /&gt;days[2] = 29;&lt;BR /&gt;doy = doy + 1;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;doy = doy - ENVIRON["z"];&lt;BR /&gt;for ( i = 1; i &amp;lt;= 12; i++ )&lt;BR /&gt;{&lt;BR /&gt;doy = doy - days[i];&lt;BR /&gt;if ( doy &amp;lt;= 0 )&lt;BR /&gt;{&lt;BR /&gt;doy = doy + days[i];&lt;BR /&gt;break;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;printf( "%d/%d/%d", i, doy, year )&lt;BR /&gt;}</description>
      <pubDate>Thu, 11 Jan 2001 02:19:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481643#M798766</guid>
      <dc:creator>James A. Donovan</dc:creator>
      <dc:date>2001-01-11T02:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Date arithmetic within shell</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481644#M798767</link>
      <description>There is a REAL simple way to do this.&lt;BR /&gt;&lt;BR /&gt;Here is a PERL example:&lt;BR /&gt;&lt;BR /&gt;use Time::Local;&lt;BR /&gt;$yy = `date +%y`;&lt;BR /&gt;$mm = `date +%m`;&lt;BR /&gt;$day = `date +%d`;&lt;BR /&gt;chomp($day);&lt;BR /&gt;chomp($yy);&lt;BR /&gt;chomp($mm);&lt;BR /&gt;$start = timelocal("00", "00", "00", $day, ($mm-1), $yy);&lt;BR /&gt;$pcurr = $start - (7*86400);   # get day 7 days prior&lt;BR /&gt;$fcurr = $start + (7*86400);   # get day 7 days in future&lt;BR /&gt;print "\nDate: ".localtime($fcurr);  # or pcurr for past&lt;BR /&gt;&lt;BR /&gt;# you can also grab the elements and apply them to some hashes for conversions if you like.&lt;BR /&gt;($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$dst)=localtime($fcurr);   # or use pcurr if you want the past&lt;BR /&gt;&lt;BR /&gt;Basically it is three steps.&lt;BR /&gt;&lt;BR /&gt;1. Get the current date/time in seconds (easy on UNIX)&lt;BR /&gt;2. Add the number of days * 86400&lt;BR /&gt;3. Convert the seconds to human readable date/time.&lt;BR /&gt;&lt;BR /&gt;Have fun!</description>
      <pubDate>Thu, 11 Jan 2001 17:24:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/date-arithmetic-within-shell/m-p/2481644#M798767</guid>
      <dc:creator>David DeMartini_1</dc:creator>
      <dc:date>2001-01-11T17:24:50Z</dc:date>
    </item>
  </channel>
</rss>

