Subversion Repository Public Repository

WOX2

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
package wox.serial;

import org.litesoft.core.typeutils.Objects;
import java.util.*;

/**
 * Version: 1.0
 * Author: Carlos R. Jaimez Gonzalez
 * Author: Simon M. Lucas
 * Site: http://woxserializer.sourceforge.net/
 *
 * Version: 1.5
 * Author: Steven M Lewis
 *
 * Version: 2.0
 * Author: George Smith
 * SVN: http://svn.xp-dev.com/svn/WOX2/
 * Note: XML form for vs 2 is more compact and therefor incompatible with vs 1
 */

public class DateTimeConverter
{
    // The WOX format for a DateTime will be:
    //
    //                                   1234567-101234567-20123
    public static final String FORMAT = "yyyy/mm/dd-hh:mm:ss.nnn";
    //                                   01234567-101234567-2012

    private int mYear, mMonth, mDay, mHour, mMinute, mSecond, mMillisecond;

    public DateTimeConverter()
    {
    }

    @SuppressWarnings({"deprecation"})
    public DateTimeConverter( Date pDate )
    {
        mYear = pDate.getYear() + 1900;
        mMonth = pDate.getMonth() + 1;
        mDay = pDate.getDate();
        mHour = pDate.getHours();
        mMinute = pDate.getMinutes();
        mSecond = pDate.getSeconds();

        long zMillisSinceEpoch = pDate.getTime();
        int zRawMilliSec = (int) (zMillisSinceEpoch % 1000L);
        mMillisecond = (zRawMilliSec >= 0) ? zRawMilliSec : (1000 + zRawMilliSec);
    }

    @SuppressWarnings({"deprecation"})
    public Date toDate()
    {
        Date date = new Date( mYear - 1900, //
                              mMonth - 1, //
                              mDay, //
                              mHour, //
                              mMinute, //
                              mSecond );
        if ( mMillisecond != 0 )
        {
            long zMillisSinceEpoch = date.getTime();
            long zAdjusted = zMillisSinceEpoch + mMillisecond;
            date = new Date( zAdjusted );
        }
        return date;
    }

    public DateTimeConverter attemptParse( String pValue )
    {
        if ( (pValue != null) && (pValue.length() > 9) && (pValue.charAt( 4 ) == '/') && (pValue.charAt( 7 ) == '/') )
        {
            try
            {
                mYear = Integer.parseInt( pValue.substring( 0, 4 ) );
                mMonth = Integer.parseInt( pValue.substring( 5, 7 ) );
                mDay = Integer.parseInt( pValue.substring( 8, 10 ) );
                if ( CheckDoneParse( pValue, 10, '-', 2, new OutInt()
                {
                    @Override
                    public void set( int pValue )
                    {
                        mHour = pValue;
                    }
                } ) )
                {
                    return this;
                }
                if ( CheckDoneParse( pValue, 13, ':', 2, new OutInt()
                {
                    @Override
                    public void set( int pValue )
                    {
                        mMinute = pValue;
                    }
                } ) )
                {
                    return this;
                }
                if ( CheckDoneParse( pValue, 16, ':', 2, new OutInt()
                {
                    @Override
                    public void set( int pValue )
                    {
                        mSecond = pValue;
                    }
                } ) )
                {
                    return this;
                }
                if ( pValue.length() > 19 )
                {
                    if ( pValue.length() == 20 )
                    {
                        pValue += "?";
                    }
                    while ( pValue.length() < 23 )
                    {
                        pValue += "0";
                    }
                }
                if ( CheckDoneParse( pValue, 19, '.', 3, new OutInt()
                {
                    @Override
                    public void set( int pValue )
                    {
                        mMillisecond = pValue;
                    }
                } ) )
                {
                    return this;
                }
                if ( pValue.length() == 23 )
                {
                    return this;
                }
            }
            catch ( NumberFormatException e )
            {
                // Fall Thru
            }
        }
        return null;
    }

    private static boolean CheckDoneParse( String pValue, int pCharAt, char pSepChar, int pNumberLength, OutInt pNumberVar )
    {
        if ( pValue.length() == pCharAt )
        {
            return true;
        }
        if ( (pValue.length() > (pCharAt + pNumberLength)) && (pValue.charAt( pCharAt ) == pSepChar) )
        {
            pNumberVar.set( Integer.parseInt( pValue.substring( pCharAt + 1, pCharAt + 1 + pNumberLength ) ) );
        }
        return false;
    }

    public String toString()
    {
        StringBuilder zSB = new StringBuilder();

        AddNumber( mYear, zSB, 4 );
        zSB.append( '/' );
        AddNumber( mMonth, zSB, 2 );
        zSB.append( '/' );
        AddNumber( mDay, zSB, 2 );

        int zMillisecond = mMillisecond;
        if ( zMillisecond != 0 )
        {
            AddHour( zSB );
            AddMinute( zSB );
            AddSecond( zSB );
            String zMilliSecs = Integer.toString( 1000 + zMillisecond ).substring( 1 );
            while ( zMilliSecs.endsWith( "0" ) )
            {
                zMilliSecs = zMilliSecs.substring( 0, zMilliSecs.length() - 1 );
            }
            zSB.append( '.' );
            zSB.append( zMilliSecs );
        }
        else if ( mSecond != 0 )
        {
            AddHour( zSB );
            AddMinute( zSB );
            AddSecond( zSB );
        }
        else if ( mMinute != 0 )
        {
            AddHour( zSB );
            AddMinute( zSB );
        }
        else if ( mHour != 0 )
        {
            AddHour( zSB );
        }
        return zSB.toString();
    }

    private void AddHour( StringBuilder pSB )
    {
        pSB.append( '-' );
        AddNumber( mHour, pSB, 2 );
    }

    private void AddMinute( StringBuilder pSB )
    {
        pSB.append( ':' );
        AddNumber( mMinute, pSB, 2 );
    }

    private void AddSecond( StringBuilder pSB )
    {
        pSB.append( ':' );
        AddNumber( mSecond, pSB, 2 );
    }

    private static void AddNumber( int pValue, StringBuilder pSB, int pLength )
    {
        String zValue = Integer.toString( pValue );
        while ( zValue.length() < pLength-- )
        {
            pSB.append( '0' );
        }
        pSB.append( zValue );
    }

    private interface OutInt
    {
        public void set( int pValue );
    }
}

Commits for WOX2/trunk/Java/src/wox/serial/DateTimeConverter.java

Diff revisions: vs.
Revision Author Commited Message
17 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:44:02 +0000

Extracting commonfoundation

15 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 22:29:49 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000