perl_date_to_seq_number_conversion

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



From: "Garry T. Williams" <garry@america.net>
Subject: Re: date conversions...
Date: Sat, 01 Jul 2000 19:18:52 -0400

At 04:23 PM 6/30/00, Kris wrote:
>hello-
>     I'm looking for some type of algorithm or code snippet
>...
>So, if I convert the first date in the file to a 1 (say it's 3/30/2000) then
>I need to convert 3/31/2000 to 2, and 4/1/2000 to 3, and so on.  Any ideas
>on how to do this?

How about something like this:

         $ cat x
         #!/usr/local/bin/perl -w
         use strict;
         use Time::Local;

         # Everything's relative to 3/30/2000
         sub MILE_STONE () { timelocal(0, 0, 0, 30, 2, 100); }

         my @some_dates = qw(
                 3/30/2000
                 3/31/2000
                 4/1/2000
                 4/2/2000
                 4/3/2000
                 6/1/2000
                 11/30/2000
                 12/1/2000
                 );

         sub relative_date {
             my $date = shift;
             my ($m, $d, $y) = split(m!/!, $date);
             my $rel = timelocal(0, 0, 0, $d, $m-1, $y-1900);
             $rel = ($rel - MILE_STONE)/(24*60*60) + 1;

             # Round because of DST
             return(sprintf("%.0f", $rel));
         }

         foreach ( @some_dates ) {
             print relative_date($_), "\n";
         }

         __END__

         $ perl x
         1
         2
         3
         4
         5
         64
         246
         247
         $

Hope this helps.

-Garry Williams

Path: nntp.stanford.edu!not-for-mail
From: Steve Revilak <srevilak@cs.umb.edu>
Newsgroups: comp.lang.perl.moderated,comp.lang.perl
Subject: Re: date conversions...
Date: Sat, 1 Jul 2000 19:32:22 -0400 (EDT)
Lines: 30

===








From: hans-georg@rist.net (Hans-Georg Rist)
Subject: Re: date conversions...
Date: Tue, 04 Jul 2000 21:15:02 GMT

On Fri, 30 Jun 2000 14:23:18 -0600, "Kris" <kaweed@micron.com> wrote:

>    I'm looking for some type of algorithm or code snippet (even an idea of
>a direction to go in) on creating a numerical list from a dated list.  I'm
>using a plotting program to plot some dates, but it won't accept dates in
>the format I have.  So what I need to do is convert each date into a number.
>So, if I convert the first date in the file to a 1 (say it's 3/30/2000) then
>I need to convert 3/31/2000 to 2, and 4/1/2000 to 3, and so on.  Any ideas
>on how to do this?  The other problem I have is that there are often
>re-ocurring dates and they aren't all in order, so I need all instances of
>4/1/2000 throughout the file to be a 3.  Any ideas?

Use Julian dates:

sub j_date {

# Arguments: year, month (1=Jan, 2=Feb, ... 12=Dec), day (1...31)
#   Returns: Julian date (days since 1.1.4713 B.C.)

# Algorithm: R. G. Tantzen

my( $year, $month, $day ) = @_;
my( $c, $y );

if ( $month > 2 ) {
   $month -= 3;
}
else {
   $month += 9;
   $year--;
}

$day += int( ( 153 * $month + 2 ) / 5 );
$c    = int( ( 146097 * int( $year / 100 ) ) / 4 );
$y    = int( ( 1461 * ( $year % 100 ) ) / 4 );

return( $c + $y + $day + 1721119 );

} # end sub j_date()


HTH,
HG

-- 
Hans-Georg Rist
hans-georg@rist.net


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu