Package: INTERNALDATE
INTERNALDATE
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
INTERNALDATE(FetchResponse) |
|
|
|
|
|
||||||||||||||||||||
format(Date) |
|
|
|
|
|
||||||||||||||||||||
getDate() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.imap.protocol;
18:
19: import java.util.Date;
20: import java.util.TimeZone;
21: import java.util.Locale;
22: import java.text.ParseException;
23: import java.text.SimpleDateFormat;
24: import java.text.FieldPosition;
25:
26: import jakarta.mail.internet.MailDateFormat;
27:
28: import org.eclipse.angus.mail.iap.*;
29: import org.eclipse.angus.mail.iap.ParsingException;
30:
31:
32: /**
33: * An INTERNALDATE FETCH item.
34: *
35: * @author John Mani
36: */
37:
38: public class INTERNALDATE implements Item {
39:
40: static final char[] name =
41:         {'I','N','T','E','R','N','A','L','D','A','T','E'};
42: public int msgno;
43: protected Date date;
44:
45: /*
46: * Used to parse dates only. The parse method is thread safe
47: * so we only need to create a single object for use by all
48: * instances. We depend on the fact that the MailDateFormat
49: * class will parse dates in INTERNALDATE format as well as
50: * dates in RFC 822 format.
51: */
52: private static final MailDateFormat mailDateFormat = new MailDateFormat();
53:
54: /**
55: * Constructor.
56: *
57: * @param        r        the FetchResponse
58: * @exception ParsingException for parsing failures
59: */
60: public INTERNALDATE(FetchResponse r) throws ParsingException {
61:         msgno = r.getNumber();
62:         r.skipSpaces();
63:         String s = r.readString();
64:•        if (s == null)
65:          throw new ParsingException("INTERNALDATE is NIL");
66:         try {
67: synchronized (mailDateFormat) {
68: date = mailDateFormat.parse(s);
69: }
70:         } catch (ParseException pex) {
71:          throw new ParsingException("INTERNALDATE parse error");
72:         }
73: }
74:
75: public Date getDate() {
76:         return date;
77: }
78:
79: // INTERNALDATE formatter
80:
81: private static SimpleDateFormat df =
82:         // Need Locale.US, the "MMM" field can produce unexpected values
83:         // in non US locales !
84:         new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss ", Locale.US);
85:
86: /**
87: * Format given Date object into INTERNALDATE string
88: *
89: * @param        d        the Date
90: * @return                INTERNALDATE string
91: */
92: public static String format(Date d) {
93:         /*
94:          * SimpleDateFormat objects aren't thread safe, so rather
95:          * than create a separate such object for each request,
96:          * we create one object and synchronize its use here
97:          * so that only one thread is using it at a time. This
98:          * trades off some potential concurrency for speed in the
99:          * common case.
100:          *
101:          * This method is only used when formatting the date in a
102:          * message that's being appended to a folder.
103:          */
104:         StringBuffer sb = new StringBuffer();
105:         synchronized (df) {
106:          df.format(d, sb, new FieldPosition(0));
107:         }
108:
109:         // compute timezone offset string
110:         TimeZone tz = TimeZone.getDefault();
111:         int offset = tz.getOffset(d.getTime());        // get offset from GMT
112:         int rawOffsetInMins = offset / 60 / 1000; // offset from GMT in mins
113:•        if (rawOffsetInMins < 0) {
114:          sb.append('-');
115:          rawOffsetInMins = (-rawOffsetInMins);
116:         } else
117:          sb.append('+');
118:         
119:         int offsetInHrs = rawOffsetInMins / 60;
120:         int offsetInMins = rawOffsetInMins % 60;
121:
122:         sb.append(Character.forDigit((offsetInHrs/10), 10));
123:         sb.append(Character.forDigit((offsetInHrs%10), 10));
124:         sb.append(Character.forDigit((offsetInMins/10), 10));
125:         sb.append(Character.forDigit((offsetInMins%10), 10));
126:
127:         return sb.toString();
128: }
129: }