parsing / formatting historical dates

Discussion in 'Perl Misc' started by friendly@yorku.ca, Apr 9, 2007.

  1. Guest

    I'm reading/re-writing data files with historical dates that go back
    potentially to antiquity. e.g., one such has name, lifespan, birth
    date/place,
    death date/place as \t separated fields.

    Galilei, Galileo 1564-1642 b: 15 Feb 1564, Pisa d: 8 Jan 1642,
    Arcetri, Italy
    Lalanne, Leon 1811-1892 b: Paris 3 Jul 1811 d:13 Mar 1892

    I need to parse the dates and reformat them, e.g. in ISO 8601 --
    '15 Feb 1564' => '1564-02-15'
    I tried using Date:parse and Date:Format,

    my ($name, $lived, $born, $died) = split(/\s?[\t]/);
    if ($born) {
    $born =~ s/^\s*b:\s+//;
    ($birthdate, $birthplace) = split(/,\s*/, $born, 2);
    my $date = str2time($birthdate);
    $birthdate = time2str("%Y-%m-%d", $date);
    }

    but, as I see now, this converts to seconds since the epoch, 1-
    jan-1970,
    so it can't work.

    Are there other modules that deal with historical dates? Assume that
    I don't need time values.

    thanks
    , Apr 9, 2007
    #1
    1. Advertising

  2. Thrill5 Guest

    <> wrote in message
    news:...
    > I'm reading/re-writing data files with historical dates that go back
    > potentially to antiquity. e.g., one such has name, lifespan, birth
    > date/place,
    > death date/place as \t separated fields.
    >
    > Galilei, Galileo 1564-1642 b: 15 Feb 1564, Pisa d: 8 Jan 1642,
    > Arcetri, Italy
    > Lalanne, Leon 1811-1892 b: Paris 3 Jul 1811 d:13 Mar 1892
    >
    > I need to parse the dates and reformat them, e.g. in ISO 8601 --
    > '15 Feb 1564' => '1564-02-15'
    > I tried using Date:parse and Date:Format,
    >
    > my ($name, $lived, $born, $died) = split(/\s?[\t]/);
    > if ($born) {
    > $born =~ s/^\s*b:\s+//;
    > ($birthdate, $birthplace) = split(/,\s*/, $born, 2);
    > my $date = str2time($birthdate);
    > $birthdate = time2str("%Y-%m-%d", $date);
    > }
    >
    > but, as I see now, this converts to seconds since the epoch, 1-
    > jan-1970,
    > so it can't work.
    >
    > Are there other modules that deal with historical dates? Assume that
    > I don't need time values.
    >
    > thanks
    >

    Not sure why you need a module to do this. Have you over simplified the
    problem?

    my %months = (Jan => 1, Feb => 2, Mar => 3, Apr => 4,
    May => 5, Jun => 6, Jul => 7, Aug => 8,
    Sep => 9, Oct => 10, Nov => 11, Dec => 12);

    sub convdate {
    my ($day, $mon, $year) = split(/\s+/,shift);
    return sprintf("%04d-%02d-%02d",$year,$months{$mon},$day);
    }

    my ($name, $lived, $born, $died) = split(/\s?[\t]/);
    if ($born) {
    $born =~ s/^\s*b:\s+//;
    ($birthdate, $birthplace) = split(/,\s*/, $born, 2);
    $birthdate = convdate($birthdate);
    }

    Scott
    Thrill5, Apr 9, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 8, 4:47 pm, "" <> wrote:
    > I'm reading/re-writing data files with historical dates that go back
    > potentially to antiquity.


    The Date::Manip module does not rely on epoch seconds to store its
    objects. It will work for your purposes.
    , Apr 9, 2007
    #3
  4. wrote:
    > I need to parse the dates and reformat them, e.g. in ISO 8601 --
    > '15 Feb 1564' => '1564-02-15'
    > I tried using Date:parse and Date:Format,


    <snip>

    > my $date = str2time($birthdate);
    > $birthdate = time2str("%Y-%m-%d", $date);


    Try this instead:

    my ($d, $m, $y) = (strptime $birthdate)[3..5];
    $birthdate = sprintf '%d-%02d-%02d', $y<1000 ? 1900+$y : $y, $m+1, $d;

    > but, as I see now, this converts to seconds since the epoch,
    > 1-jan-1970, so it can't work.


    The Date::parse::strptime() function does not have that limitation, at
    least not on my Windows box.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 9, 2007
    #4
    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. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    830
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    167
    Mark Schupp
    Aug 9, 2004
  3. Michael Schuerig
    Replies:
    0
    Views:
    100
    Michael Schuerig
    May 1, 2005
  4. Replies:
    1
    Views:
    192
    Jano Svitok
    Jul 17, 2007
  5. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    168
    Eric I.
    Jun 8, 2008
Loading...

Share This Page