How to find last sunday (date) using perl?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find last sunday (date) using perl?
# 1  
Old 02-26-2008
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?
# 2  
Old 02-26-2008
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

# 3  
Old 02-26-2008
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
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Yesterday's Date if it is Sunday

Hi All, Can you please let me know how to get the yesterday's date of the given date if the given date is Sunday? I can't use GNU. I have the code to get the yesterday's date based on the system date. Thanks (5 Replies)
Discussion started by: shash
5 Replies

2. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

3. Shell Programming and Scripting

How to find last updated date and time of a folder in Perl?

Hi All, I have a process which after some time continues move a files to some folder(say the name of the folder is logdir) What i am trying to do is as the files are coming to the logdir folder, I want the latest updated time and date of the folder in PERL. (1 Reply)
Discussion started by: parthmittal2007
1 Replies

4. Shell Programming and Scripting

How to derive the last Sunday's date when Current date value is passed

Hi All, I have a requirement in my project where a batch runs on any day of a week. The input files will land to the server every Sunday. I need to read these files based on the current BUS_DAY (which falls any day of the week). For e.g : if the BUS_DAY is 20120308, we need to derive the... (3 Replies)
Discussion started by: dsfreddie
3 Replies

5. Shell Programming and Scripting

Last sunday of current date

Hi Please help me with this problem i need to find the date of last week sunday from the current given date (12 Replies)
Discussion started by: aishsimplesweet
12 Replies

6. Shell Programming and Scripting

recurrence for every sunday for Date::MAnip in perl

Hi, I have a requirement to define all Saturdays/Sundays of every month in a year as holidays. I am making use of Date::Manip package available in perl. I tried writing a recurrence as : 0:1*3:7:0:0:0 --> this relation helps me to define 3rd Sunday of a year as sunday. My requirement is... (1 Reply)
Discussion started by: harpreetanand
1 Replies

7. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies

8. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies

9. Shell Programming and Scripting

Date - Last Sunday thru Saturday

Hi All, I have to kick off a script on every Monday to get some data from database for last week (Sunday thru Saturday). If its Monday (12/12/2005) Begin date will be Sunday - 12/4/2005 End date will be Saturday - 12/10/2005 The script might not kick off on some Mondays. So my... (2 Replies)
Discussion started by: skymirror
2 Replies
Login or Register to Ask a Question