[12074] | 1 | /*******************************************************************************
|
---|
| 2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
| 3 | *
|
---|
| 4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
| 5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
| 6 | *
|
---|
| 7 | * Copyright (C) 2011 Jan Källman
|
---|
| 8 | *
|
---|
| 9 | * This library is free software; you can redistribute it and/or
|
---|
| 10 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 11 | * License as published by the Free Software Foundation; either
|
---|
| 12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
| 13 |
|
---|
| 14 | * This library is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 17 | * See the GNU Lesser General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
| 20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
| 21 | *
|
---|
| 22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
| 23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
| 24 | *
|
---|
| 25 | * Code change notes:
|
---|
| 26 | *
|
---|
| 27 | * Author Change Date
|
---|
| 28 | * ******************************************************************************
|
---|
| 29 | * Mats Alm Added 2011-01-01
|
---|
| 30 | * Jan Källman License changed GPL-->LGPL 2011-12-27
|
---|
| 31 | *******************************************************************************/
|
---|
| 32 | using System;
|
---|
| 33 | using System.Collections.Generic;
|
---|
| 34 | using System.Linq;
|
---|
| 35 | using System.Text;
|
---|
| 36 | using System.Globalization;
|
---|
| 37 |
|
---|
| 38 | namespace OfficeOpenXml.DataValidation
|
---|
| 39 | {
|
---|
| 40 | /// <summary>
|
---|
| 41 | /// Represents a time between 00:00:00 and 23:59:59
|
---|
| 42 | /// </summary>
|
---|
| 43 | public class ExcelTime
|
---|
| 44 | {
|
---|
| 45 | private event EventHandler _timeChanged;
|
---|
| 46 | private readonly decimal SecondsPerDay = 3600 * 24;
|
---|
| 47 | private readonly decimal SecondsPerHour = 3600;
|
---|
| 48 | private readonly decimal SecondsPerMinute = 60;
|
---|
| 49 | /// <summary>
|
---|
| 50 | /// Max number of decimals when rounding.
|
---|
| 51 | /// </summary>
|
---|
| 52 | public const int NumberOfDecimals = 15;
|
---|
| 53 |
|
---|
| 54 | /// <summary>
|
---|
| 55 | /// Default constructor
|
---|
| 56 | /// </summary>
|
---|
| 57 | public ExcelTime()
|
---|
| 58 | {
|
---|
| 59 |
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /// <summary>
|
---|
| 63 | /// Constructor
|
---|
| 64 | /// </summary>
|
---|
| 65 | /// <param name="value">An existing time for initialization</param>
|
---|
| 66 | public ExcelTime(decimal value)
|
---|
| 67 | {
|
---|
| 68 | if (value < 0M)
|
---|
| 69 | {
|
---|
| 70 | throw new ArgumentException("Value cannot be less than 0");
|
---|
| 71 | }
|
---|
| 72 | else if (value >= 1M)
|
---|
| 73 | {
|
---|
| 74 | throw new ArgumentException("Value cannot be greater or equal to 1");
|
---|
| 75 | }
|
---|
| 76 | Init(value);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private void Init(decimal value)
|
---|
| 80 | {
|
---|
| 81 | // handle hour
|
---|
| 82 | decimal totalSeconds = value * SecondsPerDay;
|
---|
| 83 | decimal hour = Math.Floor(totalSeconds / SecondsPerHour);
|
---|
| 84 | Hour = (int)hour;
|
---|
| 85 |
|
---|
| 86 | // handle minute
|
---|
| 87 | decimal remainingSeconds = totalSeconds - (hour * SecondsPerHour);
|
---|
| 88 | decimal minute = Math.Floor(remainingSeconds / SecondsPerMinute);
|
---|
| 89 | Minute = (int)minute;
|
---|
| 90 |
|
---|
| 91 | // handle second
|
---|
| 92 | remainingSeconds = totalSeconds - (hour * SecondsPerHour) - (minute * SecondsPerMinute);
|
---|
| 93 | decimal second = Math.Round(remainingSeconds, MidpointRounding.AwayFromZero);
|
---|
| 94 | // Second might be rounded to 60... the SetSecond method handles that.
|
---|
| 95 | SetSecond((int)second);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /// <summary>
|
---|
| 99 | /// If we are unlucky second might be rounded up to 60. This will have the minute to be raised and might affect the hour.
|
---|
| 100 | /// </summary>
|
---|
| 101 | /// <param name="value"></param>
|
---|
| 102 | private void SetSecond(int value)
|
---|
| 103 | {
|
---|
| 104 | if (value == 60)
|
---|
| 105 | {
|
---|
| 106 | Second = 0;
|
---|
| 107 | var minute = Minute + 1;
|
---|
| 108 | SetMinute(minute);
|
---|
| 109 | }
|
---|
| 110 | else
|
---|
| 111 | {
|
---|
| 112 | Second = value;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | private void SetMinute(int value)
|
---|
| 117 | {
|
---|
| 118 | if (value == 60)
|
---|
| 119 | {
|
---|
| 120 | Minute = 0;
|
---|
| 121 | var hour = Hour + 1;
|
---|
| 122 | SetHour(hour);
|
---|
| 123 | }
|
---|
| 124 | else
|
---|
| 125 | {
|
---|
| 126 | Minute = value;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | private void SetHour(int value)
|
---|
| 131 | {
|
---|
| 132 | if (value == 24)
|
---|
| 133 | {
|
---|
| 134 | Hour = 0;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | internal event EventHandler TimeChanged
|
---|
| 139 | {
|
---|
| 140 | add { _timeChanged += value; }
|
---|
| 141 | remove { _timeChanged -= value; }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private void OnTimeChanged()
|
---|
| 145 | {
|
---|
| 146 | if (_timeChanged != null)
|
---|
| 147 | {
|
---|
| 148 | _timeChanged(this, EventArgs.Empty);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | private int _hour;
|
---|
| 153 | /// <summary>
|
---|
| 154 | /// Hour between 0 and 23
|
---|
| 155 | /// </summary>
|
---|
| 156 | public int Hour
|
---|
| 157 | {
|
---|
| 158 | get
|
---|
| 159 | {
|
---|
| 160 | return _hour;
|
---|
| 161 | }
|
---|
| 162 | set
|
---|
| 163 | {
|
---|
| 164 | if (value < 0)
|
---|
| 165 | {
|
---|
| 166 | throw new InvalidOperationException("Value for hour cannot be negative");
|
---|
| 167 | }
|
---|
| 168 | if (value > 23)
|
---|
| 169 | {
|
---|
| 170 | throw new InvalidOperationException("Value for hour cannot be greater than 23");
|
---|
| 171 | }
|
---|
| 172 | _hour = value;
|
---|
| 173 | OnTimeChanged();
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | private int _minute;
|
---|
| 178 | /// <summary>
|
---|
| 179 | /// Minute between 0 and 59
|
---|
| 180 | /// </summary>
|
---|
| 181 | public int Minute
|
---|
| 182 | {
|
---|
| 183 | get
|
---|
| 184 | {
|
---|
| 185 | return _minute;
|
---|
| 186 | }
|
---|
| 187 | set
|
---|
| 188 | {
|
---|
| 189 | if (value < 0)
|
---|
| 190 | {
|
---|
| 191 | throw new InvalidOperationException("Value for minute cannot be negative");
|
---|
| 192 | }
|
---|
| 193 | if (value > 59)
|
---|
| 194 | {
|
---|
| 195 | throw new InvalidOperationException("Value for minute cannot be greater than 59");
|
---|
| 196 | }
|
---|
| 197 | _minute = value;
|
---|
| 198 | OnTimeChanged();
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | private int? _second;
|
---|
| 203 | /// <summary>
|
---|
| 204 | /// Second between 0 and 59
|
---|
| 205 | /// </summary>
|
---|
| 206 | public int? Second
|
---|
| 207 | {
|
---|
| 208 | get
|
---|
| 209 | {
|
---|
| 210 | return _second;
|
---|
| 211 | }
|
---|
| 212 | set
|
---|
| 213 | {
|
---|
| 214 | if (value < 0)
|
---|
| 215 | {
|
---|
| 216 | throw new InvalidOperationException("Value for second cannot be negative");
|
---|
| 217 | }
|
---|
| 218 | if (value > 59)
|
---|
| 219 | {
|
---|
| 220 | throw new InvalidOperationException("Value for second cannot be greater than 59");
|
---|
| 221 | }
|
---|
| 222 | _second = value;
|
---|
| 223 | OnTimeChanged();
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | private decimal Round(decimal value)
|
---|
| 228 | {
|
---|
| 229 | return Math.Round(value, NumberOfDecimals);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | private decimal ToSeconds()
|
---|
| 233 | {
|
---|
| 234 | var result = Hour * SecondsPerHour;
|
---|
| 235 | result += Minute * SecondsPerMinute;
|
---|
| 236 | result += Second ?? 0;
|
---|
| 237 | return (decimal)result;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /// <summary>
|
---|
| 241 | /// Returns the excel decimal representation of a time.
|
---|
| 242 | /// </summary>
|
---|
| 243 | /// <returns></returns>
|
---|
| 244 | public decimal ToExcelTime()
|
---|
| 245 | {
|
---|
| 246 | var seconds = ToSeconds();
|
---|
| 247 | return Round(seconds / (decimal)SecondsPerDay);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /// <summary>
|
---|
| 251 | /// Returns the excel decimal representation of a time as a string.
|
---|
| 252 | /// </summary>
|
---|
| 253 | /// <returns></returns>
|
---|
| 254 | public string ToExcelString()
|
---|
| 255 | {
|
---|
| 256 | return ToExcelTime().ToString(CultureInfo.InvariantCulture);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | public override string ToString()
|
---|
| 260 | {
|
---|
| 261 | var second = Second ?? 0;
|
---|
| 262 | return string.Format("{0}:{1}:{2}",
|
---|
| 263 | Hour < 10 ? "0" + Hour.ToString() : Hour.ToString(),
|
---|
| 264 | Minute < 10 ? "0" + Minute.ToString() : Minute.ToString(),
|
---|
| 265 | second < 10 ? "0" + second.ToString() : second.ToString());
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | }
|
---|
| 269 | }
|
---|