Sponsored Content
Top Forums Shell Programming and Scripting Need to capture dates between start date and end date Using perl. Post 302539653 by fpmurphy on Monday 18th of July 2011 10:20:53 AM
Old 07-18-2011
Code:
#!/usr/bin/perl -w

use strict;

use DateTime;
use DateTime::Format::Strptime;

my $format = new DateTime::Format::Strptime ( pattern => '%d-%b-%Y', time_zone => 'EST', );
my $start = $format->parse_datetime($ARGV[0])->epoch();
my $finish = $format->parse_datetime($ARGV[1])->epoch();

while ($start <= $finish) {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$julian,$isdst) = localtime($start);
    printf "%4d%02d%02d|%02d\n",  $year + 1900, $mon + 1,  $mday, int($julian/7) + 1;
    $start += 24*60*60;
}

Code:
$ ./test.pl 01-MAR-2011 10-MAR-2011
20110301|09
20110302|09
20110303|09
20110304|09
20110305|10
20110306|10
20110307|10
20110308|10
20110309|10
20110310|10


Last edited by fpmurphy; 07-21-2011 at 10:32 AM..
This User Gave Thanks to fpmurphy For This Post:
 

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate quarter dates with begin date and end date

Hi All, I am trying to generate quarter dates with user giving input as begin date and end date. Example: Input by user: begin_date = "2009-01-01" end_date = 2010-04-30" required output: 2009-01-01 2009-03-31 09Q01 2009-04-01 2009-06-30 09Q02 . . till 2010-01-01 2010-03-31 10Q01 ... (9 Replies)
Discussion started by: sol_nov
9 Replies

2. Shell Programming and Scripting

Compare Start date and End date...

Hi All, I have problem in my file. It has two date variable. There are 2 variables which has the values as below 1. START_MONTH = “Date(YYYYMM) format” Ex: 201008 2. END_MONTH = “Date(YYYYMM) format” Ex: 201105 The end date should be greater than start date. Now we... (3 Replies)
Discussion started by: suresh01_apk
3 Replies

3. Shell Programming and Scripting

Need to capture all dates between start date and End date.

Hi All, I enter Start date and end date as parameters. I need to capture dates between start date and end date. Please let me know if you have any idea the same. Thanks in advance. Nagaraja Akkivalli. (5 Replies)
Discussion started by: Nagaraja Akkiva
5 Replies

4. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

5. Shell Programming and Scripting

Get the lines from logfile within start and end date

Hi guys, I am having the below logfile,date in yyyy-mm-dd 2013-08-02 *some content* 2013-08-02 *some content* 2013-08-02 *some content* 2013-08-03 *some content* 2013-08-05 *some content* from the above logfile i need to get the lines between the two timestamps,if i give... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

6. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

7. 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
DateTime::Format::Strptime(3pm) 			User Contributed Perl Documentation			   DateTime::Format::Strptime(3pm)

