Date format: Difference between revisions

From Rosetta Code
Content added Content deleted
(added perl)
Line 66: Line 66:
: .short-date
: .short-date
time&date 1 .r .- 1 .r .- 1 .r drop drop drop ;
time&date 1 .r .- 1 .r .- 1 .r drop drop drop ;

=={{header|Perl}}==

use POSIX;
print POSIX::strftime('%Y-%m-%d', localtime), "\n";
print POSIX::strftime('%A, %B %d, %Y', localtime), "\n";


=={{header|Python}}==
=={{header|Python}}==

Revision as of 12:26, 3 February 2008

Task
Date format
You are encouraged to solve this task according to the task description, using any language you may know.

Display a given date in the formats of "2007-11-10" and "Sunday, November 10, 2007".

Ada

This task has been clarified. Its programming examples are in need of review to ensure that they still fit the requirements of the task.


with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Arithmetic; use Ada.Calendar.Arithmetic;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_Io; use Ada.Text_IO;

procedure Date_Format is
   type Month_Names is array(Month_Number) of Unbounded_String;
   Months : constant Month_Names := (
      To_Unbounded_String("January"),
      To_Unbounded_String("February"),
      To_Unbounded_String("March"),
      To_Unbounded_String("April"),
      To_Unbounded_String("May"),
      To_Unbounded_String("June"),
      To_Unbounded_String("July"),
      To_Unbounded_String("August"),
      To_Unbounded_String("September"),
      To_Unbounded_String("October"),
      To_Unbounded_String("November"),
      To_Unbounded_String("December"));
   The_Day : Time;
   Increment : constant Day_Count := 1;
begin
   The_Day := Clock;
   Put_Line("Today is: " & To_String(Months(Month(The_Day))) &
      Day_Number'Image(Day(The_Day)) & "," &
      Year_Number'Image(Year(The_Day)));
   The_Day := The_Day + Increment;
   Put_Line("Tomorrow is: " & To_String(Months(Month(The_Day))) &
      Day_Number'Image(Day(The_Day)) & "," &
      Year_Number'Image(Year(The_Day)));
end Date_Format;


Forth

: str-table
  create ( n -- ) 0 do , loop
  does>  ( n -- str len ) swap cells + @ count ;
 here ," December"
 here ," November"
 here ," October"
 here ," September"
 here ," August"
 here ," July"
 here ," June"
 here ," May"
 here ," April"
 here ," March"
 here ," February"
 here ," January"
12 str-table months

: .long-date
  time&date ( s m h D M Y )
  >R 1- months type space 1 .r [char] , emit space R> .
  drop drop drop ;
: .-  [char] - emit ;
: .short-date
  time&date  1 .r .- 1 .r .- 1 .r  drop drop drop ;

Perl

use POSIX;
print POSIX::strftime('%Y-%m-%d', localtime), "\n";
print POSIX::strftime('%A, %B %d, %Y', localtime), "\n";

Python

Formatting rules: http://docs.python.org/lib/module-time.html (strftime)

import datetime
today = datetime.date.today()
# This one is built in:
print today.isoformat()
# Or use a format string for full flexibility:
print today.strftime('%A, %B %d, %Y')

Raven

time int as today

Short form:

today '%Y-%m-%d' date

Long form:

today '%A, %B %d, %Y' date