upstream version 1.2.2
[debian/freetts] / demo / util / TimeUtils.java
1 /**
2  * Copyright 2001-2003 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 import java.util.regex.Pattern;
9
10 public class TimeUtils {
11
12     /**
13      * Returns a phrase that conveys the exactness
14      * of the time.
15      *
16      * @param hour the hour of the time
17      * @param min the minute of the time
18      *
19      * @return a string phrase
20      */
21     private static String timeApprox(int hour, int min) {
22         int mm;
23
24         mm = min % 5;
25
26         if (mm == 0) {
27             return "exactly";
28         } else if (mm == 1) {
29             return "just after";
30         } else if (mm == 2) {
31             return "a little after";
32         } else {
33             return "almost";
34         }
35     }
36
37     /**
38      * Returns a phrase that conveys the minutes in relation
39      * to the hour.
40      *
41      * @param hour the hour of the time
42      * @param min the minute of the time
43      *
44      * @return a string phrase.
45      */
46     private static String timeMin(int hour, int min) {
47         int mm;
48
49         mm = min / 5;
50         if ((min % 5) > 2) {
51             mm += 1;
52         }
53         mm = mm * 5;
54         if (mm > 55) {
55             mm = 0;
56         }
57
58         if (mm == 0) {
59             return "";
60         } else if (mm == 5) {
61             return "five past";
62         } else if (mm == 10) {
63             return "ten past";
64         } else if (mm == 15) {
65             return "quarter past";
66         } else if (mm == 20) {
67             return "twenty past";
68         } else if (mm == 25) {
69             return "twenty-five past";
70         } else if (mm == 30) {
71             return "half past";
72         } else if (mm == 35) {
73             return "twenty-five to";
74         } else if (mm == 40) {
75             return "twenty to";
76         } else if (mm == 45) {
77             return "quarter to";
78         } else if (mm == 50) {
79             return "ten to";
80         } else if (mm == 55) {
81             return "five to";
82         } else {
83             return "five to";
84         }
85     }
86
87     /**
88      * Returns a phrase that conveys the hour in relation
89      * to the hour.
90      *
91      * @param hour the hour of the time
92      * @param min the minute of the time
93      *
94      * @return a string phrase.
95      */
96     private static String timeHour(int hour, int min) {
97         int hh;
98
99         hh = hour;
100         if (min > 32)  { // PBL: fixed from flite_time
101             hh += 1;
102         }
103         if (hh == 24) {
104             hh = 0;
105         }
106         if (hh > 12) {
107             hh -= 12;
108         }
109
110         if (hh == 0) {
111             return "midnight";
112         } else if (hh == 1) {
113             return "one";
114         } else if (hh == 2) {
115             return "two";
116         } else if (hh == 3) {
117             return "three";
118         } else if (hh == 4) {
119             return "four";
120         } else if (hh == 5) {
121             return "five";
122         } else if (hh == 6) {
123             return "six";
124         } else if (hh == 7) {
125             return "seven";
126         } else if (hh == 8) {
127             return "eight";
128         } else if (hh == 9) {
129             return "nine";
130         } else if (hh == 10) {
131             return "ten";
132         } else if (hh == 11) {
133             return "eleven";
134         } else if (hh == 12) {
135             return "twelve";
136         } else {
137             return "twelve";
138         }
139     }
140
141     /**
142      * Returns a phrase that conveys the time of day.
143      *
144      * @param hour the hour of the time
145      * @param min the minute of the time
146      *
147      * @return a string phrase
148      */
149     private static String timeOfDay(int hour, int min) {
150         int hh = hour;
151
152         if (min > 58)
153             hh++;
154
155         if (hh == 24) {
156             return "";
157         } else if (hh > 17) {
158             return "in the evening";
159         } else if (hh > 11) {
160             return "in the afternoon";
161         } else if ((hh == 0) && (min < 33)) {
162             return "";
163         } else {
164             return "in the morning";
165         }
166     }
167
168     /**
169      * Returns a string that corresponds to the given time.
170      *
171      * @param time the time in the form HH:MM
172      *
173      * @return the time in string, null if the given time is not in the
174      *   form HH:MM 
175      */
176     public static String timeToString(String time) {
177         String theTime = null;
178         if (Pattern.matches("[012][0-9]:[0-5][0-9]", time)) {
179             int hour = Integer.parseInt(time.substring(0, 2));
180             int min = Integer.parseInt(time.substring(3));
181
182             theTime = timeToString(hour, min);
183         }
184         return theTime;
185     }
186
187     /**
188      * Returns a string that corresponds to the given time.
189      *
190      * @param hour the hour
191      * @param min the minutes
192      *
193      * @return the time in string, null if the given time out of range
194      */
195     public static String timeToString(int hour, int min) {
196         String theTime = "The time is now, " +
197             timeApprox(hour, min) + " " +
198             timeMin(hour, min) + " " +
199             timeHour(hour, min) + ", " +
200             timeOfDay(hour, min) + "." ;
201         return theTime;
202     }
203 }