How to find last sunday (date) using perl? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to find last sunday (date) using perl?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-26-2008
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 20 August 2014, 1:24 PM EDT
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
How to find last sunday (date) using perl?

How to find last sunday (date) using perl?
Eg, If i run today (26-Feb-2008 / Tuesday), I should get 24-Feb-2008 (Sunday).

Any help?
Sponsored Links
    #2  
Old 02-26-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 18 December 2014, 10:22 AM EST
Location: Out running a Marathon.
Posts: 2,342
Thanks: 74
Thanked 158 Times in 145 Posts
Hammer & Screwdriver One solution

Here is the script
($dow is the variable for day-of-week, stored at location [6] from localtime)


Code:
> cat sunday
#!/usr/bin/perl

# start of Perl
# Program to find previous Sunday date
#
# use strict;

 $today = date(time);
 $weekend = date2(time);

# start of subroutine date MM-DD-YYYY
sub date 
{
     my($time) = @_;     # incoming parameters

     @when = localtime($time);
     $dow=$when[6];
     $when[5]+=1900;
     $when[4]++;
     $date = $when[4] . "-" . $when[3] . "-" . $when[5];

     return $date;
}

# start of subroutine date2 MM-DD-YYYY
sub date2
{
     my($time) = @_;     # incoming parameters

     $offset = 0;
     $offset = 60*60*24*$dow;
     @when = localtime($time - $offset);
     $when[5]+=1900;
     $when[4]++;
     $date = $when[4] . "-" . $when[3] . "-" . $when[5];

     return $date;
}

print "$today \n";
print "$dow \n";
print "$weekend \n";

Here is the output

Code:
> sunday
2-26-2008 
2 
2-24-2008

Sponsored Links
    #3  
Old 02-26-2008
deepakwins deepakwins is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 20 August 2014, 1:24 PM EDT
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks of the input!
May I know what is '@_' does?
Coz, we dont pass any argument to the subroutine. And if I change the statement my($time) = @_; it gives out different result.
    #4  
Old 07-18-2008
pchang pchang is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 13 August 2014, 4:43 PM EDT
Posts: 74
Thanks: 13
Thanked 1 Time in 1 Post
can someone possibly convert this perl script into unix shell script?

Also I'm interested to find Last Saturday from any given date, not Sunday.

thank you.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to derive the last Sunday's date when Current date value is passed dsfreddie Shell Programming and Scripting 3 03-08-2012 08:41 PM
Last sunday of current date aishsimplesweet Shell Programming and Scripting 12 03-14-2011 11:23 AM
recurrence for every sunday for Date::MAnip in perl harpreetanand Shell Programming and Scripting 1 01-29-2009 09:45 AM
How to find Day of the Week from the given date (Perl)? deepakwins UNIX for Dummies Questions & Answers 5 03-07-2008 04:45 PM
Date - Last Sunday thru Saturday skymirror Shell Programming and Scripting 2 12-12-2005 01:18 PM



All times are GMT -4. The time now is 10:57 PM.