/* ==================================================================== 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.UserModel { using System; using HH.WMS.Utils.NPOI.SS.Util; using System.Text; using System.Text.RegularExpressions; using System.Globalization; /** * A wrapper around a {@link SimpleDateFormat} instance, * which handles a few Excel-style extensions that * are not supported by {@link SimpleDateFormat}. * Currently, the extensions are around the handling * of elapsed time, eg rendering 1 day 2 hours * as 26 hours. */ public class ExcelStyleDateFormatter : SimpleDateFormat { public static char MMMMM_START_SYMBOL = '\ue001'; public static char MMMMM_TRUNCATE_SYMBOL = '\ue002'; public static char H_BRACKET_SYMBOL = '\ue010'; public static char HH_BRACKET_SYMBOL = '\ue011'; public static char M_BRACKET_SYMBOL = '\ue012'; public static char MM_BRACKET_SYMBOL = '\ue013'; public static char S_BRACKET_SYMBOL = '\ue014'; public static char SS_BRACKET_SYMBOL = '\ue015'; public static char L_BRACKET_SYMBOL = '\ue016'; public static char LL_BRACKET_SYMBOL = '\ue017'; private DecimalFormat format1digit = new DecimalFormat("0"); private DecimalFormat format2digits = new DecimalFormat("00"); private DecimalFormat format3digit = new DecimalFormat("0"); private DecimalFormat format4digits = new DecimalFormat("00"); static ExcelStyleDateFormatter() { //DataFormatter.SetExcelStyleRoundingMode(format1digit, RoundingMode.DOWN); //DataFormatter.SetExcelStyleRoundingMode(format2digits, RoundingMode.DOWN); //DataFormatter.SetExcelStyleRoundingMode(format3digit); //DataFormatter.SetExcelStyleRoundingMode(format4digits); } private double dateToBeFormatted = 0.0; public ExcelStyleDateFormatter() : base() { } public ExcelStyleDateFormatter(String pattern) : base(ProcessFormatPattern(pattern)) { } //public ExcelStyleDateFormatter(String pattern, // DateFormatSymbols formatSymbols) //{ // super(processFormatPattern(pattern), formatSymbols); //} //public ExcelStyleDateFormatter(String pattern, Locale locale) //{ // super(processFormatPattern(pattern), locale); //} private static string DateTimeMatchEvaluator(Match match) { return match.Groups[1].Value; } /** * Takes a format String, and Replaces Excel specific bits * with our detection sequences */ private static String ProcessFormatPattern(String f) { String t = f.Replace("MMMMM", MMMMM_START_SYMBOL + "MMM" + MMMMM_TRUNCATE_SYMBOL); t = Regex.Replace(t, "\\[H\\]", (H_BRACKET_SYMBOL).ToString(), RegexOptions.IgnoreCase); t = Regex.Replace(t, "\\[HH\\]", (HH_BRACKET_SYMBOL).ToString(), RegexOptions.IgnoreCase); t = Regex.Replace(t, "\\[m\\]", (M_BRACKET_SYMBOL).ToString(), RegexOptions.IgnoreCase); t = Regex.Replace(t, "\\[mm\\]", (MM_BRACKET_SYMBOL).ToString(), RegexOptions.IgnoreCase); t = Regex.Replace(t, "\\[s\\]", (S_BRACKET_SYMBOL).ToString(), RegexOptions.IgnoreCase); t = Regex.Replace(t, "\\[ss\\]", (SS_BRACKET_SYMBOL).ToString(), RegexOptions.IgnoreCase); t = t.Replace("s.000", "s.fff"); t = t.Replace("s.00", "s." + LL_BRACKET_SYMBOL); t = t.Replace("s.0", "s." + L_BRACKET_SYMBOL); //only one char 'M' //see http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#UsingSingleSpecifiers t = Regex.Replace(t, "(?