File: Strftime.java

package info (click to toggle)
tomcat11 11.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,360 kB
  • sloc: java: 366,026; xml: 55,052; jsp: 4,700; sh: 1,304; perl: 314; makefile: 25; ansic: 15
file content (252 lines) | stat: -rw-r--r-- 8,935 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.TimeZone;

/**
 * Converts dates to strings using the same format specifiers as strftime Notes:
 * <ul>
 * <li>This does not mimic strftime perfectly. Certain strftime commands, are not supported, and will convert as if they
 * were literals.</li>
 * <li>Certain complicated commands, like those dealing with the week of the year probably don't have exactly the same
 * behavior as strftime.</li>
 * <li>These limitations are due to use SimpleDateTime. If the conversion was done manually, all these limitations could
 * be eliminated.</li>
 * <li>The interface looks like a subset of DateFormat. Maybe someday someone will make this class extend
 * DateFormat.</li>
 * </ul>
 *
 * @author Bip Thelin
 * @author Dan Sandberg
 */
public class Strftime {
    protected static final Properties translate;
    protected final SimpleDateFormat simpleDateFormat;

    /*
     * Initialize our pattern translation
     */
    static {
        translate = new Properties();
        translate.put("a", "EEE");
        translate.put("A", "EEEE");
        translate.put("b", "MMM");
        translate.put("B", "MMMM");
        translate.put("c", "EEE MMM d HH:mm:ss yyyy");

        // There's no way to specify the century in SimpleDateFormat. We don't want to hard-code
        // 20 since this could be wrong for the pre-2000 files.
        // translate.put("C", "20");
        translate.put("d", "dd");
        translate.put("D", "MM/dd/yy");
        translate.put("e", "dd"); // will show as '03' instead of ' 3'
        translate.put("F", "yyyy-MM-dd");
        translate.put("g", "yy");
        translate.put("G", "yyyy");
        translate.put("H", "HH");
        translate.put("h", "MMM");
        translate.put("I", "hh");
        translate.put("j", "DDD");
        translate.put("k", "HH"); // will show as '07' instead of ' 7'
        translate.put("l", "hh"); // will show as '07' instead of ' 7'
        translate.put("m", "MM");
        translate.put("M", "mm");
        translate.put("n", "\n");
        translate.put("p", "a");
        translate.put("P", "a"); // will show as pm instead of PM
        translate.put("r", "hh:mm:ss a");
        translate.put("R", "HH:mm");
        // There's no way to specify this with SimpleDateFormat
        // translate.put("s","seconds since epoch");
        translate.put("S", "ss");
        translate.put("t", "\t");
        translate.put("T", "HH:mm:ss");
        // There's no way to specify this with SimpleDateFormat
        // translate.put("u","day of week ( 1-7 )");

        // There's no way to specify this with SimpleDateFormat
        // translate.put("U","week in year with first Sunday as first day...");

        translate.put("V", "ww"); // I'm not sure this is always exactly the same

        // There's no way to specify this with SimpleDateFormat
        // translate.put("W","week in year with first Monday as first day...");

        // There's no way to specify this with SimpleDateFormat
        // translate.put("w","E");
        translate.put("X", "HH:mm:ss");
        translate.put("x", "MM/dd/yy");
        translate.put("y", "yy");
        translate.put("Y", "yyyy");
        translate.put("Z", "z");
        translate.put("z", "Z");
        translate.put("%", "%");
    }


    /**
     * Create an instance of this date formatting class
     *
     * @param origFormat the strftime-style formatting string
     * @param locale     the locale to use for locale-specific conversions
     */
    public Strftime(String origFormat, Locale locale) {
        String convertedFormat = convertDateFormat(origFormat);
        simpleDateFormat = new SimpleDateFormat(convertedFormat, locale);
    }

    /**
     * Format the date according to the strftime-style string given in the constructor.
     *
     * @param date the date to format
     *
     * @return the formatted date
     */
    public String format(Date date) {
        return simpleDateFormat.format(date);
    }

    /**
     * Get the timezone used for formatting conversions
     *
     * @return the timezone
     */
    public TimeZone getTimeZone() {
        return simpleDateFormat.getTimeZone();
    }

    /**
     * Change the timezone used to format dates
     *
     * @param timeZone The new time zone
     *
     * @see SimpleDateFormat#setTimeZone
     */
    public void setTimeZone(TimeZone timeZone) {
        simpleDateFormat.setTimeZone(timeZone);
    }

    /**
     * Search the provided pattern and get the C standard Date/Time formatting rules and convert them to the Java
     * equivalent.
     *
     * @param pattern The pattern to search
     *
     * @return The modified pattern
     */
    protected String convertDateFormat(String pattern) {
        boolean inside = false;
        boolean mark = false;
        boolean modifiedCommand = false;

        StringBuilder buf = new StringBuilder();

        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);

            if (c == '%' && !mark) {
                mark = true;
            } else {
                if (mark) {
                    if (modifiedCommand) {
                        // don't do anything--we just wanted to skip a char
                        modifiedCommand = false;
                        mark = false;
                    } else {
                        inside = translateCommand(buf, pattern, i, inside);
                        // It's a modifier code
                        if (c == 'O' || c == 'E') {
                            modifiedCommand = true;
                        } else {
                            mark = false;
                        }
                    }
                } else {
                    if (!inside && c != ' ') {
                        // We start a literal, which we need to quote
                        buf.append('\'');
                        inside = true;
                    }

                    buf.append(c);
                }
            }
        }

        if (!buf.isEmpty()) {
            char lastChar = buf.charAt(buf.length() - 1);

            if (lastChar != '\'' && inside) {
                buf.append('\'');
            }
        }
        return buf.toString();
    }

    protected String quote(String str, boolean insideQuotes) {
        String retVal = str;
        if (!insideQuotes) {
            retVal = '\'' + retVal + '\'';
        }
        return retVal;
    }

    /**
     * Try to get the Java Date/Time formatting associated with the C standard provided.
     *
     * @param buf       The buffer
     * @param pattern   The date/time pattern
     * @param index     The char index
     * @param oldInside Flag value
     *
     * @return True if new is inside buffer
     */
    protected boolean translateCommand(StringBuilder buf, String pattern, int index, boolean oldInside) {
        char firstChar = pattern.charAt(index);
        boolean newInside = oldInside;

        // O and E are modifiers, they mean to present an alternative representation of the next char
        // we just handle the next char as if the O or E wasn't there
        if (firstChar == 'O' || firstChar == 'E') {
            if (index + 1 < pattern.length()) {
                newInside = translateCommand(buf, pattern, index + 1, oldInside);
            } else {
                buf.append(quote("%" + firstChar, oldInside));
            }
        } else {
            String command = translate.getProperty(String.valueOf(firstChar));

            // If we don't find a format, treat it as a literal--That's what apache does
            if (command == null) {
                buf.append(quote("%" + firstChar, oldInside));
            } else {
                // If we were inside quotes, close the quotes
                if (oldInside) {
                    buf.append('\'');
                }
                buf.append(command);
                newInside = false;
            }
        }
        return newInside;
    }
}