source: josm/trunk/src/org/openstreetmap/josm/gui/layer/geoimage/Timezone.java@ 11971

Last change on this file since 11971 was 11971, checked in by Don-vip, 7 years ago

javadoc

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.text.ParseException;
7import java.util.Objects;
8
9/**
10 * Timezone in hours.<p>
11 * TODO: should probably be replaced by {@link java.util.TimeZone}.
12 * @since 11914 (extracted from {@link CorrelateGpxWithImages})
13 */
14public final class Timezone {
15
16 static final Timezone ZERO = new Timezone(0.0);
17 private final double timezone;
18
19 Timezone(double hours) {
20 this.timezone = hours;
21 }
22
23 /**
24 * Returns the timezone in hours.
25 * @return the timezone in hours
26 */
27 public double getHours() {
28 return timezone;
29 }
30
31 String formatTimezone() {
32 StringBuilder ret = new StringBuilder();
33
34 double timezone = this.timezone;
35 if (timezone < 0) {
36 ret.append('-');
37 timezone = -timezone;
38 } else {
39 ret.append('+');
40 }
41 ret.append((long) timezone).append(':');
42 int minutes = (int) ((timezone % 1) * 60);
43 if (minutes < 10) {
44 ret.append('0');
45 }
46 ret.append(minutes);
47
48 return ret.toString();
49 }
50
51 static Timezone parseTimezone(String timezone) throws ParseException {
52
53 if (timezone.isEmpty())
54 return ZERO;
55
56 String error = tr("Error while parsing timezone.\nExpected format: {0}", "+H:MM");
57
58 char sgnTimezone = '+';
59 StringBuilder hTimezone = new StringBuilder();
60 StringBuilder mTimezone = new StringBuilder();
61 int state = 1; // 1=start/sign, 2=hours, 3=minutes.
62 for (int i = 0; i < timezone.length(); i++) {
63 char c = timezone.charAt(i);
64 switch (c) {
65 case ' ':
66 if (state != 2 || hTimezone.length() != 0)
67 throw new ParseException(error, i);
68 break;
69 case '+':
70 case '-':
71 if (state == 1) {
72 sgnTimezone = c;
73 state = 2;
74 } else
75 throw new ParseException(error, i);
76 break;
77 case ':':
78 case '.':
79 if (state == 2) {
80 state = 3;
81 } else
82 throw new ParseException(error, i);
83 break;
84 case '0':
85 case '1':
86 case '2':
87 case '3':
88 case '4':
89 case '5':
90 case '6':
91 case '7':
92 case '8':
93 case '9':
94 switch (state) {
95 case 1:
96 case 2:
97 state = 2;
98 hTimezone.append(c);
99 break;
100 case 3:
101 mTimezone.append(c);
102 break;
103 default:
104 throw new ParseException(error, i);
105 }
106 break;
107 default:
108 throw new ParseException(error, i);
109 }
110 }
111
112 int h = 0;
113 int m = 0;
114 try {
115 h = Integer.parseInt(hTimezone.toString());
116 if (mTimezone.length() > 0) {
117 m = Integer.parseInt(mTimezone.toString());
118 }
119 } catch (NumberFormatException nfe) {
120 // Invalid timezone
121 throw (ParseException) new ParseException(error, 0).initCause(nfe);
122 }
123
124 if (h > 12 || m > 59)
125 throw new ParseException(error, 0);
126 else
127 return new Timezone((h + m / 60.0) * (sgnTimezone == '-' ? -1 : 1));
128 }
129
130 @Override
131 public boolean equals(Object o) {
132 if (this == o) return true;
133 if (!(o instanceof Timezone)) return false;
134 Timezone timezone1 = (Timezone) o;
135 return Double.compare(timezone1.timezone, timezone) == 0;
136 }
137
138 @Override
139 public int hashCode() {
140 return Objects.hash(timezone);
141 }
142}
Note: See TracBrowser for help on using the repository browser.