Change date on-the-fly

Change date on-the-fly

am 18.10.2007 00:42:27 von Peter Jamieson

Hi all,
I collect data from a web site at irregular intervals using a script
part of which is below. In the code snippet shown I manually
insert date values for $from_date and $to_date, save and close the
script then run it from the command line.

I would like to avoid the manual step of entering the $from_date and
$to_date
values, instead have the code determine them for me based on a couple of
rules:

1. The $to_date is always "yesterday" ie the day before the current date
2. The $from_date is always the day after the previous $to_date, that is
the $to_date used when the script was previously run....the idea being
as time passes to cover all dates.

For example if $from_date = '2007/10/13' and $to_date = '2007/10/15'
and the script was run then if I want to run the script today, 2007/10/18
I need the script to set $from_date = '2007/10/16' and $to_date =
'2007/10/17'

I *think* I need to store the most recently completed date range within the
script
somehow and then update it after each run or before each new but cannot see
how.
Hope this makes sense!....any suggestions appreciated...cheers, Peter

#!/usr/bin/perl -w
use strict;
use warnings;
use HTTP::Cookies;
use LWP;
use Time::Local;
use Win32::ODBC;

my($db) = new Win32::ODBC('expenses');

# YYYY/MM/DD format
my $from_date = '2007/10/13';
my $to_date = '2007/10/15';

my ($from_year, $from_mon, $from_day) = split('/', $from_date);
my ($to_year, $to_mon, $to_day) = split('/', $to_date);

my $from_datetime = timelocal(0,0,0,$from_day,$from_mon-1,$from_year);
my $to_datetime = timelocal(0,0,0,$to_day,$to_mon-1,$to_year);

# snipped rest of Perl script to collect data for the stipulated date
range......

Re: Change date on-the-fly

am 18.10.2007 01:51:43 von unknown

Post removed (X-No-Archive: yes)

Re: Change date on-the-fly

am 18.10.2007 02:53:06 von Gunnar Hjalmarsson

all mail refused wrote:
> Use $^T as the time (in seconds) the script started executing.

A warning: When running under mod_perl, $^T contains the time when the
web server was started rather than when the script started executing.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Change date on-the-fly

am 18.10.2007 03:48:12 von Joe Smith

Peter Jamieson wrote:

> 1. The $to_date is always "yesterday" ie the day before the current date
> 2. The $from_date is always the day after the previous $to_date

perl -le 'print "Today is ".localtime(time); print "Yesterday was ".localtime(time-86400); print "Day before was ".localtime(time-2*86400)'
Today is Wed Oct 17 18:46:56 2007
Yesterday was Tue Oct 16 18:46:56 2007
Day before was Mon Oct 15 18:46:56 2007

-Joe

Re: Change date on-the-fly

am 18.10.2007 07:48:01 von Peter Jamieson

Thanks to Gunnar Hjalmarsson, Joe Smith and "all mail refused" for
your constructive help....much appreciated!
I will look at your suggestions asap...cheers, Peter

Re: Change date on-the-fly

am 18.10.2007 08:09:14 von Ron Bergin

On Oct 17, 3:42 pm, "Peter Jamieson"
wrote:
> Hi all,
> I collect data from a web site at irregular intervals using a script
> part of which is below. In the code snippet shown I manually
> insert date values for $from_date and $to_date, save and close the
> script then run it from the command line.
>
> I would like to avoid the manual step of entering the $from_date and
> $to_date
> values, instead have the code determine them for me based on a couple of
> rules:
>
> 1. The $to_date is always "yesterday" ie the day before the current date
> 2. The $from_date is always the day after the previous $to_date, that is
> the $to_date used when the script was previously run....the idea being
> as time passes to cover all dates.
>
> For example if $from_date = '2007/10/13' and $to_date = '2007/10/15'
> and the script was run then if I want to run the script today, 2007/10/18
> I need the script to set $from_date = '2007/10/16' and $to_date =
> '2007/10/17'
>
> I *think* I need to store the most recently completed date range within the
> script
> somehow and then update it after each run or before each new but cannot see
> how.
> Hope this makes sense!....any suggestions appreciated...cheers, Peter
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
> use HTTP::Cookies;
> use LWP;
> use Time::Local;
> use Win32::ODBC;
>
> my($db) = new Win32::ODBC('expenses');
>
> # YYYY/MM/DD format
> my $from_date = '2007/10/13';
> my $to_date = '2007/10/15';
>
> my ($from_year, $from_mon, $from_day) = split('/', $from_date);
> my ($to_year, $to_mon, $to_day) = split('/', $to_date);
>
> my $from_datetime = timelocal(0,0,0,$from_day,$from_mon-1,$from_year);
> my $to_datetime = timelocal(0,0,0,$to_day,$to_mon-1,$to_year);
>
> # snipped rest of Perl script to collect data for the stipulated date
> range......

