zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
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
233
234
235
236
237
238
239
240
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
namespace HH.WMS.Utils.NPOI.SS.Format
{
    using System;
    using System.Text.RegularExpressions;
    using System.Text;
    using HH.WMS.Utils.NPOI.SS.Util;
 
    /**
     * Formats a date value.
     *
     * @author Ken Arnold, Industrious Media LLC
     */
    public class CellDateFormatter : CellFormatter
    {
        private bool amPmUpper;
        private bool ShowM;
        private bool ShowAmPm;
        private FormatBase dateFmt;
        private String sFmt;
 
        private static TimeSpan EXCEL_EPOCH_TIME;
        private static DateTime EXCEL_EPOCH_DATE;
 
        private static CellFormatter SIMPLE_DATE = new CellDateFormatter(
                "mm/d/y");
 
        static CellDateFormatter()
        {
            DateTime c = new DateTime(1904, 1, 1);
            EXCEL_EPOCH_DATE = c;
            EXCEL_EPOCH_TIME = c.TimeOfDay;
        }
 
        private class DatePartHandler : CellFormatPart.IPartHandler
        {
            private CellDateFormatter _formatter;
            private int mStart = -1;
            private int mLen;
            private int hStart = -1;
            private int hLen;
            public DatePartHandler(CellDateFormatter formatter)
            {
                this._formatter = formatter;
            }
            public String HandlePart(Match m, String part, CellFormatType type, StringBuilder desc)
            {
 
                int pos = desc.Length;
                char firstCh = part[0];
                switch (firstCh)
                {
                    case 's':
                    case 'S':
                        if (mStart >= 0)
                        {
                            for (int i = 0; i < mLen; i++)
                                desc[mStart + i] = 'm';
                            mStart = -1;
                        }
                        return part.ToLower();
 
                    case 'h':
                    case 'H':
                        mStart = -1;
                        hStart = pos;
                        hLen = part.Length;
                        return part.ToLower();
 
                    case 'd':
                    case 'D':
                        mStart = -1;
                        if (part.Length <= 2)
                            return part.ToLower();
                        else
                            return part.ToLower().Replace('d', 'E');
 
                    case 'm':
                    case 'M':
                        mStart = pos;
                        mLen = part.Length;
                        return part.ToUpper();
 
                    case 'y':
                    case 'Y':
                        mStart = -1;
                        if (part.Length == 3)
                            part = "yyyy";
                        return part.ToLower();
 
                    case '0':
                        mStart = -1;
                        int sLen = part.Length;
                        _formatter.sFmt = "%0" + (sLen + 2) + "." + sLen + "f";
                        return part.Replace('0', 'S');
 
                    case 'a':
                    case 'A':
                    case 'p':
                    case 'P':
                        if (part.Length > 1)
                        {
                            // am/pm marker
                            mStart = -1;
                            _formatter.ShowAmPm = true;
                            _formatter.ShowM = char.ToLower(part[1]) == 'm';
                            // For some reason "am/pm" becomes AM or PM, but "a/p" becomes a or p
                            _formatter.amPmUpper = _formatter.ShowM || char.IsUpper(part[0]);
 
                            return "a";
                        }
                    //noinspection fallthrough
                        return null;
                    default:
                        return null;
                }
            }
 
            public void Finish(StringBuilder toAppendTo)
            {
                if (hStart >= 0 && !_formatter.ShowAmPm)
                {
                    for (int i = 0; i < hLen; i++)
                    {
                        toAppendTo[hStart + i] = 'H';
                    }
                }
            }
        }
 
        /**
         * Creates a new date formatter with the given specification.
         *
         * @param format The format.
         */
        public CellDateFormatter(String format)
            : base(format)
        {
            ;
            DatePartHandler partHandler = new DatePartHandler(this);
            StringBuilder descBuf = CellFormatPart.ParseFormat(format,
                    CellFormatType.DATE, partHandler);
            partHandler.Finish(descBuf);
            dateFmt = new SimpleDateFormat(descBuf.ToString());
        }
 
        /** {@inheritDoc} */
        public override void FormatValue(StringBuilder toAppendTo, Object value)
        {
            if (value == null)
                value = 0.0;
            if (value.GetType().IsPrimitive/* is Number*/)
            {
                double num = (double)value;
                double v = num;
                if (v == 0.0)
                    value = EXCEL_EPOCH_DATE;
                else
                    value = new DateTime((long)(EXCEL_EPOCH_TIME.Ticks + v));
            }
            
            throw new NotImplementedException();
            //AttributedCharacterIterator it = dateFmt.FormatToCharacterIterator(
            //        value);
            //bool doneAm = false;
            //bool doneMillis = false;
 
            //it.First();
            //for (char ch = it.First();
            //     ch != CharacterIterator.DONE;
            //     ch = it.Next())
            //{
            //    if (it.GetAttribute(DateFormat.Field.MILLISECOND) != null)
            //    {
            //        if (!doneMillis)
            //        {
            //            Date dateObj = (Date)value;
            //            int pos = toAppendTo.Length();
            //            Formatter formatter = new Formatter(toAppendTo);
            //            long msecs = dateObj.Time % 1000;
            //            formatter.Format(LOCALE, sFmt, msecs / 1000.0);
            //            toAppendTo.Remove(pos, 2);
            //            doneMillis = true;
            //        }
            //    }
            //    else if (it.GetAttribute(DateFormat.Field.AM_PM) != null)
            //    {
            //        if (!doneAm)
            //        {
            //            if (ShowAmPm)
            //            {
            //                if (amPmUpper)
            //                {
            //                    toAppendTo.Append(char.ToUpper(ch));
            //                    if (ShowM)
            //                        toAppendTo.Append('M');
            //                }
            //                else
            //                {
            //                    toAppendTo.Append(char.ToLower(ch));
            //                    if (ShowM)
            //                        toAppendTo.Append('m');
            //                }
            //            }
            //            doneAm = true;
            //        }
            //    }
            //    else
            //    {
            //        toAppendTo.Append(ch);
            //    }
            //}
        }
 
        /**
         * {@inheritDoc}
         * <p/>
         * For a date, this is <tt>"mm/d/y"</tt>.
         */
        public override void SimpleValue(StringBuilder toAppendTo, Object value)
        {
            SIMPLE_DATE.FormatValue(toAppendTo, value);
        }
    }
}