Need help with date arithmetic please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with date arithmetic please
# 1  
Old 07-07-2017
Need help with date arithmetic please

Hello fellow forum members,

I wrote below piece of code to calculate the date after a given date -
Code:
date=$DATE_FINAL
declare -a  max_month=(0 31 28 31 30 31 30 31 31 30 31 30 31)
eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!')
(( year4=year%4 ))
(( year100=year%100 ))
(( year400=year%400 ))
if [ \( $year4 -eq 0 -a \
        $year100 -ne 0 \) -o \
     $year400 -eq 0 ]
then
declare -a  max_month=(0 31 28 31 30 31 30 31 31 30 31 30 31)
fi
day=$((day+1))
if [ $day -gt ${max_month[$month]} ] >| /wload/baot/home/baoted9/logs_bde27_conversion_1/ataa_display.logs 2>&1
then
  day=1
  month=$((month+1))
  if [ $month -gt 12 ]
  then
    year=$((year+1))
    month=1
  fi
fi
if [ $month -eq "08" ] ||  [ $month -eq "09" ]
then
future_date_final=$(echo $year"$month"$day)
else
future_date_final=$(printf "%4.4d%2.2d%2.2d" $year $month $day)
fi
echo "this is your final business date $future_date_final"

It calculates the date correctly however throws an error at the end of the code as below -
Code:
line 79: 08: value too great for base (error token is "08")

It just looks too ugly, not sure how to remove it as otherwise code is working fine, tried redirecting it to a log file still appearing.
Also, I am facing issue with below code for a plain cd command with code highlighted in red -
Code:
echo "pset  date $Param_date_1"
cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset >| /wload/baot/home/baoted9/logs_bde27_conversion_1/at_display.logs 2>&1
sh UKBA_publish.sh UKBA $Param_date_1 3 >| /wload/baot/home/baoted9/logs_bde27_conversion_1/ate_display.logs 2>&1

Error is -
Code:
./auto2.sh: line 190: syntax error: unexpected end of file

Any leads will be greatly appreciated.

Last edited by ektubbe; 07-07-2017 at 11:48 AM.. Reason: Wanted to add tags
# 2  
Old 07-07-2017
My opinion is that coding date arithmetic and not using existing tools is foolish and error prone. I cannot tell what system you are on, most systems have perl.

Code:
#!/bin/sh
# ago.shl
# usage: ago.shl [number] 
#       for date [number] days ago,
#       ./ago.shl -3 
#   parm $1 == days in past e.g., yesterday = -1,
#              days in future e.g., tomorrow = 1 

ago()
{
   perl -e ' my $delta = $ARGV[0];
             $delta*=86400;          #  seconds in a day
             $delta=time + $delta;   #  epoch seconds today plus ( or minus) delta
              ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                            localtime($delta);
             
             printf("%04d%02d%02d%02d%02d.%02d\n",
                     $year+1900, $mon+1, $mday;
             ' $1
}

# example usage of the function ago

when=$(ago $1)
echo "$when"

exit 0

If you are on Linux I would HIGHLY recommend that you use the date command which is very reliable and actually supports options like yesterday and tomorrow. Skip using the perl above.
# 3  
Old 07-07-2017
The "value too great for base" happens because there is a number with a leading zero, and bash assumes octal, and 8 is too high for an octal number.
It happens with
Code:
day=$((day+1))

or later with month.
As always there is an escape from such (mis)feature: a 10# prefix forces a base10=decimal
Code:
day=$((10#$day+1))
month=$((10#$month))

Now the leading zero are removed.

---------- Post updated at 11:15 ---------- Previous update was at 10:52 ----------

You certainly want
Code:
if [ \( $year4 -eq 0 -a \
        $year100 -ne 0 \) -o \
     $year400 -eq 0 ]
then
declare -a  max_month=(0 31 29 31 30 31 30 31 31)
fi

As Jim said, such calculations are error prone.
perl is safer because it uses the libc routines.
# 4  
Old 07-07-2017
Also try my general purpose date script which is a limited gnu-date-alike written in Perl.
# 5  
Old 07-07-2017
You might like this for the leap year stuff:
Code:
function mthdays { TA=312831303130313130313031; printf "%d\n" $(( ${TA:($1-1)*2:2} + ($1!=2?0:!($2%4) && ($2%100) || !($2%400)))); }
echo $(mthdays 2 2016)
29
echo $(mthdays 2 2015)
28

For the function definition, you need to switch off bash's history : set +H
# 6  
Old 07-07-2017
Hi.

A collection of suggestions:
Code:
Date, time arithmetic, math

        1) gnu date

        2) ksh ( version 93, not 88; ksh93 M 93t+ ) printf, e.g:
           printf "%(%Y-%m-%d)T\n" "yesterday"
           Many more examples ( as of 2017.07 ):
           http://blog.fpmurphy.com/2008/10/ksh93-date-manipulation.html

        3) date.pl (limited arithmetic, but "date.pl -d '- 1 day'" works)
           ( https://www.unix.com/tips-tutorials/
           239167-general-purpose-date-script.html )

        4) dconv et al, e.g. ddiff, dconv, strptime (parsing), etc.
           ( dateutils: http://www.fresse.org/dateutils/ )

        5) tm2tm (, OK in 32-bit; fails to compile in 64-bit)
           ( https://www.unix.com/shell-programming-scripting/
           146216-date-difference-between-freebsd-linux.html#post302463136 )

        6) perl custom formatting (with function POSIX::strftime, "perldoc POSIX")

        7) date-cpan.pl, cpan perl date
           ( http://cpansearch.perl.org/src/CWEST/ppt-0.14/src/date/date.jgross )

        8) For small changes, say, "yesterday" in bash, ksh
           TZ=CST+24 date +%Y%m%d
           csh, tcsh
           setenv TZ CST+24 ; date +%Y%m%d

        9) Check for legal date:
           manstat:validata ( http://oldwww.acm.org/perlman/stat/ )

        10) Check for legal date:
            validate (local perl code)

        11) Shell, ksh, date calculations:
            www.unix.com/unix-for-dummies-questions-and-answers/4870-days-elapsed-between-2-dates.html

