phpman > perldoc > date(1)

Markdown | JSON | MCP    

Found in /usr/share/perl/5.34/pod/perlfaq4.pod
  How can I compare two dates and find the difference?
    (contributed by brian d foy)

    You could just store all your dates as a number and then subtract. Life
    isn't always that simple though.

    The Time::Piece module, which comes with Perl, replaces localtime with a
    version that returns an object. It also overloads the comparison
    operators so you can compare them directly:

        use Time::Piece;
        my $date1 = localtime( $some_time );
        my $date2 = localtime( $some_other_time );

        if( $date1 < $date2 ) {
            print "The date was in the past\n";
        }

    You can also get differences with a subtraction, which returns a
    Time::Seconds object:

        my $date_diff = $date1 - $date2;
        print "The difference is ", $date_diff->days, " days\n";

    If you want to work with formatted dates, the Date::Manip, Date::Calc,
    or DateTime modules can help you.

  How do I find yesterday's date?
    (contributed by brian d foy)

    To do it correctly, you can use one of the "Date" modules since they
    work with calendars instead of times. The DateTime module makes it
    simple, and give you the same time of day, only the day before, despite
    daylight saving time changes:

        use DateTime;

        my $yesterday = DateTime->now->subtract( days => 1 );

        print "Yesterday was $yesterday\n";

    You can also use the Date::Calc module using its "Today_and_Now"
    function.

        use Date::Calc qw( Today_and_Now Add_Delta_DHMS );

        my @date_time = Add_Delta_DHMS( Today_and_Now(), -1, 0, 0, 0 );

        print "@date_time\n";

    Most people try to use the time rather than the calendar to figure out
    dates, but that assumes that days are twenty-four hours each. For most
    people, there are two days a year when they aren't: the switch to and
    from summer time throws this off. For example, the rest of the
    suggestions will be wrong sometimes:

    Starting with Perl 5.10, Time::Piece and Time::Seconds are part of the
    standard distribution, so you might think that you could do something
    like this:

        use Time::Piece;
        use Time::Seconds;

        my $yesterday = localtime() - ONE_DAY; # WRONG
        print "Yesterday was $yesterday\n";

    The Time::Piece module exports a new "localtime" that returns an object,
    and Time::Seconds exports the "ONE_DAY" constant that is a set number of
    seconds. This means that it always gives the time 24 hours ago, which is
    not always yesterday. This can cause problems around the end of daylight
    saving time when there's one day that is 25 hours long.

    You have the same problem with Time::Local, which will give the wrong
    answer for those same special cases:

        # contributed by Gunnar Hjalmarsson
         use Time::Local;
         my $today = timelocal 0, 0, 12, ( localtime )[3..5];
         my ($d, $m, $y) = ( localtime $today-86400 )[3..5]; # WRONG
         printf "Yesterday: %d-%02d-%02d\n", $y+1900, $m+1, $d;

  How do I validate input?
    (contributed by brian d foy)

    There are many ways to ensure that values are what you expect or want to
    accept. Besides the specific examples that we cover in the perlfaq, you
    can also look at the modules with "Assert" and "Validate" in their
    names, along with other modules such as Regexp::Common.

    Some modules have validation for particular types of input, such as
    Business::ISBN, Business::CreditCard, Email::Valid, and
    Data::Validate::IP.

Found in /usr/share/perl/5.34/pod/perlfaq5.pod
  How do I randomly update a binary file?
    If you're just trying to patch a binary, in many cases something as
    simple as this works:

        perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs

    However, if you have fixed sized records, then you might do something
    more like this:

        my $RECSIZE = 220; # size of record, in bytes
        my $recno   = 37;  # which record to update
        open my $fh, '+<', 'somewhere' or die "can't update somewhere: $!";
        seek $fh, $recno * $RECSIZE, 0;
        read $fh, $record, $RECSIZE == $RECSIZE or die "can't read record $recno: $!";
        # munge the record
        seek $fh, -$RECSIZE, 1;
        print $fh $record;
        close $fh;

    Locking and error checking are left as an exercise for the reader. Don't
    forget them or you'll be quite sorry.

Found in /usr/share/perl/5.34/pod/perlfaq8.pod
  How do I set the time and date?
    Assuming you're running under sufficient permissions, you should be able
    to set the system-wide date and time by running the date(1) program.
    (There is no way to set the time and date on a per-process basis.) This
    mechanism will work for Unix, MS-DOS, Windows, and NT; the VMS
    equivalent is "set time".

    However, if all you want to do is change your time zone, you can
    probably get away with setting an environment variable:

        $ENV{TZ} = "MST7MDT";           # Unixish
        $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
        system('trn', 'comp.lang.perl.misc');

date(1)
Found in /usr/share/perl/5.34/pod/perlfaq4.pod Found in /usr/share/perl/5.34/pod/perlfaq5.pod Found in /usr/share/perl/5.34/pod/perlfaq8.pod

Generated by phpman v3.7.12 Author: Che Dong Under GNU General Public License
2026-06-13 12:42 @216.73.216.28
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 TransitionalValid CSS!

^_back to top