Time.format

Hi all.

If there's one thing I miss about PHP, it must be date(). PHP's date()
function is similar in purpose to Time.strftime, only with a whole
slew of extra formatting options. For more info, see
http://php.net/date

However, ruby seems to have no equivalent function. So I made my own.
(Well, not entirely. I copied it from
http://simon.incutio.com/archive/2003/10/07/dateInPython)

Attached. It works like this:

Time.now.format "jS M Y" # => "18th Jul 2005"

I'd like to have this in the core, if people like it.

···

--
- nornagon

Eep. When I said "attached", I meant "attached to the next post."

timeformat.rb (3.48 KB)

···

On 7/18/05, nornagon <nornagon@gmail.com> wrote:

[snip]

--
- nornagon

why not just post the library at RAA or rubyforge?
-lv

nornagon wrote:

···

Hi all.

If there's one thing I miss about PHP, it must be date(). PHP's date()
function is similar in purpose to Time.strftime, only with a whole
slew of extra formatting options. For more info, see
http://php.net/date

However, ruby seems to have no equivalent function. So I made my own.
(Well, not entirely. I copied it from
http://simon.incutio.com/archive/2003/10/07/dateInPython\)

Attached. It works like this:

Time.now.format "jS M Y" # => "18th Jul 2005"

I'd like to have this in the core, if people like it.

nornagon wrote:

I'd like to have this in the core, if people like it.

I'd much rather see a date library that formats dates according to the user's expressed preferences. (i.e. using the settings from International on Windows, Date & Time on OS X, LC_DATE on Linux, ...)

mathew

Okay, it's three in the morning and I'm not quite on my toes. Here it
is again with a few more things implemented.

timeformat.rb (3.55 KB)

···

--
- nornagon

Hi,

At Mon, 18 Jul 2005 02:16:47 +0900,
nornagon wrote in [ruby-talk:148492]:

Okay, it's three in the morning and I'm not quite on my toes. Here it
is again with a few more things implemented.

$ ri Time#strftime
---------------------------------------------------------- Time#strftime
     time.strftime( string ) => string

···

------------------------------------------------------------------------
     Formats _time_ according to the directives in the given format
     string. Any text not listed as a directive will be passed through
     to the output string.

     Format meaning:

       %a - The abbreviated weekday name (``Sun'')
       %A - The full weekday name (``Sunday'')
       %b - The abbreviated month name (``Jan'')
       %B - The full month name (``January'')
       %c - The preferred local date and time representation
       %d - Day of the month (01..31)
       %H - Hour of the day, 24-hour clock (00..23)
       %I - Hour of the day, 12-hour clock (01..12)
       %j - Day of the year (001..366)
       %m - Month of the year (01..12)
       %M - Minute of the hour (00..59)
       %p - Meridian indicator (``AM'' or ``PM'')
       %S - Second of the minute (00..60)
       %U - Week number of the current year,
               starting with the first Sunday as the first
               day of the first week (00..53)
       %W - Week number of the current year,
               starting with the first Monday as the first
               day of the first week (00..53)
       %w - Day of the week (Sunday is 0, 0..6)
       %x - Preferred representation for the date alone, no time
       %X - Preferred representation for the time alone, no date
       %y - Year without a century (00..99)
       %Y - Year with century
       %Z - Time zone name
       %% - Literal ``%'' character
     
        t = Time.now
        t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
        t.strftime("at %I:%M%p") #=> "at 08:56AM"

--
Nobu Nakada

Yes, I know that Time#strftime exists. It simply doesn't have as many
options as Time#format does.

···

On 7/18/05, nobu.nokada@softhome.net <nobu.nokada@softhome.net> wrote:

Hi,

$ ri Time#strftime
[snip]

--
- nornagon

nornagon wrote:

Yes, I know that Time#strftime exists. It simply doesn't have as many
options as Time#format does.

I don't believe they both belong in the core (my opinion only).
But you are more than welcome to add it to the RAA. If you
document it and show why anyone should use it instead of strftime,
people will use it (especially PHP people).

Hal