Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

potential date and time issues / code cleanup

by Conal (Beadle)
on Mar 23, 2008 at 05:15 UTC ( [id://675737]=perlquestion: print w/replies, xml ) Need Help??

Conal has asked for the wisdom of the Perl Monks concerning the following question:

Hi can anyone kindly help me out with making this code more robust and useful.. for the purposes of the data i am working with days start at 5pm and finish at 4.59pm the next day. i have a data file for each day containing my readings

i am not very good at this so please point out any others areas i might be messing up or could do better if you feel so inclined

use File::Copy; use Time::Local; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdist) = localtime +time; my $week="daily/Sun-23-Mar-2008/"; .... other code ######### daily file creation ######## ######################## #create new file for new day @ 1700 if ($hour == 17 && $min ==0 && min !=30) { qx(touch $week.$mday); } ##################### ### calculate REAL day ### by subtracting 1 from mday if hour > 00 and < 1700 ### WHATABOUT THE MONTH??? my $filemday = $mday; if ($hour < 17 && $hour >= 0) { $filemday = $filemday-1; } my $dayfile = $week.$filemday.".txt"; ### print data to file if (-e $dayfile) { open (DATA,">>$dayfile"); print DATA ""$hour:$min,some data\n"; close (DATA); } ... other code
now obviously subtracting 1 from the $mday variable is gonna leave me in a whole load crap. Can anyone suggest any ideas for what i can do to fix this please?

thanks.

conal.

Replies are listed 'Best First'.
Re: potential date and time issues / code cleanup
by ikegami (Patriarch) on Mar 23, 2008 at 05:21 UTC

    Getting the previous day's date is a FAQ. You should find a variety of answers on PM. One way is the following:

    # Jan = 1 my ($yest_y, $yest_m, $yest_d) = ( gmtime( timegm(0,0,0,$today_d,$today_m-1,$today_y) - 24*60*60 ) )[5,4,3]; $yest_m += 1; $yest_y += 1900;

    Use timegm + gmtime even for local dates.

Re: potential date and time issues / code cleanup
by walto (Pilgrim) on Mar 23, 2008 at 06:43 UTC
    I like Date::Manip to handle date and time issues (Date::Manip).

    I am not sure if this is intentional but
    if ($hour == 17 && $min ==0 && min !=30)

    min !=30 could be a typo for $min !=30
    but is $min !=30 not always true if $min ==0 ?
      ha .. good spot walto .. thankyou! guess that relatively minor issue infers most of the other code is pretty ok.. heh .. ? ;p

      thanks to everyones reply ..i have a few ideas now for when i go back at my code.

      conal.
Re: potential date and time issues / code cleanup
by nefigah (Monk) on Mar 23, 2008 at 06:22 UTC

    There's the Date::Calc CPAN module:

    use warnings; use strict; use Date::Calc::Object; my $date = Date::Calc->today - 1;

    Which will get you yesterday's date in a pretty clean way.


    I'm a peripheral visionary... I can see into the future, but just way off to the side.

Re: potential date and time issues / code cleanup
by wade (Pilgrim) on Mar 23, 2008 at 06:40 UTC
    First of all, don't forget the:
    use strict; use warnings;
    If you're going to do much with dates, you should check out Date::Simple or, for more complicated stuff DateTime over on CPAN. They've worked out all the details so that you can use your creative juices on newer, more interesting problems.
    --
    Wade
Re: potential date and time issues / code cleanup
by CountZero (Bishop) on Mar 23, 2008 at 19:23 UTC
    if ($hour == 17 && $min ==0 ...

    Unless your program is running continuously (which seems a waste of processorcycles) , you only have a one minute window in which this test will be triggered, so you should be pretty sure that indeed you do not miss this window.

    On the other hand if your program does run continuously, you have to test whether your file exists already or you will be overwriting it several times within the window.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://675737]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found