Commit 7779bb0b authored by Julian Trischler's avatar Julian Trischler

Initial commit.

parents
README
======
Description
-----------
„Wie schbäd?“ is the Swabian phrase for „What time is it?“. So as you may
have figured out this incredible program displays given times in Swabian to
you.
Installation and Compilation
----------------------------
To install this awesome program, you first have to compile it for your
desired system. To do so, fire up a terminal and go into the directory where
the source code is located. Then type
gnatmake wieschbaed.adb
Of course you need an Ada compiler to do so. I recommend GNAT to you. It's
a free and really great one. Use Google or your favored search engine to
find a download link for it.
Once you've compiled it, you can run it.
Productive Usage
----------------
In productive use, you better use an alias to run this program. Insert the
following line into your ~/.bashrc or ~/.zshrc or ~/.tcshrc or whatever
shell you are using. Yeah, you get it!
alias wieschbaed=/path/to/wie_schbaed `date +%H:%M:%S`
This will let you get the current time with just typing 'wieschbaed'. It's
awesome, yeah!
Further Information
===================
For more information just run the software without any parameters or view
the source code.
Just one thing here: The author of this program is Julian 'Lyse' Trischler,
he has published it under Do-The-Fuck-You-Want-To-Public-License. Feel free
to edit and improve it.
----------------------------------------------------------------------------
-- Wieschbaed - Wie schbäd? - Wie spät ist es? - What time is it? --
-- This program displays the given time in Swabian. --
-- --
-- This software was written by Julian 'Lyse' Trischler on June 18th 2010 --
-- and is published under Do-The-Fuck-You-Want-To-Public-License. --
-- Version 0.8 (2010-06-18) --
----------------------------------------------------------------------------
with Ada.Text_IO;
with Ada.Command_Line;
with Ada.Exceptions;
procedure Wie_Schbaed is
------------------------------------------------------------------------
-- @Type: Hour --
-- @Purpose: This datatype represents hours in time objects. The 24 --
-- hour clock is used here. --
-- @See: Minute --
-- @See: Second --
-- @See: Time --
------------------------------------------------------------------------
type Hour is new Integer range 0..23;
------------------------------------------------------------------------
-- @Type: Minute --
-- @Purpose: This datatype represents minutes in time objects. --
-- @See: Hour --
-- @See: Second --
-- @See: Time --
------------------------------------------------------------------------
type Minute is new Integer range 0..59;
------------------------------------------------------------------------
-- @Type: Second --
-- @Purpose: This datatype represents seconds in time objects. Please --
-- note, leap-seconds (60) are not supported, only the --
-- classic range from 0 to 59. --
-- @See: Hour --
-- @See: Minute --
-- @See: Time --
------------------------------------------------------------------------
type Second is new Integer range 0..59;
------------------------------------------------------------------------
-- @Type: Time --
-- @Purpose: This datatype represents a complete time with hours, --
-- minutes and seconds. --
-- @See: Hour --
-- @See: Minute --
-- @See: Second --
------------------------------------------------------------------------
type Time is record
H: Hour;
M: Minute;
S: Second;
end record;
------------------------------------------------------------------------
-- @Function: Inc --
-- @Description: Increments the given hour by one and finally returns --
-- the result. This function takes care of overflowing, --
-- so after the 23th hour, 0 is the next. --
-- @Parameters: --
-- +H The hour to increment save --
-- @Return: The incremented hour --
-- @See: Hour --
------------------------------------------------------------------------
function Inc(H: Hour) return Hour is
begin
if H = Hour'Last then
return Hour'First;
else
return H + 1;
end if;
end Inc;
------------------------------------------------------------------------
-- @Function: Str --
-- @Description: Generates a string representation of the given time --
-- in format "HH:MM:SS", where "HH" is the hours, "MM" --
-- the minutes and "SS" the seconds. Each three parts --
-- may have a leading zero if necessary. --
-- @Parameters: --
-- +T The time to string represent --
-- @Return: String representation of the time --
-- @See: Time --
------------------------------------------------------------------------
function Str(T: Time) return String is
function Format(I: Integer) return String is
Image: String := I'Img;
Str: String := Image(Image'First + 1 .. Image'Last);
begin
if Str'Length = 2 then
return Str;
else
return "0" & Str;
end if;
end;
begin
return Format(Integer(T.H)) & ":"
& Format(Integer(T.M)) & ":"
& Format(Integer(T.S));
end Str;
function Str(H: Hour) return String is
begin
case H is
when 0 | 12 => return "zwölfe";
when 1 | 13 => return "einse";
when 2 | 14 => return "zweie";
when 3 | 15 => return "dreie";
when 4 | 16 => return "viere";
when 5 | 17 => return "fünfe";
when 6 | 18 => return "sechse";
when 7 | 19 => return "siebene";
when 8 | 20 => return "achte";
when 9 | 21 => return "neune";
when 10 | 22 => return "zehne";
when 11 | 23 => return "elfe";
end case;
end Str;
function Special_Str(H: Hour) return String is
begin
case H is
when 0 => return "Mitternacht";
when 12 => return "Mittag";
when others => return Str(H);
end case;
end Special_Str;
function Time_To_String(T: Time) return String is
begin
case T.M is
when 0 .. 1 => return Special_Str(T.H);
when 2 .. 5 => return "kurz nach " & Special_Str(T.H);
when 6 .. 9 => return "bald viertel " & Str(Inc(T.H));
when 10 .. 13 => return "kurz vor viertel " & Str(Inc(T.H));
when 14 .. 16 => return "viertel " & Str(Inc(T.H));
when 17 .. 22 => return "kurz nach viertel " & Str(Inc(T.H));
when 23 .. 26 => return "bald halber " & Str(Inc(T.H));
when 27 .. 28 => return "kurz vor halber " & Str(Inc(T.H));
when 29 .. 31 => return "halber " & Str(Inc(T.H));
when 32 .. 35 => return "kurz nach halber " & Str(Inc(T.H));
when 36 .. 39 => return "bald dreiviertel " & Str(Inc(T.H));
when 40 .. 43 => return "kurz vor dreiviertel " & Str(Inc(T.H));
when 44 .. 46 => return "dreiviertel " & Str(Inc(T.H));
when 47 .. 50 => return "kurz nach dreiviertel " & Str(Inc(T.H));
when 51 .. 54 => return "bald " & Special_Str(Inc(T.H));
when 55 .. 58 => return "kurz vor " & Special_Str(Inc(T.H));
when 59 => return Special_Str(Inc(T.H));
end case;
end;
procedure Test(T: Time) is
begin
Ada.Text_IO.Put_Line(Str(T) & " => " & Time_To_String(T));
end Test;
Input_Error: exception;
-- Data'First must equal Pattern'First
function Time_From_String(Data: String; Pattern: String) return Time is
T: Time := Time'(H => 0, M => 0, S => 0);
begin
if Data'Length /= Pattern'Length then
raise Input_Error with "Data doesn't match pattern length";
else
for I in Pattern'Range loop
case Pattern(I) is
--------------------------------------------------------
-- Check for correct hour data --
--------------------------------------------------------
when 'H' =>
if Data(I) in '0' .. '9' then
begin
T.H := 10 * T.H + Hour'Value(Data(I..I));
exception
when Constraint_Error =>
raise Input_Error with "Illegal hour data at position" & I'Img;
end;
else
raise Input_Error with "Illegal hour data at position" & I'Img;
end if;
--------------------------------------------------------
-- Check for correct minute data --
--------------------------------------------------------
when 'M' =>
if Data(I) in '0' .. '9' then
begin
T.M := 10 * T.M + Minute'Value(Data(I..I));
exception
when Constraint_Error =>
raise Input_Error with "Illegal minute data at position" & I'Img;
end;
else
raise Input_Error with "Illegal minute data at position" & I'Img;
end if;
--------------------------------------------------------
-- Check for correct minute data --
--------------------------------------------------------
when 'S' =>
if Data(I) in '0' .. '9' then
begin
T.S := 10 * T.S + Second'Value(Data(I..I));
exception
when Constraint_Error =>
raise Input_Error with "Illegal second data at position" & I'Img;
end;
else
raise Input_Error with "Illegal second data at position" & I'Img;
end if;
--------------------------------------------------------
-- Check for correct time separator data --
--------------------------------------------------------
when ':' =>
if Data(I) /= ':' then
raise Input_Error with "Illegal time separator data at position" & I'Img;
end if;
when others =>
raise Constraint_Error with "Illegal pattern character '" & Pattern(I) & "' at position" & I'Img & " in """ & Pattern & """";
end case;
end loop;
end if;
return T;
end Time_From_String;
T: Time;
begin
-- try to read the command line argument, it should be in one
-- of the following formats:
-- * HH:MM:SS
-- * HH:MM
-- * HHMMSS
-- * HHMM
-- * default UNIX date output (TODO to be implemented some time later)
-- * use current time if no input is given (TODO to be implemented some time later)
if Ada.Command_Line.Argument_Count = 1 then
declare
Input: String := Ada.Command_Line.Argument(1);
begin
-- input length must be correct; as mentioned above, the formats
-- used at the moment are 4, 5, 6 or 8 characters long
case Input'Length is
------------------------------------------------------------
-- HHMM format is four characters long --
------------------------------------------------------------
when 4 =>
T := Time_From_String(Data => Input, Pattern => "HHMM");
------------------------------------------------------------
-- HH:MM format is five characters long --
------------------------------------------------------------
when 5 =>
T := Time_From_String(Data => Input, Pattern => "HH:MM");
------------------------------------------------------------
-- HHMMSS format is six characters long --
------------------------------------------------------------
when 6 =>
T := Time_From_String(Data => Input, Pattern => "HHMMSS");
------------------------------------------------------------
-- HH:MM:SS format is eight characters long --
------------------------------------------------------------
when 8 =>
T := Time_From_String(Data => Input, Pattern => "HH:MM:SS");
------------------------------------------------------------
-- this time data length is not supported, thus illegal --
------------------------------------------------------------
when others =>
raise Input_Error with "Illegal time data length";
end case;
end;
--Test(T);
Ada.Text_IO.Put_Line(Time_To_String(T));
else
Ada.Text_IO.Put_Line("wieschbaed - „Wie schbäd?“ - „Wie spät ist es?“ - „What time is it?“");
Ada.Text_IO.Put_Line("This program displays the given time in Swabian.");
Ada.Text_IO.Put_Line("Usage: " & Ada.Command_Line.Command_Name & " <TIME>");
Ada.Text_IO.Put_Line(" <TIME> in one of the following formats:");
Ada.Text_IO.Put_Line(" HH:MM:SS - HH:MM - HHMMSS - HHMM");
end if;
exception
when E: Input_Error =>
Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
Ada.Exceptions.Exception_Message(E));
Ada.Command_Line.Set_Exit_Status(1);
when E: others =>
Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
"Uncatched " & Ada.Exceptions.Exception_Name(E)
& ": " & Ada.Exceptions.Exception_Message(E));
end Wie_Schbaed;
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment