source: josm/trunk/src/org/openstreetmap/josm/gui/layer/geoimage/Offset.java@ 11914

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

sonar - squid:S2972 - Inner classes should not have too many lines of code

  • Property svn:eol-style set to native
File size: 3.0 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.Locale;
8import java.util.Objects;
9import java.util.concurrent.TimeUnit;
10
11import org.openstreetmap.josm.tools.Pair;
12
13public final class Offset {
14
15 static final Offset ZERO = new Offset(0);
16 private final long milliseconds;
17
18 private Offset(long milliseconds) {
19 this.milliseconds = milliseconds;
20 }
21
22 static Offset milliseconds(long milliseconds) {
23 return new Offset(milliseconds);
24 }
25
26 static Offset seconds(long seconds) {
27 return new Offset(1000 * seconds);
28 }
29
30 long getMilliseconds() {
31 return milliseconds;
32 }
33
34 long getSeconds() {
35 return milliseconds / 1000;
36 }
37
38 String formatOffset() {
39 if (milliseconds % 1000 == 0) {
40 return Long.toString(milliseconds / 1000);
41 } else if (milliseconds % 100 == 0) {
42 return String.format(Locale.ENGLISH, "%.1f", milliseconds / 1000.);
43 } else {
44 return String.format(Locale.ENGLISH, "%.3f", milliseconds / 1000.);
45 }
46 }
47
48 static Offset parseOffset(String offset) throws ParseException {
49 String error = tr("Error while parsing offset.\nExpected format: {0}", "number");
50
51 if (!offset.isEmpty()) {
52 try {
53 if (offset.startsWith("+")) {
54 offset = offset.substring(1);
55 }
56 return Offset.milliseconds(Math.round(Double.parseDouble(offset) * 1000));
57 } catch (NumberFormatException nfe) {
58 throw (ParseException) new ParseException(error, 0).initCause(nfe);
59 }
60 } else {
61 return Offset.ZERO;
62 }
63 }
64
65 int getDayOffset() {
66 // Find day difference
67 return (int) Math.round(((double) getMilliseconds()) / TimeUnit.DAYS.toMillis(1));
68 }
69
70 Offset withoutDayOffset() {
71 return milliseconds(getMilliseconds() - TimeUnit.DAYS.toMillis(getDayOffset()));
72 }
73
74 Pair<Timezone, Offset> splitOutTimezone() {
75 // In hours
76 final double tz = ((double) withoutDayOffset().getSeconds()) / TimeUnit.HOURS.toSeconds(1);
77
78 // Due to imprecise clocks we might get a "+3:28" timezone, which should obviously be 3:30 with
79 // -2 minutes offset. This determines the real timezone and finds offset.
80 final double timezone = (double) Math.round(tz * 2) / 2; // hours, rounded to one decimal place
81 final long delta = Math.round(getMilliseconds() - timezone * TimeUnit.HOURS.toMillis(1));
82 return Pair.create(new Timezone(timezone), Offset.milliseconds(delta));
83 }
84
85 @Override
86 public boolean equals(Object o) {
87 if (this == o) return true;
88 if (!(o instanceof Offset)) return false;
89 Offset offset = (Offset) o;
90 return milliseconds == offset.milliseconds;
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(milliseconds);
96 }
97}
Note: See TracBrowser for help on using the repository browser.