Mdc::FormatTime


Mdc::FormatTime


Name

        FormatTime - Date and time formatting
        Version 1.04 - Oct 11, 2004


Synopsis

 use Mdc::FormatTime;           # use this module
 $T = new Mdc::FormatTime;      # create a new datetime object using default settings
 print $T->Now;                 # Print current date and time (2003/11/01 13:09:22)


Description

The FormatTime module provides a method for creating a date/time object which will produce a formatted time and date string.

A predefined date/time format or a customized date/time format may be chosen during object creation or via the object's ``Now'' method.

The HiRes module must be installed on your system if fractions of seconds will be used. If the HiRes module is not installed, comment out the ``use Time::HiRes'' directive at the beginning of this module and reset the variable $has_HiRes to zero. If a format is chosen which contains fractions of a second and the HiRes module is not installed, the fraction portion of the returned time string will contain only zeros.

Predefined formats:

 %default       Default format (2003/11/01 13:09:22)
 %ISO           International date format ISO 8601 (2003/02/01)
 %US            Standard US date format 2/1/03)
 %12hr          12 hour time format (1:09 PM)
 %24hr          24 hour time format (13:09:22)
 %GMT           24 hour time format (21:09:22 GMT)
 %longdate      Long date format (Saturday, November 1, 2003)
 %timestamp     Non-delimited string of 17 digits (20031101130922000)
                Resolution is one millisecond.  Offset is GMT
 %http          Date string used for http headers (Sat, 01 Nov 2003 21:09:22 GMT)

Custom formatting

 %Y             single digit year
 %YY            two digit year
 %YYYY          four digit year
 %M             month
 %MM            two digit month with leading zero
 %MMM           three character month (Jan)
 %MMMM          complete month (January)
 %D             date
 %DD            two digit date with leading zero
 %W             numeric day of the week (1=Sunday,7=Saturday)
 %WW            two character day of the week (Su)
 %WWW           three character day of the week (Sun)
 %WWWW          complete day of the week (Sunday)
 %h             hour
 %hh            two digit hour with leading zero
 %m             minute
 %mm            two digit minute with leading zero
 %s             seconds
 %ss            two digit seconds with leading zero
 %u             tenths of a second
 %uu            hundredths of a second
 %uuu           milliseconds
 %uuuu          tenths of a millisecond
 %uuuuu         hundredths of a millisecond
 %uuuuuu        microseconds

Directives to specify 12 hour mode

 %ap            12 hour time mode with lower case "a" or "p"
 %ampm          12 hour time mode with lower case "am" or "pm"
 %AP            12 hour time mode with upper case "A" or "P"
 %AMPM          12 hour time mode with upper case "AM" or "PM"

Offset directives:

 +5h            add five hours
 +5m            add five minutes
 +5s            add five seconds
 -5D            subtract five days

Other directives:

 GMT            Set Greenwich Mean Time, "GMT" returned in output string
 %gmt           Set Greenwich Mean Time, "gmt" not returned in output string
 %th            Set auto suffix mode for %D, eg., 1st, 2nd, 3rd, 4th.

Note: Do not confuse the directive ``GMT'' with the predefined format ``%GMT''. %gmt, %th and offset directives may be placed anywhere on the format string.


Methods

new

Create a new date/time object. Using a predefined format here will cause the ``Now'' method to return the data/time in that format.

 Example: Create a date/time object using the long date format
 my $T = new Mdc::FormatTime ( '%longdate' );
 Example: Create a date/time object using Greenwich Mean Time minus eight hours.
 my $T = new Mdc::FormatTime ( '%GMT-8h' );

Now

Return the date/time string. The date/time format that was chosen at object creation may be temporarily overridden by passing a predefined format or a customized format to this method.

 $time = $T->Now                        # use the object's default format
 $time = $T->Now('%24hr')               # override with 24 hour format

Define

Define named formats. If used in a subroutine context before creating any objects, will allow the new formats to be available to all subsequently defined objects.

 use Mdc::FormatTime;
 Mdc::FormatTime::Define( newname => '%MMM-%DD' );
 $T = new Mdc::FormatTime('%newname');
 my $date = $T->Now;

If used as an object method, the format definition will be available to that object only.

 use Mdc::FormatTime;
 $T = new Mdc::FormatTime;
 $T->Define( newname => '%MMM-%DD' );
 my $date = $T->Now('%newname');


Author and Copyright

 Mdc::FormatTime - Copyright (c) 2003, Mark K Mueller, All Rights Reserved.
 http://www.markmueller.com/

MkM 11:56 PM 2003-09-08