I would use the strftime funcion from POSIX module.

use POSIX qw(strftime);

use constant ONE_DAY => 86400;

my $from_date = strftime("%Y/%m/%d", localtime(time - 3 * ONE_DAY));
my $to_date = strftime("%Y/%m/%d", localtime(time - ONE_DAY));

Re: Change date on-the-fly

am 18.10.2007 17:47:11 von ansok

In article <1192662748.758544.215190@v23g2000prn.googlegroups.com>,
Ron Bergin wrote:
>I would use the strftime funcion from POSIX module.
>
>use POSIX qw(strftime);
>
>use constant ONE_DAY => 86400;
>
>my $from_date = strftime("%Y/%m/%d", localtime(time - 3 * ONE_DAY));
>my $to_date = strftime("%Y/%m/%d", localtime(time - ONE_DAY));

Keep in mind that this may not give the desired results around
the daylight-savings transitions.

If you know that your script will never (what, never?) be run
2300-0100, or if you know that the effective timezone does not
use DST, then this may not be an issue for you. Otherwise,
I'd look into one of the Date:: modules instead.

There is something to be said for storing the last-processed
dates in a file -- if for some reason one run doesn't happen
(system was down, perhaps), then the next run will handle the
data that would have been processed (as well as any new data).

Gary

Re: Change date on-the-fly

am 18.10.2007 19:32:33 von Peter Jamieson

"Ron Bergin" wrote in message
news:1192662748.758544.215190@v23g2000prn.googlegroups.com.. .
> I would use the strftime funcion from POSIX module.
>
> use POSIX qw(strftime);
>
> use constant ONE_DAY => 86400;
>
> my $from_date = strftime("%Y/%m/%d", localtime(time - 3 * ONE_DAY));
> my $to_date = strftime("%Y/%m/%d", localtime(time - ONE_DAY));
>

Thx Ron!
I will have a look at POSIX.
Your assistance is appreciated, cheers, Peter

Re: Change date on-the-fly

am 18.10.2007 19:32:33 von Peter Jamieson

"Gary E. Ansok" wrote in message
news:ff7v5v$chc$1@naig.caltech.edu...
> In article <1192662748.758544.215190@v23g2000prn.googlegroups.com>,
> Ron Bergin wrote:
>>I would use the strftime funcion from POSIX module.
>>
>>use POSIX qw(strftime);
>>
>>use constant ONE_DAY => 86400;
>>
>>my $from_date = strftime("%Y/%m/%d", localtime(time - 3 * ONE_DAY));
>>my $to_date = strftime("%Y/%m/%d", localtime(time - ONE_DAY));
>
> Keep in mind that this may not give the desired results around
> the daylight-savings transitions.
>
> If you know that your script will never (what, never?) be run
> 2300-0100, or if you know that the effective timezone does not
> use DST, then this may not be an issue for you. Otherwise,
> I'd look into one of the Date:: modules instead.
>
> There is something to be said for storing the last-processed
> dates in a file -- if for some reason one run doesn't happen
> (system was down, perhaps), then the next run will handle the
> data that would have been processed (as well as any new data).
>
> Gary

Thx for the comments Gary!
I am thinking this is the way to go as the file can be checked
periodically.
Your help is appreciated, cheers, Peter