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
using System;
using System.Text;

/// <header>
/// <wox>
///     <version>1.0</version>
///     <author>Carlos R. Jaimez Gonzalez</author>
///     <author>Simon M. Lucas</author>
///     <site>http://woxserializer.sourceforge.net/</site>
/// </wox>
/// <wox>
///     <version>1.5</version>
///     <author>Steven M Lewis</author>
/// </wox>
/// <wox>
///     <version>2.0</version>
///     <author>George A Smith</author>
///     <svn>http://woxserializer.sourceforge.net/</svn>
///     <note>XML form for vs 2 is more compact and therefor incompatible with vs 1</note>
/// </wox>
/// </header>

namespace wox.serial
{
    public class DateTimeConverter
    {
        // The WOX format for a DateTime will be:
        //
        //                            1234567-101234567-20123
        public const string FORMAT = "yyyy/mm/dd-hh:mm:ss.nnn";
        //                            01234567-101234567-2012

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

        public DateTimeConverter()
        {
        }

        public DateTimeConverter( DateTime pDateTime )
        {
            mYear = pDateTime.Year;
            mMonth = pDateTime.Month;
            mDay = pDateTime.Day;
            mHour = pDateTime.Hour;
            mMinute = pDateTime.Minute;
            mSecond = pDateTime.Second;
            mMillisecond = pDateTime.Millisecond;
        }

        public DateTimeConverter AttemptParse( string pValue )
        {
            if ( (pValue != null) && (pValue.Length > 9) && (pValue[4] == '/') && (pValue[7] == '/') )
            {
                try
                {
                    mYear = int.Parse( pValue.Substring( 0, 4 ) );
                    mMonth = int.Parse( pValue.Substring( 5, 2 ) );
                    mDay = int.Parse( pValue.Substring( 8, 2 ) );
                    if ( CheckDoneParse( pValue, 10, '-', 2, ref mHour ) )
                    {
                        return this;
                    }
                    if ( CheckDoneParse( pValue, 13, ':', 2, ref mMinute ) )
                    {
                        return this;
                    }
                    if ( CheckDoneParse( pValue, 16, ':', 2, ref mSecond ) )
                    {
                        return this;
                    }
                    if ( pValue.Length > 19 )
                    {
                        if ( pValue.Length == 20 )
                        {
                            pValue += "?";
                        }
                        while ( pValue.Length < 23 )
                        {
                            pValue += "0";
                        }
                    }
                    if ( CheckDoneParse( pValue, 19, '.', 3, ref mMillisecond ) )
                    {
                        return this;
                    }
                    if ( pValue.Length == 23 )
                    {
                        return this;
                    }
                }
                catch ( FormatException )
                {
                    // Fall Thru...
                }
            }
            return null;
        }

        private static bool CheckDoneParse( string pValue, int pCharAt, char pSepChar, int pNumberLength, ref int pNumberVar )
        {
            if ( pValue.Length == pCharAt )
            {
                return true;
            }
            if ( (pValue.Length > (pCharAt + pNumberLength)) && (pValue[pCharAt] == pSepChar) )
            {
                pNumberVar = int.Parse( pValue.Substring( pCharAt + 1, pNumberLength ) );
            }
            return false;
        }

        public DateTime ToDateTime()
        {
            return new DateTime( mYear, mMonth, mDay, mHour, mMinute, mSecond, mMillisecond );
        }

        public override 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 = (1000 + zMillisecond).ToString().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 = pValue.ToString();
            while ( zValue.Length < pLength-- )
            {
                pSB.Append( '0' );
            }
            pSB.Append( zValue );
        }
    }
}

Commits for WOX2/trunk/CSharp/src/wox/serial/DateTimeConverter.cs

Diff revisions: vs.
Revision Author Commited Message
14 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 19:15:03 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000