NAME
DateTime::Format::Strptime - Parse and format strp and strf time patterns VERSION
version 1.5000 SYNOPSIS
use DateTime::Format::Strptime; my $Strp = new DateTime::Format::Strptime( pattern => '%T', locale => 'en_AU', time_zone => 'Australia/Melbourne', ); my $dt = $Strp->parse_datetime('23:16:42'); $Strp->format_datetime($dt); # 23:16:42 # Croak when things go wrong: my $Strp = new DateTime::Format::Strptime( pattern => '%T', locale => 'en_AU', time_zone => 'Australia/Melbourne', on_error => 'croak', ); $newpattern = $Strp->pattern('%Q'); # Unidentified token in pattern: %Q in %Q at line 34 of script.pl # Do something else when things go wrong: my $Strp = new DateTime::Format::Strptime( pattern => '%T', locale => 'en_AU', time_zone => 'Australia/Melbourne', on_error => &phone_police, ); DESCRIPTION
This module implements most of strptime(3), the POSIX function that is the reverse of strftime(3), for "DateTime". While "strftime" takes a "DateTime" and a pattern and returns a string, "strptime" takes a string and a pattern and returns the "DateTime" object associated. CONSTRUCTOR
o new( pattern=>$strptime_pattern ) Creates the format object. You must specify a pattern, you can also specify a "time_zone" and a "locale". If you specify a time zone then any resulting "DateTime" object will be in that time zone. If you do not specify a "time_zone" parameter, but there is a time zone in the string you pass to "parse_datetime", then the resulting "DateTime" will use that time zone. You can optionally use an on_error parameter. This parameter has three valid options: o 'undef' (not undef, 'undef', it's a string not an undefined value) This is the default behavior. The module will return undef whenever it gets upset. The error can be accessed using the $object->errstr method. This is the ideal behaviour for interactive use where a user might provide an illegal pattern or a date that doesn't match the pattern. o 'croak' (not croak, 'croak', it's a string, not a function) This used to be the default behaviour. The module will croak with an error message whenever it gets upset. o sub{...} or &subname When given a code ref, the module will call that sub when it gets upset. The sub receives two parameters: the object and the error message. Using these two it is possible to emulate the 'undef' behavior. (Returning a true value causes the method to return undef. Returning a false value causes the method to bravely continue): sub{$_[0]->{errmsg} = $_[1]; 1}, METHODS
This class offers the following methods. o parse_datetime($string) Given a string in the pattern specified in the constructor, this method will return a new "DateTime" object. If given a string that doesn't match the pattern, the formatter will croak or return undef, depending on the setting of on_error in the constructor. o format_datetime($datetime) Given a "DateTime" object, this methods returns a string formatted in the object's format. This method is synonymous with "DateTime"'s strftime method. o locale($locale) When given a locale or "DateTime::Locale" object, this method sets its locale appropriately. If the locale is not understood, the method will croak or return undef (depending on the setting of on_error in the constructor) If successful this method returns the current locale. (After processing as above). o pattern($strptime_pattern) When given a pattern, this method sets the object's pattern. If the pattern is invalid, the method will croak or return undef (depending on the value of the "on_error" parameter) If successful this method returns the current pattern. (After processing as above) o time_zone($time_zone) When given a name, offset or "DateTime::TimeZone" object, this method sets the object's time zone. This effects the "DateTime" object returned by parse_datetime If the time zone is invalid, the method will croak or return undef (depending on the value of the "on_error" parameter) If successful this method returns the current time zone. (After processing as above) o errmsg If the on_error behavior of the object is 'undef', error messages with this method so you can work out why things went wrong. This code emulates a $DateTime::Format::Strptime with the "on_error" parameter equal to 'croak': "$Strp-"pattern($pattern) or die $DateTime::Format::Strptime::errmsg> EXPORTS
There are no methods exported by default, however the following are available: o strptime($strptime_pattern, $string) Given a pattern and a string this function will return a new "DateTime" object. o strftime($strftime_pattern, $datetime) Given a pattern and a "DateTime" object this function will return a formatted string. STRPTIME PATTERN TOKENS
The following tokens are allowed in the pattern string for strptime (parse_datetime): o %% The % character. o %a or %A The weekday name according to the current locale, in abbreviated form or the full name. o %b or %B or %h The month name according to the current locale, in abbreviated form or the full name. o %C The century number (0-99). o %d or %e The day of month (1-31). o %D Equivalent to %m/%d/%y. (This is the American style date, very confusing to non-Americans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard pattern is %F.) o %F Equivalent to %Y-%m-%d. (This is the ISO style date) o %g The year corresponding to the ISO week number, but without the century (0-99). o %G The year corresponding to the ISO week number. o %H The hour (0-23). o %I The hour on a 12-hour clock (1-12). o %j The day number in the year (1-366). o %m The month number (1-12). o %M The minute (0-59). o %n Arbitrary whitespace. o %N Nanoseconds. For other sub-second values use "%[number]N". o %p The equivalent of AM or PM according to the locale in use. (See DateTime::Locale) o %r Equivalent to %I:%M:%S %p. o %R Equivalent to %H:%M. o %s Number of seconds since the Epoch. o %S The second (0-60; 60 may occur for leap seconds. See DateTime::LeapSecond). o %t Arbitrary whitespace. o %T Equivalent to %H:%M:%S. o %U The week number with Sunday the first day of the week (0-53). The first Sunday of January is the first day of week 1. o %u The weekday number (1-7) with Monday = 1. This is the "DateTime" standard. o %w The weekday number (0-6) with Sunday = 0. o %W The week number with Monday the first day of the week (0-53). The first Monday of January is the first day of week 1. o %y The year within century (0-99). When a century is not otherwise specified, values in the range 69-99 refer to years in the twentieth century (1969-1999); values in the range 00-68 refer to years in the twenty-first century (2000-2068). o %Y The year, including century (for example, 1991). o %z An RFC-822/ISO 8601 standard time zone specification. (For example +1100) [See note below] o %Z The timezone name. (For example EST -- which is ambiguous) [See note below] o %O This extended token allows the use of Olson Time Zone names to appear in parsed strings. NOTE: This pattern cannot be passed to "DateTime"'s "strftime()" method, but can be passed to "format_datetime()". AUTHOR EMERITUS
This module was created by Rick Measham. BUGS
Please report any bugs or feature requests to "bug-datetime-format-strptime@rt.cpan.org", or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SEE ALSO
"datetime@perl.org" mailing list. http://datetime.perl.org/ perl, DateTime, DateTime::TimeZone, DateTime::Locale AUTHOR
Dave Rolsky <autarch@urth.org> COPYRIGHT AND LICENSE
This software is Copyright (c) 2010 by Dave Rolsky. This is free software, licensed under: The Artistic License 2.0 perl v5.10.1 2010-10-16 DateTime::Format::Strptime(3pm)
All times are GMT -4. The time now is 01:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy