Date function

Discussion in 'Perl Misc' started by K.J. 44, Nov 28, 2006.

  1. K.J. 44

    K.J. 44 Guest

    Hi,

    is there a date function in Perl or do I have to try to find a module?

    What i need is to be able to come up with the current date, then
    subtract one day, and add that to the file name in the form of:

    YYYY-MM-DD-RestOfFileName.txt

    The script I have written parses these files and I want to run it every
    night after midnight on the previous days file which has the naming
    convention above.

    I am running ActiveState Perl in a Windows environment.

    Thanks.
     
    K.J. 44, Nov 28, 2006
    #1
    1. Advertising

  2. K.J. 44

    K.J. 44 Guest

    I will try that. Thank you!

    Sharif Islam wrote:
    > K.J. 44 wrote:
    > > Hi,
    > >
    > > is there a date function in Perl or do I have to try to find a module?
    > >
    > > What i need is to be able to come up with the current date, then
    > > subtract one day, and add that to the file name in the form of:
    > >
    > > YYYY-MM-DD-RestOfFileName.txt
    > >
    > > The script I have written parses these files and I want to run it every
    > > night after midnight on the previous days file which has the naming
    > > convention above.
    > >
    > > I am running ActiveState Perl in a Windows environment.

    >
    >
    > You can use the Date::Format module.
    >
    > use strict
    > use Date::Format;
    > my $yesterday = time() - ( 24 * 60 * 60 );
    > my $prefix= time2str("%Y-%m-%d", $yesterday);
    > # YYYY-MM-DD-RestOfFileName.txt
    > print $prefix."-RestOfFileName.txt"
     
    K.J. 44, Nov 28, 2006
    #2
    1. Advertising

  3. K.J. 44

    boyd Guest

    In article <>,
    "K.J. 44" <> wrote:

    > Hi,
    >
    > is there a date function in Perl or do I have to try to find a module?
    >
    > What i need is to be able to come up with the current date, then
    > subtract one day, and add that to the file name in the form of:
    >
    > YYYY-MM-DD-RestOfFileName.txt
    >
    > The script I have written parses these files and I want to run it every
    > night after midnight on the previous days file which has the naming
    > convention above.
    >
    > I am running ActiveState Perl in a Windows environment.
    >
    > Thanks.


    There are built-in functions and modules, and numerous ones in the CPAN
    library. I usually build this kind of thing myself, finding it quicker
    than trying to find the right module.

    something like:

    my( $day, $mon, $yr ) = ( localtime( time - 24*3600) )[3, 4, 5];
    $yr += 1900;
    $mon += 1;
    my $str = sprintf '%4d-%02d-%02d', $yr, $mon, $day;

    would give you the prefix string for your filename.

    Boyd
     
    boyd, Nov 28, 2006
    #3
  4. Sharif Islam wrote:
    > You can use the Date::Format module.
    >
    > use strict
    > use Date::Format;
    > my $yesterday = time() - ( 24 * 60 * 60 );
    > my $prefix= time2str("%Y-%m-%d", $yesterday);
    > # YYYY-MM-DD-RestOfFileName.txt
    > print $prefix."-RestOfFileName.txt"


    Or you can stick to the builtin functions and the standard module
    Time::Local:

    use Time::Local;
    my $midnight = timelocal 0, 0, 0, (localtime)[3..5];
    my ($d, $m, $y) = (localtime $midnight-40000)[3..5];
    print 'Yesterday: ',
    sprintf('%d-%02d-%02d', $y+1900, $m+1, $d), "\n";

    Note that Sharif's solution doesn't address the DST problem.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 28, 2006
    #4
  5. Sharif Islam wrote:
    > K.J. 44 wrote:
    > > Hi,
    > >
    > > is there a date function in Perl or do I have to try to find a module?
    > >
    > > What i need is to be able to come up with the current date, then
    > > subtract one day, and add that to the file name in the form of:
    > >
    > > YYYY-MM-DD-RestOfFileName.txt
    > >
    > > The script I have written parses these files and I want to run it every
    > > night after midnight on the previous days file which has the naming
    > > convention above.
    > >
    > > I am running ActiveState Perl in a Windows environment.

    >
    >
    > You can use the Date::Format module.


    Or just the POSIX module's strftime().
     
    Brian McCauley, Nov 28, 2006
    #5
  6. boyd wrote:
    > something like:
    >
    > my( $day, $mon, $yr ) = ( localtime( time - 24*3600) )[3, 4, 5];
    > $yr += 1900;
    > $mon += 1;
    > my $str = sprintf '%4d-%02d-%02d', $yr, $mon, $day;
    >
    > would give you the prefix string for your filename.


    What about DST? ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 28, 2006
    #6
  7. K.J. 44

    boyd Guest

    In article <>,
    Gunnar Hjalmarsson <> wrote:

    > boyd wrote:
    > > something like:
    > >
    > > my( $day, $mon, $yr ) = ( localtime( time - 24*3600) )[3, 4, 5];
    > > $yr += 1900;
    > > $mon += 1;
    > > my $str = sprintf '%4d-%02d-%02d', $yr, $mon, $day;
    > >
    > > would give you the prefix string for your filename.

    >
    > What about DST? ;-)


    Good point. So my method would mess up on one of the DST changeovers if
    it ran between midnight and 0100 local. Is that right? What is the
    "40000" for in your script?

    Boyd
     
    boyd, Nov 28, 2006
    #7
  8. boyd wrote:
    > In article <>,
    > Gunnar Hjalmarsson <> wrote:
    >>boyd wrote:
    >>>something like:
    >>>
    >>>my( $day, $mon, $yr ) = ( localtime( time - 24*3600) )[3, 4, 5];
    >>>$yr += 1900;
    >>>$mon += 1;
    >>>my $str = sprintf '%4d-%02d-%02d', $yr, $mon, $day;
    >>>
    >>>would give you the prefix string for your filename.

    >>
    >>What about DST? ;-)

    >
    > Good point. So my method would mess up on one of the DST changeovers if
    > it ran between midnight and 0100 local. Is that right?


    Yeah, that's what I'm thinking of. Maybe not a disaster in this case,
    but always worth considering when dealing with dates.

    > What is the "40000" for in your script?


    That's 'approximately' 12 hours, i.e. well enough time to make the
    result DST safe. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 28, 2006
    #8
  9. K.J. 44

    boyd Guest

    In article <>,
    Gunnar Hjalmarsson <> wrote:

    > boyd wrote:
    > > In article <>,
    > > Gunnar Hjalmarsson <> wrote:
    > >>boyd wrote:
    > >>>something like:
    > >>>
    > >>>my( $day, $mon, $yr ) = ( localtime( time - 24*3600) )[3, 4, 5];
    > >>>$yr += 1900;
    > >>>$mon += 1;
    > >>>my $str = sprintf '%4d-%02d-%02d', $yr, $mon, $day;
    > >>>
    > >>>would give you the prefix string for your filename.
    > >>
    > >>What about DST? ;-)

    > >
    > > Good point. So my method would mess up on one of the DST changeovers if
    > > it ran between midnight and 0100 local. Is that right?

    >
    > Yeah, that's what I'm thinking of. Maybe not a disaster in this case,
    > but always worth considering when dealing with dates.
    >
    > > What is the "40000" for in your script?

    >
    > That's 'approximately' 12 hours, i.e. well enough time to make the
    > result DST safe. :)


    Thanks. I have another question. I administer a Sun network and one
    Linux box at work, both of which are isolated from the world ( I have to
    hand carry in patches, etc. ) So the upcoming DST rule change may be a
    problem. I'm sure you are aware that next March, DST starts on, IIRC,
    Mar. 11, and ends the first weekend in November. What's the best way to
    deal with the changes? (at home I have Ubuntu and Mac OS X )

    Boyd
     
    boyd, Nov 28, 2006
    #9
  10. boyd wrote:
    > I administer a Sun network and one
    > Linux box at work, both of which are isolated from the world ( I have to
    > hand carry in patches, etc. ) So the upcoming DST rule change may be a
    > problem. I'm sure you are aware that next March, DST starts on, IIRC,
    > Mar. 11, and ends the first weekend in November. What's the best way to
    > deal with the changes? (at home I have Ubuntu and Mac OS X )


    I'm anything but a DST expert, but don't all OS distros come with
    current info about switches to/from DST worldwide? I didn't know about
    the change you are referring to, maybe because I live in Sweden and
    those dates vary between countries, but if there was a late decision in
    your country, isn't there simply a patch for each OS available?

    Hopefully somebody is able to give you a more authoritative comment. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 28, 2006
    #10
  11. K.J. 44

    K.J. 44 Guest

    I am getting an error saying that it cannot find Date:Format in my
    library so I need to add it in. I have never installed a Perl Module
    and am not exactly sure where to start. I was at:

    http://www.perl.com/CPAN/modules/01modules.index.html

    I am not sure how to find what i need. Thanks for any help.

    Sharif Islam wrote:
    > K.J. 44 wrote:
    > > Hi,
    > >
    > > is there a date function in Perl or do I have to try to find a module?
    > >
    > > What i need is to be able to come up with the current date, then
    > > subtract one day, and add that to the file name in the form of:
    > >
    > > YYYY-MM-DD-RestOfFileName.txt
    > >
    > > The script I have written parses these files and I want to run it every
    > > night after midnight on the previous days file which has the naming
    > > convention above.
    > >
    > > I am running ActiveState Perl in a Windows environment.

    >
    >
    > You can use the Date::Format module.
    >
    > use strict
    > use Date::Format;
    > my $yesterday = time() - ( 24 * 60 * 60 );
    > my $prefix= time2str("%Y-%m-%d", $yesterday);
    > # YYYY-MM-DD-RestOfFileName.txt
    > print $prefix."-RestOfFileName.txt"
     
    K.J. 44, Dec 1, 2006
    #11
  12. K.J. 44

    J. Gleixner Guest

    K.J. 44 wrote:
    > I am getting an error saying that it cannot find Date:Format in my
    > library so I need to add it in. I have never installed a Perl Module
    > and am not exactly sure where to start. I was at:
    >
    > http://www.perl.com/CPAN/modules/01modules.index.html
    >
    > I am not sure how to find what i need. Thanks for any help.


    First, since this is a separate question, use a more appropriate
    subject, e.g. "How to install modules". Second, please stop top-posting.
    Third, to find the module, take a look at http://search.cpan.org/.
    Finally, to answer your question on how to install it, read the
    documentation for your installation or search the Internet on something
    relevant, such as 'perl "how to install module"', and you'd likely have
    found helpful documentation already.

    ActiveState:
    http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/faq/ActivePerl-faq2.html

    *nix: perldoc -q "How do I install a module from CPAN"
     
    J. Gleixner, Dec 1, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    645
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,361
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    565
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    754
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    425
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page