Loading ...
Sorry, an error occurred while loading the content.

ISO 8601 Function in Perl?

Expand Messages
  • Sunatori, Go Simon
    My ISO 8601-based Calendar Generator is as popular as ever, with a few kind souls making donations once in a while. *
    Message 1 of 4 , Aug 4, 2003
    • 0 Attachment
      My ISO 8601-based Calendar Generator is as popular as ever, with a few
      kind souls making donations once in a while.

      * http://WWW.HyperInfo.CA/~HyperInfo.CA/Calendar.html

      However, I am forced to translate the CGI, which was originally coded
      in HyperTalk, then in AppleScript, into Perl to be run on a UNIX
      server.

      I am a newbie in Perl, and I am wondering if there are any pre-written
      date/time functions (e.g., localtime(), timelocal(), etc.) which
      conform to ISO 8601. If not, I will just have to translate the
      AppleScript isoDateTime() function shown below into Perl...

      Thank you very much for your attention.
      Merci de votre attention.

      Prof. Simon Sunatori, P.Eng./ing., M.Eng. (Engineering Physics), F.N.A.
      HyperInfo Canada Inc.
      --
      Simon Sunatori <http://WWW.HyperInfo.CA/~GS.Sunatori@.../>
      65, des Parulines <mailto:GS.Sunatori@...>
      Gatineau (Quebec) <telephone:+1-819-595-9210>
      CANADA J9A 1Z4 <facsimile:+1-425-984-7292>

      ------------------------------------------------------------------------
      on isoDateTime(theDateTime, theOption)
      try
      set theReturn to ""
      set oldDelimiter to AppleScript's text item delimiters
      set AppleScript's text item delimiters to {" "}
      set theDateTime to (theDateTime as string)
      set theMonths to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
      set theYYYY to (text item 4 of theDateTime)
      repeat with i from 1 to (count of theMonths)
      if ((text item 2 of theDateTime) = (item i of theMonths)) then
      set theMM to i as string
      exit repeat
      end if
      end repeat
      if ((count of theMM) < 2) then set theMM to ("0" & theMM)
      set theDD to text 1 thru -2 of (text item 3 of theDateTime)
      if ((count of theDD) < 2) then set theDD to ("0" & theDD)
      set theTime to (text item 5 of theDateTime)
      set theDay to text 1 thru -2 of (text item 1 of theDateTime)
      set AppleScript's text item delimiters to oldDelimiter
      if (theOption = "date") then
      set theReturn to (theYYYY & "-" & theMM & "-" & theDD)
      else if (theOption = "time") then
      set theReturn to (theYYYY & "-" & theMM & "-" & theDD & "T" & theTime & ",0")
      else
      set theReturn to (theYYYY & "-" & theMM & "-" & theDD & " (" & theDay & ")")
      end if
      return theReturn
      on error
      return -1
      end try
      end isoDateTime
      ------------------------------------------------------------------------
    • Morris, Mike
      See http://datetime.perl.org/modules.html. If there s such a thing, I d expect it to be there. There is a DateTime::Format::ISO8601 Modules that Parses
      Message 2 of 4 , Aug 4, 2003
      • 0 Attachment
        See http://datetime.perl.org/modules.html.

        If there's such a thing, I'd expect it to be there.

        There is a "DateTime::Format::ISO8601" Modules that "Parses ISO8601
        formats", but not sure about the other way..


        > -----Original Message-----
        > From: Sunatori, Go Simon [mailto:GS.Sunatori@...]
        > Sent: Monday, August 04, 2003 8:09 PM
        > To: ISO8601@yahoogroups.com
        > Subject: [ISO8601] ISO 8601 Function in Perl?
        >
        >
        > My ISO 8601-based Calendar Generator is as popular as ever,
        > with a few
        > kind souls making donations once in a while.
        >
        > * http://WWW.HyperInfo.CA/~HyperInfo.CA/Calendar.html
        >
        > However, I am forced to translate the CGI, which was originally coded
        > in HyperTalk, then in AppleScript, into Perl to be run on a UNIX
        > server.
        >
        > I am a newbie in Perl, and I am wondering if there are any
        > pre-written
        > date/time functions (e.g., localtime(), timelocal(), etc.) which
        > conform to ISO 8601. If not, I will just have to translate the
        > AppleScript isoDateTime() function shown below into Perl...
        >
        > Thank you very much for your attention.
        > Merci de votre attention.
        >
        > Prof. Simon Sunatori, P.Eng./ing., M.Eng. (Engineering
        > Physics), F.N.A.
        > HyperInfo Canada Inc.
        > --
        > Simon Sunatori
        > <http://WWW.HyperInfo.CA/~GS.Sunatori@.../>
        > 65, des Parulines <mailto:GS.Sunatori@...>
        > Gatineau (Quebec) <telephone:+1-819-595-9210>
        > CANADA J9A 1Z4 <facsimile:+1-425-984-7292>
        >
        > --------------------------------------------------------------
        > ----------
        > on isoDateTime(theDateTime, theOption)
        > try
        > set theReturn to ""
        > set oldDelimiter to AppleScript's text item delimiters
        > set AppleScript's text item delimiters to {" "}
        > set theDateTime to (theDateTime as string)
        > set theMonths to {"January", "February",
        > "March", "April", "May", "June", "July", "August",
        > "September", "October", "November", "December"}
        > set theYYYY to (text item 4 of theDateTime)
        > repeat with i from 1 to (count of theMonths)
        > if ((text item 2 of theDateTime) =
        > (item i of theMonths)) then
        > set theMM to i as string
        > exit repeat
        > end if
        > end repeat
        > if ((count of theMM) < 2) then set theMM to
        > ("0" & theMM)
        > set theDD to text 1 thru -2 of (text item 3 of
        > theDateTime)
        > if ((count of theDD) < 2) then set theDD to
        > ("0" & theDD)
        > set theTime to (text item 5 of theDateTime)
        > set theDay to text 1 thru -2 of (text item 1 of
        > theDateTime)
        > set AppleScript's text item delimiters to oldDelimiter
        > if (theOption = "date") then
        > set theReturn to (theYYYY & "-" & theMM
        > & "-" & theDD)
        > else if (theOption = "time") then
        > set theReturn to (theYYYY & "-" & theMM
        > & "-" & theDD & "T" & theTime & ",0")
        > else
        > set theReturn to (theYYYY & "-" & theMM
        > & "-" & theDD & " (" & theDay & ")")
        > end if
        > return theReturn
        > on error
        > return -1
        > end try
        > end isoDateTime
        > --------------------------------------------------------------
        > ----------
        >
        > ------------------------ Yahoo! Groups Sponsor
        > ---------------------~-->
        > Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
        > Printer at Myinks.com. Free s/h on orders $50 or more to the
        > US & Canada. http://www.c1tracking.com/l.asp?cid=5511
        > http://us.click.yahoo.com/sO0ANB/LIdGAA/ySSFAA/1U_rlB/TM
        > --------------------------------------------------------------
        > -------~->
        >
        >
        >
        > Your use of Yahoo! Groups is subject to
        > http://docs.yahoo.com/info/terms/
        >
        >
      • Aron Roberts
        In the message ISO 8601 Function in Perl? , dated 2003-08-04, Prof. ... Jukka Yukka Korpela s terrific Web page on ISO 8601 provides sample code in various
        Message 3 of 4 , Aug 5, 2003
        • 0 Attachment
          In the message "ISO 8601 Function in Perl?", dated 2003-08-04, Prof.
          Simon Sunatori wrote:

          >I am wondering if there are any pre-written [Perl] date/time
          >functions (e.g., localtime(), timelocal(), etc.) which conform to
          >ISO 8601.

          Jukka "Yukka" Korpela's terrific Web page on ISO 8601 provides
          sample code in various languages, including Perl:

          "Info on ISO 8601, the date and time representation standard"
          http://www.cs.tut.fi/~jkorpela/iso8601.html

          Here's Jukka's example code in Perl "for getting the current date
          and time and writing it in UTC":

          >($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
          > gmtime(time);
          >$t = sprintf "%4d-%02d-%02dT%02d:%02dZ\n",
          > 1900+$year,$mon+1,$mday,$hour,$min;
          >print $t;

          If you wish to work with the local time, rather than UTC, you can likely:

          - Substitute localtime() for gmtime().

          - Remove the trailing "Z" from the end of the string
          following "sprintf".

          To generate an unambiguous representation of the local time, you
          would need to also add an ISO 8601-confirming representation of the
          local offset from UTC to the end of the time string.

          If you wish to learn more about the built-in functions and various
          add-on modules (libraries) available for working with dates and times
          in Perl -- some of the latter quite extensive in their functionality
          -- the following article is a great starting place:

          Dave Rolsky
          "The Many Dates and Times of Perl"
          O'Reilly Perl.com, March 13, 2003
          http://www.perl.com/pub/a/2003/03/13/datetime.html

          Near the end of this article, Dave also mentions his own suite of
          date and time modules, which are ultimately intended to be
          comprehensive. You can view archives of the discussions about these
          libraries at:

          http://archive.develooper.com/datetime@.../

          (The hostname "develooper" above is spelled correctly :-)

          Aron Roberts Workstation Software Support Group . 221 Evans Hall
          University of California, Berkeley, CA 94720-3808 USA
          aron@... . +1 510-642-5974 . fax 510-643-5385
        • Sunatori, Go Simon
          ... In order to generate a Monthly Calendar, I need to extract the day of the week. I looked around and found function time2iso() in the help documentation.
          Message 4 of 4 , Aug 8, 2003
          • 0 Attachment
            Mike Morris wrote:
            >
            > See http://datetime.perl.org/modules.html.
            >
            > If there's such a thing, I'd expect it to be there.
            >
            > There is a "DateTime::Format::ISO8601" Modules that "Parses ISO8601
            > formats", but not sure about the other way..

            Aron Roberts wrote:
            >
            > Here's Jukka's example code in Perl "for getting the current date
            > and time and writing it in UTC":

            In order to generate a Monthly Calendar, I need to extract the day of
            the week. I looked around and found function time2iso() in the help
            documentation. However, it does not work in MacPerl 5.6.

            use HTTP::Date;
            print time2iso(time),"\n"; # outputs:
            # Undefined subroutine &main::time2iso called.

            There is no module "DateTime::Format::ISO8601" in MacPerl 5.6, either.
            Therefore, I picked function timelocal() and used Jukka's example
            code. With this, I do not need function POSIX::strftime().

            use Time::Local;
            my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
            localtime(timelocal(0,0,0,1,($MM-1),($YYYY)));
            print sprintf("%4d-%02d-%02dT%02d:%02d:%02d,0", ($year + 1900),
            ($mon + 1), $mday, $hour, $min, $sec);

            It has come to my attention that element "$wday" from PERL function
            localtime() supports only years from 1954 to 2039, while AppleScript
            function "date" supports the full Gregorian Calendar from 1582 to 3999.

            * AppleScript | http://WWW.HyperInfo.CA/~HyperInfo.CA/Calendar.html
            * PERL | http://WWW.HyperInfo.CA/~HyperInfo.CA/Calendarr.html

            The problem does not appear to be platform-specific or
            version-specific. I hope that "The Perl DateTime Project" will
            address the localtime() issue...

            Thank you very much for your attention.
            Merci de votre attention.

            Prof. Simon Sunatori, P.Eng./ing., M.Eng. (Engineering Physics), F.N.A.
            President & Chief Executive Officer
            HyperInfo Canada Inc.
            --
            Simon Sunatori <http://WWW.HyperInfo.CA/~GS.Sunatori@.../>
            65, des Parulines <mailto:GS.Sunatori@...>
            Gatineau (Quebec) <telephone:+1-819-595-9210>
            CANADA J9A 1Z4 <facsimile:+1-425-984-7292>
          Your message has been successfully submitted and would be delivered to recipients shortly.