Display previous days dates in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display previous days dates in ksh
# 1  
Old 12-15-2013
Display previous days dates in ksh

---------- Post updated at 03:42 AM ---------- Previous update was at 03:38 AM ----------

Sorry for a duplicate post, my post at the first place could not appear due to some net issue on my machine. Here is what i posted earlier:

Hi, i am using ksh in Solaris, i wanted to assign today's, yesterday's, and day before yesterday's date ( in mm/dd/YYYY format) to a variable in a script. Presently i am using :
Code:
tdate=$( date +"%m/%d/%Y" ) #Today
ydate=$(date +"%m/$(date +"%d" | awk '{ printf ("%02d",($1-1)) }')/%Y") #Yesterday
bydate=$(date +"%m/$(date +"%d" | awk '{ printf ("%02d",($1-2)) }')/%Y") #Day before yesterday

It works fine most of the time until today's date is 1 . Going by above logic, last two day's date would be 0 and -1, which in turn should be 30 and 29 or 31 and 30 based on the month( and a special case for February and leap year ). Presently i am making an exception on those days and making my job manually, but i wish to automate the process all along. Can someone help me to have a more optimized and accurate logic.
Thanks in advance
# 2  
Old 12-15-2013
No need of awk

try this

Code:
$ tdate=$( date +"%m/%d/%Y" ) # Today

$ ydate=$( date -d" -1 day" +"%m/%d/%Y" ) # Yesterday OR

$ ydate=$( date --date yesterday  +"%m/%d/%Y" ) # Yesterday

$ dydate=$( date -d" -2 day" +"%m/%d/%Y" ) # Day before yesterday OR

$ dydate=$( date --date="2 days ago" +"%m/%d/%Y" ) # Day before yesterday

$ echo $tdate
12/15/2013

$ echo $ydate
12/14/2013

$ echo $dydate
12/13/2013


Last edited by Akshay Hegde; 12-15-2013 at 05:22 AM.. Reason: more example
# 3  
Old 12-15-2013
Thanks Akshay,
Now i realize how pathetic my approach was :P .
# 4  
Old 12-18-2013
hi,
Problem is similar so tried continuing in the same thread than opening a new one. the problem now is i am trying to calculate time elapsed between two instances, at present i am using below in awk where date command doesn't work:
Code:
std=substr($2,4,2) ;                          #start date
end=substr($4,4,2) ;                         #end date
mul=((end-std)-1);                           #no: of full days elapsed in the interval
t1=(3600*substr($3,1,2)+60*substr($3,4,2)+substr($3,7,2)); #total no: of start time seconds
t2=(3600*substr($5,1,2)+60*substr($5,4,2)+substr($5,7,2)); #total no: of end time seconds
if (mul == "-1")                                 #both instances in the same day
t=t2-t1;
else                                               #instances occur in two different days
t=((24*3600)-t1)+(mul*24*3600)+t2;
printf("%02d:%02d:%02d\n",(t/3600),((t/60)%60),t%60);      #time elapsed in HH:MM:YY format

my input file will be like
name1 12/16/2013 10:56:38 12/16/2013 10:59:37 value1
name2 12/16/2013 04:37:41 12/17/2013 11:34:19 value2

and so on(a sequence of such records)

the problem again is the same. If start of the month appears with date 01 the previous day would be 31 or 30 or 28 (or 29 ) and my logic above wont work then.
One way is i hard code such inatances with month values in case statement. But just want to know if there is some better option to deal this ?
Any help will be gratefully welcome :-)

---------- Post updated 12-18-13 at 01:34 PM ---------- Previous update was 12-17-13 at 05:40 PM ----------

Does any one have any update? Atleast someone reply that there is no other way to do. Please make a reply so that I try some approach of mine.

Last edited by pr5439; 12-17-2013 at 06:57 PM.. Reason: more explaination
# 5  
Old 12-18-2013
Try this perl prog:

Code:
#!/usr/bin/env perl
use Time::Local;
use POSIX qw(strftime);

while (my $ln = <STDIN>) {
    my ($d, $smt, $sdy, $sy, $sh, $sm, $ss, $emt, $edy, $ey, $eh, $em, $es) = split(/[ \/:]/, $ln);
        my $t = abs(timelocal($es,$em,$eh,$edy,$emt-1,$ey) -
                timelocal($ss,$sm,$sh,$sdy,$smt-1,$sy));

    printf "%02d:%02d:%02d\n", $t / 3600, ($t / 60) % 60, $t % 60;
}

---------- Post updated at 10:38 AM ---------- Previous update was at 10:14 AM ----------

Call it like this

Code:
$ ./datediff.pl < infile
00:02:59
30:56:38


Last edited by Chubler_XL; 12-18-2013 at 10:21 PM.. Reason: Use abs() to avoid negative difference
# 6  
Old 12-22-2013
Hi.

A group of date-manipulation utilities is available, among them ddiff. Here's how it works with your sample data:
Code:
#!/usr/bin/env ksh

# @(#) s1       Demonstrate date arithmetic, differences, ddiff, dateutils.
# See:
# https://github.com/downloads/hroptatyr/dateutils/dateutils-0.2.3.tar.xz

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ksh ddiff

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, first-second, second-first:"
while :
do
  if read v1 d1 t1 d2 t2 v2
  then
    x1=${d1}T${t1}
    x2=${d2}T${t2}
    db "first date-time of pair, x1 [$x1]"
    db "last  date-time of pair, x2 [$x2]"
  else
    break
  fi
  ddiff $x1 $x2 -f '%d days and %S seconds' -i "%m/%d/%YT%T"
  ddiff $x2 $x1 -f '%d days and %S seconds' -i "%m/%d/%YT%T"
  pe
done < $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = POSIX, LANG = POSIX
(Versions displayed with local utility "version")
OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86
bash GNU bash 3.00.16
ksh M-11/16/88i
ddiff 0.2.6

-----
 Input data file data1:
name1 12/16/2013 10:56:38 12/16/2013 10:59:37 value1
name2 12/16/2013 04:37:41 12/17/2013 11:34:19 value2

-----
 Results, first-second, second-first:
0 days and 179 seconds
0 days and -179 seconds

1 days and 24998 seconds
-1 days and -24998 seconds

This was run under ksh (some demo-support scripts use bash).
Code:
NAME
     ddiff - Compute durations between dates and times

SYNOPSIS
     ddiff [OPTION]... DATE/TIME [DATE/TIME]...

DESCRIPTION
     ddiff 0.2.6

     Compute duration from DATE/TIME (the reference date/time) to
     the other DATE/TIMEs given and print the result as duration.
     If the other DATE/TIMEs are omitted read them from stdin.

See the git web page noted in the script. One would need to download and compile the codes (written in c) as I have done today in Solaris:
Code:
-rwxr-xr-x   1  105904 Dec 22 12:35 ddiff

if this is beyond your skills then use other solutions above.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get number of days between 2 dates

Gents. Please can u help. I would like to calculate the days between two dates. Example file1 ( previous date) file1 - Input file 9/29/2010 10195 9/29/2010 1057 2/2/2016 10 2/2/2016 10169 2/2/2016 1057 2/3/2016 10005 2/3/2016 10014 In file2 I add the actual date using this code.... (9 Replies)
Discussion started by: jiam912
9 Replies

2. Shell Programming and Scripting

Difference in dates in days (ksh shell)

Hi Guys, Need a small help, how do i get the difference between two dates (in days) in KSH shell My date is in mm/dd/YYYY format, Is there a function to get time stamp from the respective date and time ( mm/dd/yyyy HH:MM:SS) (1 Reply)
Discussion started by: selvankj
1 Replies

3. Shell Programming and Scripting

days are between the two dates?

I have two times in the format of YYMMDD. Does anyone know an easy way in ksh for me to display how many days are between the two dates? Example1: X=101202 Y=101205 There are 3 days between X & Y Example2: X=101202 Y=111202 There are 365 days between X & Y Example3: X=101205... (3 Replies)
Discussion started by: oldman2
3 Replies

4. Shell Programming and Scripting

display all dates 200 days back

i need help! can someone help me please? i try to calculate date under unix (ksh)...AIX operating system. I have to find the date 200 days from today's date. then the script should loop 200 times and display on command line every day's date until the current date. example: todays date:... (4 Replies)
Discussion started by: pavan_test
4 Replies

5. UNIX for Dummies Questions & Answers

display all dates 200 days back

i need help! can someone help me please? i try to calculate date under unix (ksh)...AIX operating system. I have to find the date 200 days from today's date. then the script should loop 200 times and display on command line every day's date until the current date. example: todays date:... (1 Reply)
Discussion started by: pavan_test
1 Replies

6. UNIX for Dummies Questions & Answers

using 'date' to get previous days' dates

I am familiar with using the 'date' command to get the current date but I have a situation where I need to get the previous day's date as well as the date two days prior. Theoretically I could use 'expr' to compute these values but I need it to work in instances where the previous month's dates... (2 Replies)
Discussion started by: slant-40
2 Replies

7. Shell Programming and Scripting

Days difference between two dates

Hello, I would like to find out the number of days between two dates of the format yyyy-mm-dd. Any help on this is highly appreciated. Thanks. (6 Replies)
Discussion started by: Data469
6 Replies

8. UNIX for Dummies Questions & Answers

Dates of previous years

Is there any way to use date with previous dates such as "2 23 2000" in order to see what day of the week it was? I tried changing the current date to "date 022300452000" but then it told me that I could not do this because I was "Not the owner". Any other ways of getting the day result? (2 Replies)
Discussion started by: terms5
2 Replies

9. Shell Programming and Scripting

Difference between two dates in no of days ???

Hi All How to get the difference between two dates in no of days ??? My date format is like this YYYY/MM/DD. I have to get the no of days between two dates in the given format. I tried to search the forum but nothing came up similar to my requitement. Your help will be appreciated. ... (1 Reply)
Discussion started by: csaha
1 Replies

10. UNIX for Dummies Questions & Answers

days elapsed between 2 dates

does anybody know how to find out the number of days elapsed between 2 dates e.g. days elapsed between 020212 and 020110 (YYMMDD format) Thanking you in advance. Ravi. (1 Reply)
Discussion started by: rkkiran
1 Replies
Login or Register to Ask a Question