File: TimeUtils.java

package info (click to toggle)
freetts 1.2.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 65,244 kB
  • sloc: java: 21,305; xml: 1,340; sh: 969; lisp: 587; ansic: 241; makefile: 25; awk: 11
file content (203 lines) | stat: -rw-r--r-- 5,141 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * Copyright 2001-2003 Sun Microsystems, Inc.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 */
import java.util.regex.Pattern;

public class TimeUtils {

    /**
     * Returns a phrase that conveys the exactness
     * of the time.
     *
     * @param hour the hour of the time
     * @param min the minute of the time
     *
     * @return a string phrase
     */
    private static String timeApprox(int hour, int min) {
        int mm;

        mm = min % 5;

        if (mm == 0) {
            return "exactly";
        } else if (mm == 1) {
            return "just after";
        } else if (mm == 2) {
            return "a little after";
        } else {
            return "almost";
        }
    }

    /**
     * Returns a phrase that conveys the minutes in relation
     * to the hour.
     *
     * @param hour the hour of the time
     * @param min the minute of the time
     *
     * @return a string phrase.
     */
    private static String timeMin(int hour, int min) {
        int mm;

        mm = min / 5;
        if ((min % 5) > 2) {
            mm += 1;
        }
        mm = mm * 5;
        if (mm > 55) {
            mm = 0;
        }

        if (mm == 0) {
            return "";
        } else if (mm == 5) {
            return "five past";
        } else if (mm == 10) {
            return "ten past";
        } else if (mm == 15) {
            return "quarter past";
        } else if (mm == 20) {
            return "twenty past";
        } else if (mm == 25) {
            return "twenty-five past";
        } else if (mm == 30) {
            return "half past";
        } else if (mm == 35) {
            return "twenty-five to";
        } else if (mm == 40) {
            return "twenty to";
        } else if (mm == 45) {
            return "quarter to";
        } else if (mm == 50) {
            return "ten to";
        } else if (mm == 55) {
            return "five to";
        } else {
            return "five to";
        }
    }

    /**
     * Returns a phrase that conveys the hour in relation
     * to the hour.
     *
     * @param hour the hour of the time
     * @param min the minute of the time
     *
     * @return a string phrase.
     */
    private static String timeHour(int hour, int min) {
        int hh;

        hh = hour;
        if (min > 32)  { // PBL: fixed from flite_time
            hh += 1;
        }
        if (hh == 24) {
            hh = 0;
        }
        if (hh > 12) {
            hh -= 12;
        }

        if (hh == 0) {
            return "midnight";
        } else if (hh == 1) {
            return "one";
        } else if (hh == 2) {
            return "two";
        } else if (hh == 3) {
            return "three";
        } else if (hh == 4) {
            return "four";
        } else if (hh == 5) {
            return "five";
        } else if (hh == 6) {
            return "six";
        } else if (hh == 7) {
            return "seven";
        } else if (hh == 8) {
            return "eight";
        } else if (hh == 9) {
            return "nine";
        } else if (hh == 10) {
            return "ten";
        } else if (hh == 11) {
            return "eleven";
        } else if (hh == 12) {
            return "twelve";
        } else {
            return "twelve";
        }
    }

    /**
     * Returns a phrase that conveys the time of day.
     *
     * @param hour the hour of the time
     * @param min the minute of the time
     *
     * @return a string phrase
     */
    private static String timeOfDay(int hour, int min) {
        int hh = hour;

        if (min > 58)
            hh++;

        if (hh == 24) {
            return "";
        } else if (hh > 17) {
            return "in the evening";
        } else if (hh > 11) {
            return "in the afternoon";
        } else if ((hh == 0) && (min < 33)) {
            return "";
        } else {
            return "in the morning";
        }
    }

    /**
     * Returns a string that corresponds to the given time.
     *
     * @param time the time in the form HH:MM
     *
     * @return the time in string, null if the given time is not in the
     *   form HH:MM 
     */
    public static String timeToString(String time) {
        String theTime = null;
        if (Pattern.matches("[012][0-9]:[0-5][0-9]", time)) {
            int hour = Integer.parseInt(time.substring(0, 2));
            int min = Integer.parseInt(time.substring(3));

            theTime = timeToString(hour, min);
        }
        return theTime;
    }

    /**
     * Returns a string that corresponds to the given time.
     *
     * @param hour the hour
     * @param min the minutes
     *
     * @return the time in string, null if the given time out of range
     */
    public static String timeToString(int hour, int min) {
        String theTime = "The time is now, " +
            timeApprox(hour, min) + " " +
            timeMin(hour, min) + " " +
            timeHour(hour, min) + ", " +
            timeOfDay(hour, min) + "." ;
        return theTime;
    }
}