DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlDayToSecond.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 
19 namespace Deveel.Data.Sql.Objects {
20  public struct SqlDayToSecond : ISqlObject, IComparable<SqlDayToSecond>, IEquatable<SqlDayToSecond> {
21  private readonly TimeSpan? value;
22 
23  public static readonly SqlDayToSecond Null = new SqlDayToSecond(true);
24  public static readonly SqlDayToSecond Zero = new SqlDayToSecond(0, 0, 0, 0, 0);
25 
26  private SqlDayToSecond(bool isNull) {
27  value = null;
28  }
29 
30  public SqlDayToSecond(int hours, int minutes, int seconds)
31  : this(0, hours, minutes, seconds) {
32  }
33 
34  public SqlDayToSecond(int days, int hours, int minutes, int seconds)
35  : this(days, hours, minutes, seconds, 0) {
36  }
37 
38  public SqlDayToSecond(int days, int hours, int minutes, int seconds, int milliseconds) {
39  value = new TimeSpan(days, hours, minutes, seconds, milliseconds);
40  }
41 
42  int IComparable.CompareTo(object obj) {
43  return CompareTo((SqlDayToSecond) obj);
44  }
45 
46  int IComparable<ISqlObject>.CompareTo(ISqlObject other) {
47  return CompareTo((SqlDayToSecond) other);
48  }
49 
50  public bool IsNull {
51  get { return value == null; }
52  }
53 
54  private void AssertNotNull() {
55  if (value == null)
56  throw new NullReferenceException();
57  }
58 
59  public double TotalMilliseconds {
60  get {
61  AssertNotNull();
62  return value.Value.TotalMilliseconds;
63  }
64  }
65 
66  public int Days {
67  get {
68  AssertNotNull();
69  return value.Value.Days;
70  }
71  }
72 
73  public int Hours {
74  get {
75  AssertNotNull();
76  return value.Value.Hours;
77  }
78  }
79 
80  public int Minutes {
81  get {
82  AssertNotNull();
83  return value.Value.Minutes;
84  }
85  }
86 
87  public int Seconds {
88  get {
89  AssertNotNull();
90  return value.Value.Seconds;
91  }
92  }
93 
94  public int Milliseconds {
95  get {
96  AssertNotNull();
97  return value.Value.Milliseconds;
98  }
99  }
100 
102  return other is SqlDayToSecond;
103  }
104 
105  public int CompareTo(SqlDayToSecond other) {
106  if (IsNull && other.IsNull)
107  return 0;
108  if (!IsNull && other.IsNull)
109  return -1;
110  if (IsNull && !other.IsNull)
111  return 1;
112 
113  return value.Value.CompareTo(other.value.Value);
114  }
115 
116  public SqlDayToSecond Add(SqlDayToSecond interval) {
117  if (IsNull)
118  return interval;
119  if (interval.IsNull)
120  return this;
121 
122  var ts = new TimeSpan(interval.Days, interval.Hours, interval.Minutes, interval.Seconds, interval.Milliseconds);
123  var result = value.Value.Add(ts);
124  return new SqlDayToSecond(result.Days, result.Hours, result.Minutes, result.Seconds, result.Milliseconds);
125  }
126 
128  if (IsNull)
129  return interval;
130  if (interval.IsNull)
131  return this;
132 
133  var ts = new TimeSpan(interval.Days, interval.Hours, interval.Minutes, interval.Seconds, interval.Milliseconds);
134  var result = value.Value.Subtract(ts);
135  return new SqlDayToSecond(result.Days, result.Hours, result.Minutes, result.Seconds, result.Milliseconds);
136  }
137 
138  public byte[] ToByArray() {
139  throw new NotImplementedException();
140  }
141 
142  public bool Equals(SqlDayToSecond other) {
143  if (IsNull && other.IsNull)
144  return true;
145 
146  return value.Equals(other.value);
147  }
148 
149  public override bool Equals(object obj) {
150  return Equals((SqlDayToSecond) obj);
151  }
152 
153  public override int GetHashCode() {
154  return value == null ? 0 : value.Value.GetHashCode();
155  }
156 
157  public static SqlDayToSecond operator +(SqlDayToSecond a, SqlDayToSecond b) {
158  return a.Add(b);
159  }
160 
161  public static SqlDayToSecond operator -(SqlDayToSecond a, SqlDayToSecond b) {
162  return a.Subtract(b);
163  }
164 
165  public static bool operator ==(SqlDayToSecond a, SqlDayToSecond b) {
166  return a.Equals(b);
167  }
168 
169  public static bool operator !=(SqlDayToSecond a, SqlDayToSecond b) {
170  return !(a == b);
171  }
172  }
173 }
SqlDayToSecond(int days, int hours, int minutes, int seconds)
int CompareTo(SqlDayToSecond other)
SqlDayToSecond(int days, int hours, int minutes, int seconds, int milliseconds)
SqlDayToSecond Add(SqlDayToSecond interval)
SqlDayToSecond Subtract(SqlDayToSecond interval)
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
bool Equals(SqlDayToSecond other)
override bool Equals(object obj)
SqlDayToSecond(int hours, int minutes, int seconds)