Change date on-the-fly

P

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

all mail refused

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.

Use $^T as the time (in seconds) the script started executing.
Subtract 86400 to get yesterday.

Use gmtime() or localtime() if necessary to convert time-in-seconds to
human-readable values.

Store in some file (other than the script itself) a record of what times were
used during the previous execution.
 
G

Gunnar Hjalmarsson

all said:
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.
 
P

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
 
R

Ron Bergin

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));
 
G

Gary E. Ansok

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
 
P

Peter Jamieson

Ron Bergin said:
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
 
P

Peter Jamieson

Gary E. Ansok said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top