1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2019 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using GDModel.Providers.GEDCOM;
23 
24 namespace GDModel
25 {
26     public sealed class GDMTime : GDMTag
27     {
28         private byte fHour;
29         private byte fMinutes;
30         private byte fSeconds;
31         private short fFraction;
32 
33 
34         public byte Hour
35         {
36             get { return fHour; }
37             set { fHour = value; }
38         }
39 
40         public byte Minutes
41         {
42             get { return fMinutes; }
43             set { fMinutes = value; }
44         }
45 
46         public byte Seconds
47         {
48             get { return fSeconds; }
49             set { fSeconds = value; }
50         }
51 
52         public short Fraction
53         {
54             get { return fFraction; }
55             set { fFraction = value; }
56         }
57 
58         public TimeSpan Value
59         {
60             get {
61                 return new TimeSpan(0, fHour, fMinutes, fSeconds, (int)(100u * fFraction));
62             }
63             set {
64                 fHour = (byte)value.Hours;
65                 fMinutes = (byte)value.Minutes;
66                 fSeconds = (byte)value.Seconds;
67                 fFraction = (short)Math.Truncate(value.Milliseconds / 100.0);
68             }
69         }
70 
71 
GDMTime()72         public GDMTime()
73         {
74             SetName(GEDCOMTagType.TIME);
75         }
76 
GetStringValue()77         protected override string GetStringValue()
78         {
79             string result;
80             if (fHour == 0 && fMinutes == 0 && fSeconds == 0) {
81                 result = "";
82             } else {
83                 result = string.Format("{0:00}:{1:00}:{2:00}", new object[] { fHour, fMinutes, fSeconds });
84 
85                 if (fFraction > 0) {
86                     result = result + "." + fFraction.ToString();
87                 }
88             }
89             return result;
90         }
91 
Clear()92         public override void Clear()
93         {
94             base.Clear();
95             fHour = 0;
96             fMinutes = 0;
97             fSeconds = 0;
98             fFraction = 0;
99         }
100 
IsEmpty()101         public override bool IsEmpty()
102         {
103             return base.IsEmpty() && fHour == 0 && fMinutes == 0 && fSeconds == 0;
104         }
105 
ParseString(string strValue)106         public override string ParseString(string strValue)
107         {
108             return GEDCOMUtils.ParseTime(strValue, this);
109         }
110 
SetRawData(byte hour, byte minutes, byte seconds, short fraction)111         internal void SetRawData(byte hour, byte minutes, byte seconds, short fraction)
112         {
113             fHour = hour;
114             fMinutes = minutes;
115             fSeconds = seconds;
116             fFraction = fraction;
117         }
118     }
119 }
120