summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcs/server/core/TimeString.java
blob: 6e5d04f33b752510ffa8aee722cfa3b5db221cae (plain)
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
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
 rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcs.server.core;

//~--- non-JDK imports --------------------------------------------------------

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import org.joda.time.format.ISODateTimeFormat;

/**

 * Represents a Time object, according to ISO 8601, and utility methods.

 *

 * @author Gulius Caesar

 */
public class TimeString {

    private static DateTimeFormatter DTF = ISODateTimeFormat.dateTime();
    private DateTime dateTime;
    private long milliseconds;
    private String strTime;

    /* Default constructor */
    public TimeString(String timeStr) throws IllegalArgumentException {
        strTime = timeStr;
        dateTime = parse(strTime);
        milliseconds = parseMillis(strTime);
    }

    public DateTime getDateTime() {
        return dateTime;
    }

    public long getMilliseconds() {
        return milliseconds;
    }

    public long subtract(String newString) throws IllegalArgumentException {
        long newD = parseMillis(newString);
        long res = milliseconds - newD;

        return res;
    }

    public double divide(String newString) throws IllegalArgumentException {
        long newD = parseMillis(newString);
        double result = milliseconds * new Double(1.0) / newD;

        return result;
    }

    /** ******************** Static methods ***************** */

    /*

     * Subtraction Method, calculates the difference in milliseconds between two dates

     */
    public static long difference(String bigDate, String smallDate) throws IllegalArgumentException {
        long smallD = parseMillis(smallDate);
        long bigD = parseMillis(bigDate);
        long result = bigD - smallD;

        return result;
    }

    /**

     * Division of one Date by the other.

     */
    public static double division(String IAmDivided, String IDivide) throws IllegalArgumentException {
        long iad = parseMillis(IAmDivided);
        long id = parseMillis(IDivide);
        double result = iad * new Double(1.0) / id;

        return result;
    }

    /*

     * Division of a millisecond-Date-respresentation by a number.

     */
    public static double division(String InputDate, long Divisor) throws IllegalArgumentException {
        long ID = parseMillis(InputDate);

        double result = ID * new Double(1.0) / Divisor;

        return result;

    }

    /**

     * Date to millisecond parser. Accepts the standard ISO 8601 + shorter codes.

     * @return long number of miliseconds

     */
    public static long parseMillis(String TString) throws IllegalArgumentException {
        long Millis;
        int NumberOfSymbols = TString.length();

        switch (NumberOfSymbols) {
            case 24:
                DTF = ISODateTimeFormat.dateTime();
                Millis = DTF.parseMillis(TString);

                break;
            case 22:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SS");
                Millis = DTF.parseMillis(TString);

                break;
            case 19:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
                Millis = DTF.parseMillis(TString);

                break;
            case 16:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm");
                Millis = DTF.parseMillis(TString);

                break;
            case 13:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH");
                Millis = DTF.parseMillis(TString);

                break;
            case 10:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd");
                Millis = DTF.parseMillis(TString);

                break;
            case 7:
                DTF = DateTimeFormat.forPattern("yyyy-MM");
                Millis = DTF.parseMillis(TString);

                break;
            case 4:
                DTF = DateTimeFormat.forPattern("yyyy");
                Millis = DTF.parseMillis(TString);

                break;
            default:
                throw new IllegalArgumentException("Unknown DateTime format.");
        }

        return Millis;
    }

    /**

     *

     * @param TString string containing date

     * @return joda DateTime corresponding to the input string

     * @throws IllegalArgumentException

     */
    public static DateTime parse(String str) throws IllegalArgumentException {
        DateTime date;
        int NumberOfSymbols = str.length();

        System.out.println("Parsing date '" + str + "', with length " + str.length());

        switch (NumberOfSymbols) {
            case 24:
                DTF = ISODateTimeFormat.dateTime();
                date = DTF.parseDateTime(str);

                break;
            case 22:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SS");
                date = DTF.parseDateTime(str);

                break;
            case 19:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
                date = DTF.parseDateTime(str);

                break;
            case 16:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm");
                date = DTF.parseDateTime(str);

                break;
            case 13:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH");
                date = DTF.parseDateTime(str);

                break;
            case 10:
                DTF = DateTimeFormat.forPattern("yyyy-MM-dd");
                date = DTF.parseDateTime(str);

                break;
            case 7:
                DTF = DateTimeFormat.forPattern("yyyy-MM");
                date = DTF.parseDateTime(str);

                break;
            case 4:
                DTF = DateTimeFormat.forPattern("yyyy");
                date = DTF.parseDateTime(str);

                break;
            default:
                throw new IllegalArgumentException("Unknown DateTime format.");
        }

        return date;
    }
}