Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to find last sunday (date) using perl? Post 302170723 by joeyg on Tuesday 26th of February 2008 02:39:18 PM
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
Calendar::Simple(3pm)					User Contributed Perl Documentation				     Calendar::Simple(3pm)

NAME
Calendar::Simple - Perl extension to create simple calendars SYNOPSIS
use Calendar::Simple; my @curr = calendar; # get current month my @this_sept = calendar(9); # get 9th month of current year my @sept_2002 = calendar(9, 2002); # get 9th month of 2002 my @monday = calendar(9, 2002, 1); # get 9th month of 2002, # weeks start on Monday my @span = date_span(mon => 10, # returns span of dates year => 2006, begin => 15, end => 28); DESCRIPTION
A very simple module that exports one function called "calendar". calendar This function returns a data structure representing the dates in a month. The data structure returned is an array of array references. The first level array represents the weeks in the month. The second level array contains the actual days. By default, each week starts on a Sunday and the value in the array is the date of that day. Any days at the beginning of the first week or the end of the last week that are from the previous or next month have the value "undef". If the month or year parameters are omitted then the current month or year are assumed. A third, optional parameter, start_day, allows you to set the day each week starts with, with the same values as localtime sets for wday (namely, 0 for Sunday, 1 for Monday and so on). date_span This function returns a cur-down version of a month data structure which begins and ends on dates other than the first and last dates of the month. Any weeks that fall completely outside of the date range are removed from the structure and any days within the remaining weeks that fall outside of the date range are set to "undef". As there are a number of parameters to this function, they are passed using a named parameter interface. The parameters are as follows: year The required year. Defaults to the current year if omitted. mon The required month. Defaults to the current month if omitted. begin The first day of the required span. Defaults to the first if omitted. end The last day of the required span. Defaults to the last day of the month if omitted. start_day Indicates the day of the week that each week starts with. This takes the same values as the optional third parameter to "calendar". The default is 0 (for Sunday). This function isn't exported by default, so in order to use it in your program you need to use the module like this: use Calendar::Simple 'date_span'; EXAMPLE A simple "cal" replacement would therefore look like this: #!/usr/bin/perl -w use strict; use Calendar::Simple; my @months = qw(January February March April May June July August September October November December); my $mon = shift || (localtime)[4] + 1; my $yr = shift || (localtime)[5] + 1900; my @month = calendar($mon, $yr); print " $months[$mon -1] $yr "; print "Su Mo Tu We Th Fr Sa "; foreach (@month) { print map { $_ ? sprintf "%2d ", $_ : ' ' } @$_; print " "; } A version of this example, called "pcal", is installed when you install this module. Date Range This module will make use of DateTime.pm if it is installed. By using DateTime.pm it can use any date that DateTime can represent. If DateTime is not installed it uses Perl's built-in date handling and therefore can't deal with dates before 1970 and it will also have problems with dates after 2038 on a 32-bit machine. EXPORT "calendar" AUTHOR
Dave Cross <dave@mag-sol.com> ACKNOWLEDGEMENTS
With thanks to Paul Mison <cpan@husk.org> for the start day patch. COPYRIGHT
Copyright (C) 2002-2008, Magnum Solutions Ltd. All Rights Reserved. LICENSE
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
perl, localtime, DateTime perl v5.10.1 2010-04-02 Calendar::Simple(3pm)
All times are GMT -4. The time now is 03:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy