File Coverage

blib/lib/Date/Spoken/German.pm
Criterion Covered Total %
statement 41 43 95.3
branch 26 32 81.2
condition 5 14 35.7
subroutine 7 7 100.0
pod 5 5 100.0
total 84 101 83.1


line stmt bran cond sub pod time code
1             # German.pm
2             #
3             # (c) 2003 Christian Winter
4             # All rights reserved. This program is free software; you can redistribute
5             # and/or modify it under the same terms as perl itself.
6            
7             =head1 NAME
8            
9             Date::Spoken::German - Output dates as Latin1 text as you would speak it
10            
11             =head1 SYNOPSIS
12            
13             use Date::Spoken::German;
14            
15             print timetospoken( time() );
16             print datetospoken( $DAY, $MONTH, $YEAR );
17            
18             =head1 DESCRIPTION
19            
20             This module provides you with functions to easily convert a date (given
21             as either integer values for day, month and year or as a unix timestamp)
22             to its representation as German text, like you would read it aloud.
23            
24             =head1 EXPORTABLE TAGS
25            
26             :ALL - all helper methods are also exported into the callers namespace
27            
28             =head1 FUNCTIONS
29            
30             =head2 Exported by default
31            
32             =over 2
33            
34             =item B
35            
36             In scalar context, return a string consisting of the text
37             representation of the date in the given unix timestamp,
38             like e.g. "dreizehnter Mai zweitausenddrei".
39            
40             In list context, returns the three words of the string as a list.
41            
42             =item B
43            
44             Takes the values for day of month, month and year as integers
45             (month starting with B<1>) and gives the same return values as
46             I.
47            
48             =back
49            
50             =head2 Exported by :ALL
51            
52             =over 2
53            
54             =item B
55            
56             Takes a year (absolute integer value) as input and returns the
57             text representation in German.
58            
59             =item B
60            
61             Takes a month (integer value, January = 1) as input and returns
62             the text representation in German.
63            
64             =item B
65            
66             Converts a day number to its German text representation.
67            
68             =back
69            
70             =head1 KNOWN ISSUES
71            
72             None at the moment.
73            
74             =head1 BUGS
75            
76             Please report all bugs to the author of this module:
77             Christian Winter
78            
79             =for html Mail a Bug
80            
81             =head1 CREDITS
82            
83             To Larry Wall for Perl itself, and to all developers out there
84             contributing to the Perl community. Special thanks to all regulars
85             in the usenet perl groups for giving me a lot of hints that helped
86             me understand what Perl can do.
87            
88             =head1 SEE ALSO
89            
90             Type B to get info on Perl itselft.
91             Type B to get info on Perl modules.
92            
93             =cut
94            
95             package Date::Spoken::German;
96            
97 1     1   31931 use encoding 'latin1';
  1         25010  
  1         7  
98 1     1   2481 use POSIX;
  1         10956  
  1         8  
99             require Exporter;
100            
101             @ISA = qw(Exporter);
102             @EXPORT = qw(datetospoken timetospoken);
103             %EXPORT_TAGS = ( ALL => [qw(yeartospoken datetospoken timetospoken monthtospoken daytospoken)] );
104            
105             our $VERSION = "0.05";
106             our $AUTHOR = 'Christian Winter ';
107            
108             my %cipher = ( 1 => "ein", 2 => "zwei", 3 => "drei", 4 => "vier", 5 => "fünf", 6 => "sechs", 7 => "sieben",
109             8 => "acht", 9 => "neun", 10 => "zehn", 11 => "elf", 12 => "zwölf", 16 => "sechzehn", 17 => "siebzehn", 18 => "achzehn" );
110             my %specialcipher = ( 1 => "ers", 3 => "drit", 7 => "sieb", 8 => "ach" );
111             my %tens = ( 1 => "zehn", 2 => "zwanzig", 3 => "dreissig", 6 => "sechzig", 7 => "siebzig", 8 => "achzig" );
112             my %month = ( 1 => "Januar", 2 => "Februar", 3 => "März", 4 => "April", 5 => "Mai", 6 => "Juni",
113             7 => "Juli", 8 => "August", 9 => "September", 10 => "Oktober", 11 => "November", 12 => "Dezember" );
114            
115             sub yeartospoken
116             {
117 5     5 1 7 my $year = shift;
118 5         26 (my $tens = $year) =~ s/^.*(\d\d)$/$1/;
119 5         6 my $hundreds = "";
120 5 50       10 if( $year < 10 ) {
121 0   0     0 $year = $cipher{$year} || "null";
122             } else {
123 5 100       18 if( $tens == 0 ) {
    100          
124 1         2 $tens = "";
125             } elsif( ($tens % 10) == 0 ) {
126 1         22 $tens =~ s/(.)(.)/$tens{$1}/;
127             } else {
128 3 100       7 if( $tens < 10 ) {
129 1         9 $tens =~ s/(.)(.)/$cipher{$2}/;
130             } else {
131 2 100       9 $tens =~ s/(.)(.)/$cipher{"$1$2"} || $cipher{$2}.(($1==1)?$tens{$1}:"und".$tens{$1})/e;
  2 50       55  
132             }
133             }
134 5 100       14 if( $year >= 100 ) {
135 4         15 ($hundreds = $year) =~ s/^(.?.)..$/$1/;
136 4 100 66     24 if( $hundreds >= 20 || $hundreds == 10 ) {
137 1 50       3 $hundreds =~ s/(.)(.)/$cipher{$1}."tausend".(($2>0)?$cipher{$2}."hundert":"")/ex;
  1         10  
138             } else {
139 3 100       9 if( $hundreds > 10 ) {
140 2   33     7 $hundreds =~ s/(.)(.)/($cipher{"$1$2"} || $cipher{$2}."zehn")."hundert"/e;
  2         31  
141             } else {
142 1         3 $hundreds = $cipher{$hundreds}."hundert";
143             }
144             }
145             }
146             }
147 5         42 return $hundreds.$tens;
148             }
149            
150             sub monthtospoken
151             {
152 5     5 1 5 my $monat = shift;
153 5         22 return $month{$monat};
154             }
155            
156             sub daytospoken
157             {
158 5     5 1 7 my $tag = shift;
159 5 100       12 $endung = ($tag > 19)?"ster":"ter";
160 5 100       10 if( $tag >= 10 ) {
161 3 50       13 $tag =~ s/(.)(.)/$cipher{"$1$2"} || (($2>0)?$cipher{$2}.(($1>1)?"und":""):"").$tens{$1}/ex;
  3 50       47  
    100          
162             } else {
163 2   33     9 $tag = $specialcipher{$tag} || $cipher{$tag};
164             }
165 5         20 return $tag.$endung;
166             }
167            
168             sub datetospoken
169             {
170 5     5 1 1410 my ($tag, $monat, $jahr) = @_;
171 5 50       12 if( wantarray ) {
172 0         0 return daytospoken($tag), monthtospoken($monat), yeartospoken($jahr);
173             } else {
174 5         11 return daytospoken($tag)." ".monthtospoken($monat)." ".yeartospoken($jahr);
175             }
176             }
177            
178             sub timetospoken
179             {
180 2   33 2 1 917 my( $tag, $monat, $jahr) = (gmtime( shift || time() ))[3,4,5];
181 2         9 return datetospoken($tag, $monat+1, 1900+$jahr);
182             }
183            
184             1;