Calculate yesterdays date 
Author Message
 Calculate yesterdays date



...

Quote:
> windlord:~> wc /usr/pubsw/lib/perl5/site_perl/Date/Manip.pm
>    5378   23939  184930 /usr/pubsw/lib/perl5/site_perl/Date/Manip.pm
> windlord:~> time perl -MDate::Manip -e1
> 0.36u 0.05s 0:00.38 107.8%

> Eek.

This seems like a good post on which to hang a question that has been
troubling me for some time (pun intended).

Many Frequently Asked Questions here seem to resolve simply to:  Convert
your input date/time string to Unix-Epoch seconds, then do some simple
manipulations.  The Frequent Answers point to these humongous, very
general Date modules.

But (as I have posted several times recently), conversion from a broken-
down date/time array to epoch seconds takes only a few lines of trivial
but very ugly code.  It does not require a module at all.  (I know there
is a 'timelocal' function, but I believe it is embedded in one of these
big modules.)

As a relative newcomer to Perl, I find CPAN to be a very rich collection
of well-tested, formally-documented modules.  I have not found a
comparable collection of usable, short code snippets (though perhaps
CPAN started out that way).

What I would like to see is a dynamic on-line equivalent of the kind of
code published in The Perl Cookbook, contributed to and reviewed
collaboratively, and with a searchable index.  Such collections exist
for modules (on CPAN), and for complete programs elsewhere (often
maligned here for their style or quality).  The Perl Journal often
publishes useful one-liners.

How about an archive (in CPAN?) of 'small' functions or snippets, from
one line to one page of code?  I wouldn't be able to organize it, but I
certainly would be able to contribute.

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.*-*-*.com/



Sun, 29 Jul 2001 03:00:00 GMT  
 Calculate yesterdays date
[ comp.lang.perl.modules removed from Newsgroups: ]

Quote:

> What I would like to see is a dynamic on-line equivalent of the kind of
> code published in The Perl Cookbook, contributed to and reviewed
> collaboratively, and with a searchable index.

Done.

The Perl Function Repository-
  http://www.*-*-*.com/ ~dgris/perl/pfr/

Right now the only piece of code included in the PFR is Russ
Allbery's yesterday() subroutine that was posted here yesterday.
(Russ, if you don't want the code in there, let me know).

Now, before everyone gets e{*filter*}d and starts flooding me with
code pieces to include- *don't send code directly to me.

I'd like to see any code that is intended for the PFR to be reviewed
here beforehand.  I believe that the peer review that open source
offers is the strongest advantage that our model has over the
proprietary development model and I'd like to see us exploit that
advantage as much as possible. Besides, I trust the ability of the
general perl community to catch bizarre bugs and gotchas much more
than I trust my own ability to do so.

If you need to CC me on posts about code that should be in the PFR,
feel free, but it isn't necessary.  I read clpm(od|isc) religiously
and am not likely to miss any interesting discussion.  If you'd like
to prepend `PFR:' to the subject of articles to make sure that I don't
miss them, that would be a good thing (plus it would allow people
uninterested in the project to easily skip articles related to it).

Finally, this was put together in about an hour last night and I
fully expect that it has more than its share of bugs.  I'll clean
them up as we go.

Have the appropriate amount of fun.

dgris
--





Mon, 30 Jul 2001 03:00:00 GMT  
 Calculate yesterdays date


Quote:

>The Perl Function Repository-
>  http://moiraine.dimensional.com/~dgris/perl/pfr/

>If you need to CC me on posts about code that should be in the PFR,
>feel free, but it isn't necessary.  I read clpm(od|isc) religiously
>and am not likely to miss any interesting discussion.  If you'd like
>to prepend `PFR:' to the subject of articles to make sure that I don't
>miss them, that would be a good thing (plus it would allow people
>uninterested in the project to easily skip articles related to it).

I've thought about doing something like this myself, but don't have
the resources.  There are a bunch of things that don't really need
modules to solve, just a good function or 2 to paste into the code.
Thanks for doing this.  Here is my suggestion for the next addition:
This was posted by Jeff Godden a while back (I don't take any credit),
to compute julian dates:

# ----- this will return day number 719469 for 1970/1/1
sub jday {
 # note screwed up american date element ordering

 my($y) = $year + ($month-3)/12;
 int(367*$y+0.625)-2*int($y)+int($y/4)-int($y/100)+int($y/400)+$day;

Quote:
}

Note, this is off from the "usual" definition of Julian days by a
constant, but if that is important to you, then just add the constant
back in.  It also does not take into account the week that was lost
when most of the world switched to the Gregorian calender, but if all
you want to do is compute the number of days between 2 recent (100
years or so) to future dates, it works fine.  If there is interest,
one of these days I'll try to write the routine to go from the Julian
day to month/day/year.

Thanks again for being the one to manage the PFR.

--
-------------------------------------------------------------------------------
     Gregory L. Snow         | Barth's Distinction:
     (Greg)                  |  There are two types of people: those who divide



Mon, 30 Jul 2001 03:00:00 GMT  
 Calculate yesterdays date

Daniel> Right now the only piece of code included in the PFR is Russ
Daniel> Allbery's yesterday() subroutine that was posted here yesterday.
Daniel> (Russ, if you don't want the code in there, let me know).

Will it be changed to thedaybefore() tomorrow? :)

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Mon, 30 Jul 2001 03:00:00 GMT  
 Calculate yesterdays date
[ Posted and mailed. ]

Quote:

> The Perl Function Repository-
>   http://moiraine.dimensional.com/~dgris/perl/pfr/
> Right now the only piece of code included in the PFR is Russ Allbery's
> yesterday() subroutine that was posted here yesterday.  (Russ, if you
> don't want the code in there, let me know).

Great with me.  Here's a new version incorporating some suggestions from
Larry Rosler and with a clear copyright statement.

sub yesterday {
    my $now  = defined $_[0] ? $_[0] : time;
    my $then = time - 60 * 60 * 24;
    my $ndst = (localtime $now)[8] > 0;
    my $tdst = (localtime $then)[8] > 0;
    $then - ($tdst - $ndst) * 60 * 60;

Quote:
}

# Should give you "this time yesterday" in seconds since epoch relative to
# the first argument or the current time if no argument is given and
# suitable for passing to localtime or whatever else you need to do with
# it.  $ndst is whether we're currently in daylight savings time; $tdst is
# whether the point 24 hours ago was in daylight savings time.  If $tdst
# and $ndst are the same, a boundary wasn't crossed, and the correction
# will subtract 0.  If $tdst is 1 and $ndst is 0, subtract an hour more
# from yesterday's time since we gained an extra hour while going off
# daylight savings time.  If $tdst is 0 and $ndst is 1, subtract a
# negative hour (add an hour) to yesterday's time since we lost an hour.
#
# All of this is because during those days when one switches off or onto
# DST, a "day" isn't 24 hours long; it's either 23 or 25.
#
# The explicit settings of $ndst and $tdst are necessary because localtime
# only says it returns the system tm struct, and the system tm struct at
# least on Solaris doesn't guarantee any particuliar positive value (like,
# say, 1) for isdst, just a positive value.  And that value can
# potentially be negative, if DST information isn't available (this sub
# just treats those cases like no DST).
#
# Note that between 2am and 3am on the day after the time zone switches
# off daylight savings time, the exact hour of "yesterday" corresponding
# to the current hour is not clearly defined.  Note also that if used
# between 2am and 3am the day after the change to daylight savings time,
# the result will be between 3am and 4am of the previous day; it's
# arguable whether this is correct.
#
# This sub does not attempt to deal with leap seconds (most things don't).
#

# This code is in the public domain

--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker





Mon, 30 Jul 2001 03:00:00 GMT  
 Calculate yesterdays date


says...

Quote:

> > What I would like to see is a dynamic on-line equivalent of the kind of
> > code published in The Perl Cookbook, contributed to and reviewed
> > collaboratively, and with a searchable index.

So it's appropriate that I submit one of the first functions.  I have a
couple of others to come, including light-weight versions of strftime
(time to formatted date/time) and its inverse (formatted date/time to
time).

As ground rules, I suggest that all functions be '-w' and 'use strict;'
compliant.  Lines should fit without folding; tabs should be avoided.  
Documentation is 'good', but test code might not be necessary, to keep
the volume down.

As you can see, the setup and argument checking for this function are
trivial but space-consuming.  The guts of the algorithm is also
'trivial', but quite inscrutable (to me).  Comments, suggestions, and
brickbats are welcome, of course!

Quote:
> Have the appropriate amount of fun.

You bet!  Thanks for setting this up.  Links to the Repository need to
be created from http://www.perl.com/ and other appropriate places, of
course.

*********************************************************************

#!/usr/local/bin/perl -w
use strict;

# UTC_to_Epoch: Convert UTC date/time to Unix-epoch time.
# Larry Rosler, 11 February, 1999

# UTC_to_Epoch converts a broken-down date/time array from UTC to time
# in seconds since the Unix Epoch (midnight on 1 January, 1970).  It is
# essentially the inverse of gmtime.

# The input is a list of at least three integers:
#   year   [1970 to 2099]
#   month  [1 to 12]
#   day    [1 to whatever is appropriate for the year and month]
#   hour   [0 to 23; 0 if omitted]
#   minute [0 to 59; 0 if omitted]
#   second [0 to 59; 0 if omitted]
# 'undef' is returned if the arguments do not represent a valid
# date/time on or after the Unix Epoch.

sub UTC_to_Epoch {

    defined $year && defined $mon && defined $day      or return;

    $m_day[2] = 29 unless $year % 4; # OK from 1901 through 2099.

    1970 <= $year         && $year <= 2099
    && 1 <= $mon          && $mon  <= 12
    && 1 <= $day          && $day  <= $m_day[$mon]
    && 0 <= ($hour ||= 0) && $hour <= 23
    && 0 <= ($min  ||= 0) && $min  <= 59
    && 0 <= ($sec  ||= 0) && $sec  <= 59               or return;

    # Adapted from Astronomical Computing, Sky & Telescope, May, 1984.
    24 * 60 * 60 * (367 * $year - 678972 - 40587 + int(275 * $mon / 9) +
        $day - int((int(int($year + ($mon < 9 ? -1 : 1) *
        int(abs($mon - 9) / 7)) / 100) + 1) * 3 / 4) -
        int(7 * (int(($mon + 9) / 12) + $year) / 4)) +
        60 * 60 * $hour + 60 * $min + $sec

Quote:
}

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/



Mon, 30 Jul 2001 03:00:00 GMT  
 Calculate yesterdays date
Based on some earlier discussion either here or in .misc, I was inspired
to look at s2p from the Perl distribution and in particular the &q sub
that's used in combination with heredocs.  In several places in s2p,
there's code like:

    print BODY &q(<<'EOT');
:       while ($ARGV[0] =~ /^-/) {
:           $_ = shift;
:         last if /^--/;
:           if (/^-n/) {
:               $nflag++;
:               next;
:           }
:           die "I don't recognize this switch: $_\\n";
:       }
:      
EOT

and &q cleans up the colons so that it's valid Perl code.  I decided I
really liked that method, so I've been using it with all of the heredocs
in all of the programs I've written since.

&q in s2p is Perl 4 style and assumes tabs.  Here's my version, with some
comments and a usage note.


# This sub lets you make your heredocs in your Perl script more easily
# distinguishable from the surrounding code by putting colons down the
# left-hand side.  As written, it expects either a tab or seven spaces
# between the colon and the actual text; depending on your usage, you may
# want to change that (by changing the regex).  A colon by itself on a
# line is interpreted as a blank line.
#
# Example usage (from the comp.lang.perl.moderated moderation system):
#
#     print MAIL unquote (<<"EOM");
# :       From: $maintainer
# :       To: $from
# :       Subject: comp.lang.perl.moderated registration confirmation
# :
# :       You have been registered as a poster for comp.lang.perl.moderated.
# :       All future posts from the addresses:
# :
# EOM
#

# This code is in the public domain.

--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker





Mon, 30 Jul 2001 03:00:00 GMT  
 Calculate yesterdays date

Quote:
> As ground rules, I suggest that all functions be '-w' and 'use strict;'
> compliant.  Lines should fit without folding; tabs should be avoided.
> Documentation is 'good', but test code might not be necessary, to keep
> the volume down.

I'd also recommend that people make some clear statement about copyright
or licensing.  I personally will probably put anything I write for the PFR
explicitly into the public domain, since I think worrying about licenses
with excerpts of code that small is pointless.  But another reasonable
approach may be classic:

# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

or some variation of BSD, GPL, or what have you.  But in any event, I'd
encourage contributors to make it clear one way or the other; without a
clear copyright statement, a lot of people won't be able to use the code.

--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker





Mon, 30 Jul 2001 03:00:00 GMT  
 
 [ 93 post ]  Go to page: [1] [2] [3] [4] [5] [6] [7]

 Relevant Pages 

1. Calculate yesterdays date

2. Calculate yesterdays date

3. Need program to calculate dates between 2 dates

4. Getting the date range in Unix time for yesterday

5. Yesterdays date in the following format 020628 not Fri Jun 28 19:02:58 2002

6. How to get yesterday date

7. Yesterdays Date

8. finding out yesterdays date

9. Getting Yesterday's Date

10. date, datediff, yesterday perl functions ?

11. Get the yesterday date ?

12. yesterday's date

 

 
Powered by phpBB® Forum Software