Best wishes ... cheers, drl
# 7  
Old 07-09-2017
Quote:
Originally Posted by MadeInGermany
The "value too great for base" happens because there is a number with a leading zero, and bash assumes octal, and 8 is too high for an octal number.
It happens with
Code:
day=$((day+1))

or later with month.
As always there is an escape from such (mis)feature: a 10# prefix forces a base10=decimal
Code:
day=$((10#$day+1))
month=$((10#$month))

Now the leading zero are removed.

---------- Post updated at 11:15 ---------- Previous update was at 10:52 ----------

You certainly want
Code:
if [ \( $year4 -eq 0 -a \
        $year100 -ne 0 \) -o \
     $year400 -eq 0 ]
then
declare -a  max_month=(0 31 29 31 30 31 30 31 31)
fi

As Jim said, such calculations are error prone.
perl is safer because it uses the libc routines.
Thanks madeingermany...I tried but the error is still because of line I mentioned in my first post n I am using AIX so most of the commands don't work...I need to do date calculation for an arbitrary date so tz doesn't work for me either Smilie
~Ekta
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using: tr -cd < afile | wc -c I am also able to get the total number of characters using: wc -c afile How do I divide the first... (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Help needed with some date arithmetic

I have a file (main.lst) containing a list of dates in DDMMYYYY format. The dates will mostly be the same but it is possible to have multiple dates and these need not be in chronological order. I have another file containing another list of dates (holidays.lst). The task is to get the latest... (5 Replies)
Discussion started by: elixir_sinari
5 Replies

3. Post Here to Contact Site Administrators and Moderators

Broken link FAQ date arithmetic with shell

page unix com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html Date Arithmetic with the Shell has link of www samag com/documents/s=8284/sam0307b/0307b.htm which is no longer. Is this the correct place to post this?:confused: and I got message... (1 Reply)
Discussion started by: dgerman
1 Replies

4. Shell Programming and Scripting

Date and time Arithmetic

Hi, I need to process a file which contains below data. Usually the files contains both Start and Finish time. but for Few records, it contains only Start. For those records I need to add the finish line by adding 5 minutes to Start time. Started BBIDX Tue Jun 1 15:15:11 EDT 2010 292308... (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

5. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

6. Shell Programming and Scripting

Arithmetic on timestamps

Hi Friends, please advise on shell script to add two time stamps for example : a=12:32 b=12:00 c=a+b=00:32 please help me to find shell script to add to two time stamps, as i need to convert time from EST to GMT or SST to prepare status of jobs in unix and to specify estimated time to... (3 Replies)
Discussion started by: balireddy_77
3 Replies

7. Shell Programming and Scripting

How to perform arithmetic operation on date

Hi all, I would appreciate if anyone knows how to perform adding to date. As for normal date, i can easily plus with any number. But when it comes to month end say for example 28 Jun, i need to perform a plus with number 3, it will not return 1 Jul. Thanks in advance for your help. (4 Replies)
Discussion started by: agathaeleanor
4 Replies

8. Shell Programming and Scripting

arithmetic in ksh

Helloo.. I am trying one very simple thing I could not find anything on google.. I have 2 integer variable..and I need to do division...in ksh where $catch and $num are integer variable.. I tryed with this: printf "%0.2f" $final=$catch/$num but it does not work.. any help is... (12 Replies)
Discussion started by: amon
12 Replies

9. UNIX for Dummies Questions & Answers

arithmetic problem

i am used to making scripts for hp-ux. but lately i tried to make some for solaris. the problem is that when i tried to execute it it gave me an error the "let: not found". why is that? how can i perform an arithmetic function in the solaris shell script? thanks :) (2 Replies)
Discussion started by: inquirer
2 Replies

10. UNIX for Advanced & Expert Users

time arithmetic

Can anyone help please. I am writing a kourne shell script and I am unsure how to do the following: I have extracted a time string from a logfile, and I have another time string I want to compare it to to see if it's later than the time I'm comparing with. i.e. expectedSLA="23:00:00", ... (2 Replies)
Discussion started by: csong2
2 Replies
Login or Register to Ask a Question