Sponsored Content
Top Forums Shell Programming and Scripting Perl script to toggle through dates by week Post 302365583 by subhap on Tuesday 27th of October 2009 07:57:07 PM
Old 10-27-2009
MySQL Perl script to toggle through dates by week

Hi,
I need help to toggle through dates on a weekly basis to be fed into a script as inputs. The format should be: yyyy/mm/dd (start) yyyy/mm/dd (end), where end date is 7 days increments.

The date (start) would be input as an ARGV and would continue until current date.

I can check current date as follows:
Code:
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime time;
$year += 1900;
$mon += 1;

is there any perl function that would toggle through by a week.

And if the Start Date is say Not Sunday, then the start date should be adjusted to the next nearest Sunday.

thank you
Subha

---------- Post updated at 06:57 PM ---------- Previous update was at 12:23 PM ----------

#!/usr/bin/perl -w
use strict;
use warnings;
use Time::Local;

if ($#ARGV < 0)
{
die "Usage dates <input arg> : format is yyyy/mm/dd\n ";
exit (1);
}

my $start_date = $ARGV[0];
my $time = 0;
my $end_time = time;
print "Start Date: $start_date \n";
print "End Date: $end_time \n";

my @dates = split ('/', $start_date);
my $syyyy = $dates[0];
my $smm = $dates[1];
my $sdd = $dates[2];
$smm = $smm - 1;
$time = timelocal(0,0,0,$sdd,$smm,$syyyy);

print "Start time entered:", scalar(localtime($time)), "\n";

while ($time < $end_time)
{
$time += 7 * 24 * 60 * 60;
my ($isec, $imin, $ihour, $idd, $imm, $iyyyy, $iwday, $iyday, $iisdst) = localtime($time);
$iyyyy += 1900;
$imm += 1;
print "Time toggle: $iyyyy $imm $idd \n";
}

Last edited by pludi; 10-27-2009 at 02:36 PM.. Reason: code tags, please...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help w/ Perl dates

I need to create 12 variables, the first of which is the date of the first day of the current month (01/01/2006), and the remaining 11 are to equal each month after the current. var1 = 01/01/2006 var2 = 02/01/2006 var3 = 03/01/2006 var4 = 04/01/2006 etc. How can I easily do this is in... (7 Replies)
Discussion started by: ssmiths001
7 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

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

4. Shell Programming and Scripting

Perl difference between dates

Hi, Is there any way I can get the difference between two dates in terms of days? I have used this method so far, but I cant format it in terms of days. @a=&DateCalc($date1,$date2,0); The o/p that I am getting is sort of like this: +0:0:0:4:0:0:0 I just want to get 4 days as an o/p.... (1 Reply)
Discussion started by: King Nothing
1 Replies

5. Shell Programming and Scripting

Subtracting two dates in PERL

Hi guys, First of all, I would like to say this is my first post in the unix.com forums. I am a beginner in PERL and have only started writing my first scripts. With that out of the way, I have a question regarding the calculation of time dates in PERL. I have two scalar variables with the... (1 Reply)
Discussion started by: DiRNiS
1 Replies

6. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

7. Linux

Get all the files from a FTP location with previous week's dates in the file names using Linux

I have a weird requirement where I have to get the files from a FTP(Lets say FTP1) location and place it on my current FTP(Lets say FTP2) location. The issue is, these are daily files (in a pattern Sales_YYYYMMDD_report.csv) and are placed every day on FTP1 and my process usually runs on Monday(eg.... (2 Replies)
Discussion started by: dhruuv369
2 Replies

8. Shell Programming and Scripting

Need Help:Shell Script for Solaris to change the dates in a file by one week

I have to increase the date by one week in an input when script is executed in solaris. I was able to acheive this using ksh script that is working in Linux enivironment, when i execute the same script in Solaris i am getting below error: /var/tmp\n\r-> ./script.ksh date: illegal option -- d... (3 Replies)
Discussion started by: sriramanaramoju
3 Replies

9. Shell Programming and Scripting

Perl : Module to get the last week date and time

Hello folks, I am looking for a Perl module or a program logic that gives the startdate and enddate of the last week.. Suppose say, assuming this week starts from Sunday to Saturday If I execute the script in this week. I need to get the last week sunday's date. Could anyone please... (1 Reply)
Discussion started by: scriptscript
1 Replies

10. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies
Time::Local(3)						User Contributed Perl Documentation					    Time::Local(3)

NAME
Time::Local - efficiently compute time from local and GMT time SYNOPSIS
$time = timelocal( $sec, $min, $hour, $mday, $mon, $year ); $time = timegm( $sec, $min, $hour, $mday, $mon, $year ); DESCRIPTION
This module provides functions that are the inverse of built-in perl functions "localtime()" and "gmtime()". They accept a date as a six- element array, and return the corresponding time(2) value in seconds since the system epoch (Midnight, January 1, 1970 GMT on Unix, for example). This value can be positive or negative, though POSIX only requires support for positive values, so dates before the system's epoch may not work on all operating systems. It is worth drawing particular attention to the expected ranges for the values provided. The value for the day of the month is the actual day (ie 1..31), while the month is the number of months since January (0..11). This is consistent with the values returned from "localtime()" and "gmtime()". FUNCTIONS
"timelocal()" and "timegm()" This module exports two functions by default, "timelocal()" and "timegm()". The "timelocal()" and "timegm()" functions perform range checking on the input $sec, $min, $hour, $mday, and $mon values by default. "timelocal_nocheck()" and "timegm_nocheck()" If you are working with data you know to be valid, you can speed your code up by using the "nocheck" variants, "timelocal_nocheck()" and "timegm_nocheck()". These variants must be explicitly imported. use Time::Local 'timelocal_nocheck'; # The 365th day of 1999 print scalar localtime timelocal_nocheck( 0, 0, 0, 365, 0, 99 ); If you supply data which is not valid (month 27, second 1,000) the results will be unpredictable (so don't do that). Year Value Interpretation Strictly speaking, the year should be specified in a form consistent with "localtime()", i.e. the offset from 1900. In order to make the interpretation of the year easier for humans, however, who are more accustomed to seeing years as two-digit or four-digit values, the following conventions are followed: o Years greater than 999 are interpreted as being the actual year, rather than the offset from 1900. Thus, 1964 would indicate the year Martin Luther King won the Nobel prize, not the year 3864. o Years in the range 100..999 are interpreted as offset from 1900, so that 112 indicates 2012. This rule also applies to years less than zero (but see note below regarding date range). o Years in the range 0..99 are interpreted as shorthand for years in the rolling "current century," defined as 50 years on either side of the current year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045, but 55 would refer to 1955. Twenty years from now, 55 would instead refer to 2055. This is messy, but matches the way people currently think about two digit dates. Whenever possible, use an absolute four digit year instead. The scheme above allows interpretation of a wide range of dates, particularly if 4-digit years are used. Limits of time_t On perl versions older than 5.12.0, the range of dates that can be actually be handled depends on the size of "time_t" (usually a signed integer) on the given platform. Currently, this is 32 bits for most systems, yielding an approximate range from Dec 1901 to Jan 2038. Both "timelocal()" and "timegm()" croak if given dates outside the supported range. As of version 5.12.0, perl has stopped using the underlying time library of the operating system it's running on and has its own implementation of those routines with a safe range of at least +/ 2**52 (about 142 million years). Ambiguous Local Times (DST) Because of DST changes, there are many time zones where the same local time occurs for two different GMT times on the same day. For example, in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00 can represent either 2001-10-28 00:30:00 GMT, or 2001-10-28 01:30:00 GMT. When given an ambiguous local time, the timelocal() function should always return the epoch for the earlier of the two possible GMT times. Non-Existent Local Times (DST) When a DST change causes a locale clock to skip one hour forward, there will be an hour's worth of local times that don't exist. Again, for the "Europe/Paris" time zone, the local clock jumped from 2001-03-25 01:59:59 to 2001-03-25 03:00:00. If the "timelocal()" function is given a non-existent local time, it will simply return an epoch value for the time one hour later. Negative Epoch Values On perl version 5.12.0 and newer, negative epoch values are fully supported. On older versions of perl, negative epoch ("time_t") values, which are not officially supported by the POSIX standards, are known not to work on some systems. These include MacOS (pre-OSX) and Win32. On systems which do support negative epoch values, this module should be able to cope with dates before the start of the epoch, down the minimum value of time_t for the system. IMPLEMENTATION
These routines are quite efficient and yet are always guaranteed to agree with "localtime()" and "gmtime()". We manage this by caching the start times of any months we've seen before. If we know the start time of the month, we can always calculate any time within the month. The start times are calculated using a mathematical formula. Unlike other algorithms that do multiple calls to "gmtime()". The "timelocal()" function is implemented using the same cache. We just assume that we're translating a GMT time, and then fudge it when we're done for the timezone and daylight savings arguments. Note that the timezone is evaluated for each date because countries occasionally change their official timezones. Assuming that "localtime()" corrects for these changes, this routine will also be correct. BUGS
The whole scheme for interpreting two-digit years can be considered a bug. SUPPORT
Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details. Please submit bugs to the CPAN RT system at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Time-Local or via email at bug-time-local@rt.cpan.org. COPYRIGHT
Copyright (c) 1997-2003 Graham Barr, 2003-2007 David Rolsky. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. AUTHOR
This module is based on a Perl 4 library, timelocal.pl, that was included with Perl 4.036, and was most likely written by Tom Christiansen. The current version was written by Graham Barr. It is now being maintained separately from the Perl core by Dave Rolsky, <autarch@urth.org>. perl v5.16.3 2012-07-20 Time::Local(3)
All times are GMT -4. The time now is 05:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy