Current location: Hot Scripts Forums » Programming Languages » Perl » Newbie found a solution to a date problem.


Newbie found a solution to a date problem.

Reply
  #1 (permalink)  
Old 04-23-04, 04:57 PM
mickey_kamer mickey_kamer is offline
New Member
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Newbie found a solution to a date problem.

Hello all,

I had a problem this week. Well two actually...

1.) I am no programmer

2.) I had to calculate a date in the past based on the date that the script runs on.

Now this was easy to do if I knew both dates before hand but was a problem for me since I didn't know anything about PERL or programming. I still don't know anything about PERL or programming but I wrote the below to solve my problem and I want to pass it along to everyone else. I bet there is an easier way of doing it but I am so proud of myself for getting this far with it now I can prune a database using a script that runs from CRON. As far as I can tell, you'll need to install the Date::Calc module from CPAN to make this work for you. That is all I think you'll need to install besides of course PERL. I deleted the Shebang line becuase I know you all know that one by now.


#Build the date string
use Date::Calc qw(Add_Delta_Days);

#Gets localtime on the computer executed on.
($d, $m, $y) = (localtime)[3,4,5];

#Adjust the offset to either a neg or pos number of days.
$offset = -60;

#Formats the date and sets the offset to subtract 60 days form the #current date. This works with the first line above.
($y2, $m2, $d2) = Add_Delta_Days($y+1900, $m+1, $d, $offset);

#Checks to see if the month is greater than 10.
if ($m2<10) {$m2 = "0" . $m2;};

#Put all six numbers into one var ie 20040412.
$myDate2 = $y2 . $m2 . $d2;

As I said this was really simple but took me two days to research and understand. I hope it helps someone else.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-27-04, 02:13 PM
Roger Roger is offline
New Member
 
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by mickey_kamer
Hello all,

I had a problem this week. Well two actually...

1.) I am no programmer

2.) I had to calculate a date in the past based on the date that the script runs on.

Now this was easy to do if I knew both dates before hand but was a problem for me since I didn't know anything about PERL or programming. I still don't know anything about PERL or programming but I wrote the below to solve my problem and I want to pass it along to everyone else. I bet there is an easier way of doing it but I am so proud of myself for getting this far with it now I can prune a database using a script that runs from CRON. As far as I can tell, you'll need to install the Date::Calc module from CPAN to make this work for you. That is all I think you'll need to install besides of course PERL. I deleted the Shebang line becuase I know you all know that one by now.


#Build the date string
use Date::Calc qw(Add_Delta_Days);

#Gets localtime on the computer executed on.
($d, $m, $y) = (localtime)[3,4,5];

#Adjust the offset to either a neg or pos number of days.
$offset = -60;

#Formats the date and sets the offset to subtract 60 days form the #current date. This works with the first line above.
($y2, $m2, $d2) = Add_Delta_Days($y+1900, $m+1, $d, $offset);

#Checks to see if the month is greater than 10.
if ($m2<10) {$m2 = "0" . $m2;};

#Put all six numbers into one var ie 20040412.
$myDate2 = $y2 . $m2 . $d2;

As I said this was really simple but took me two days to research and understand. I hope it helps someone else.
Mickey,

Thanks for your post and effor, but there's a better and easier solution for what you need with the Date::Manip module, with just two lines of code:

use Date::Manip;

$60date = DateCalc("today","- 60 days",\$err);
$date = UnixDate($60date,"%Y%m%d");

I hope this helps.

Roger
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-28-04, 03:11 PM
mickey_kamer mickey_kamer is offline
New Member
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Roger
Mickey,

Thanks for your post and effor, but there's a better and easier solution for what you need with the Date::Manip module, with just two lines of code:

use Date::Manip;

$60date = DateCalc("today","- 60 days",\$err);
$date = UnixDate($60date,"%Y%m%d");

I hope this helps.

Roger
Roger,

Your solution would be quick and easy however when I tested it using what you have above I got nothing but the following errors:


Bareword found where operator expected at line 2, near "$60date"
(Missing operator before date?)
Bareword found where operator expected at line 3, near "$60date"
(Missing operator before date?)
syntax error at line 2, near "$60date "
syntax error at line 3, near "$60date"
Execution aborted due to compilation errors.
---------------------------------------------------------------------
Since I do not know why I am getting the errors I tied this:
use Date::Manip;
$date2 = DateCalc("today","-60 days",\$err);
$date = UnixDate($date2,"%Y%m%d");

Thinking the leading number in a varible would be a problem I changed from $60date to $date2.

Now I get the following error:
ERROR: Date::Manip unable to determine TimeZone.
Date::Manip:ate_TimeZone called at C:/Perl/site/lib/Date/Manip.pm line 661
Date::Manip:ate_Init() called at C:/Perl/site/lib/Date/Manip.pm line 779
Date::Manip:arseDateString('today') called at C:/Perl/site/lib/Date/Manip.pm line 1479
Date::Manip:ateCalc('today','-60 days','SCALAR(0x2924980)') called at test.pl line 2

So, I don't have a clue as to what is going on here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-28-04, 11:33 PM
SatRoger SatRoger is offline
New Member
 
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by mickey_kamer
Thinking the leading number in a varible would be a problem I changed from $60date to $date2.

Now I get the following error:
ERROR: Date::Manip unable to determine TimeZone.
Date::Manip:ate_TimeZone called at C:/Perl/site/lib/Date/Manip.pm line 661
Date::Manip:ate_Init() called at C:/Perl/site/lib/Date/Manip.pm line 779
Date::Manip:arseDateString('today') called at C:/Perl/site/lib/Date/Manip.pm line 1479
Date::Manip:ateCalc('today','-60 days','SCALAR(0x2924980)') called at test.pl line 2

So, I don't have a clue as to what is going on here.

Always remember, test before writing
As far as the second part, it looks that Date::Manip is unable to determine the Time Zone of the computer it's install, so it cannot calculate today's date!
It must be something to do with the fact the server is a Windows machine
It does work fine on a Linux machine - I've tested this time


Try setting the timezone:

use Date::Manip;

Date_Init("TZ=CDT");
$date2 = DateCalc("today","- 60 days",\$err);
$date = UnixDate($date2,"%Y%m%d");

Last edited by SatRoger; 04-28-04 at 11:35 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-09-07, 06:54 AM
crmpicco's Avatar
crmpicco crmpicco is offline
Wannabe Coder
 
Join Date: Jan 2005
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
solved my problem Mickey! cheers. Picco
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
perl newbie has a problem... fowler23 Perl 4 03-24-10 06:14 PM
Schedule articles to publish on a specific date - news publishing solution nikolaoupan Script Requests 3 12-14-04 10:51 AM
Access Database Date Problem emvanlill Windows .NET Programming 2 05-10-04 04:06 AM
Date problem Merovingian PHP 4 04-04-04 04:12 PM
Problem with date in combobox. Periodically lose valu Danie Visual Basic 1 03-04-04 03:41 PM


All times are GMT -5. The time now is 01:52 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.