1 module hunt.database.driver.postgresql.data.Interval; 2 3 import std.conv; 4 5 /** 6 * Postgres Interval is date and time based 7 * such as 120 years 3 months 332 days 20 hours 20 minutes 20.999999 seconds 8 * 9 * @author <a href="mailto:emad.albloushi@gmail.com">Emad Alblueshi</a> 10 */ 11 12 class Interval { 13 14 private int _years, _months, _days, _hours, _minutes, _seconds, _microseconds; 15 16 this() { 17 this(0, 0, 0, 0, 0, 0, 0); 18 } 19 20 this(int years, int months, int days, int hours, int minutes, int seconds, int microseconds) { 21 this._years = years; 22 this._months = months; 23 this._days = days; 24 this._hours = hours; 25 this._minutes = minutes; 26 this._seconds = seconds; 27 this._microseconds = microseconds; 28 } 29 30 this(int years, int months, int days, int hours, int minutes, int seconds) { 31 this(years, months, days, hours, minutes, seconds, 0); 32 } 33 34 this(int years, int months, int days, int hours, int minutes) { 35 this(years, months, days, hours, minutes, 0); 36 } 37 38 this(int years, int months, int days, int hours) { 39 this(years, months, days, hours, 0); 40 } 41 42 this(int years, int months, int days) { 43 this(years, months, days, 0); 44 } 45 46 this(int years, int months) { 47 this(years, months, 0); 48 } 49 50 this(int years) { 51 this(years, 0); 52 } 53 54 // this(JsonObject json) { 55 // IntervalConverter.fromJson(json, this); 56 // } 57 58 static Interval of() { 59 return new Interval(); 60 } 61 62 static Interval of(int years, int months, int days, int hours, int minutes, int seconds, int microseconds) { 63 return new Interval(years, months, days, hours, minutes, seconds, microseconds); 64 } 65 66 static Interval of(int years, int months, int days, int hours, int minutes, int seconds) { 67 return new Interval(years, months, days, hours, minutes, seconds); 68 } 69 70 static Interval of(int years, int months, int days, int hours, int minutes) { 71 return new Interval(years, months, days, hours, minutes); 72 } 73 74 static Interval of(int years, int months, int days, int hours) { 75 return new Interval(years, months, days, hours); 76 } 77 78 static Interval of(int years, int months, int days) { 79 return new Interval(years, months, days); 80 } 81 82 static Interval of(int years, int months) { 83 return new Interval(years, months); 84 } 85 86 static Interval of(int years) { 87 return new Interval(years); 88 } 89 90 Interval years(int years) { 91 this._years = years; 92 return this; 93 } 94 95 Interval months(int months) { 96 this._months = months; 97 return this; 98 } 99 100 Interval days(int days) { 101 this._days = days; 102 return this; 103 } 104 105 Interval hours(int hours) { 106 this._hours = hours; 107 return this; 108 } 109 110 Interval minutes(int minutes) { 111 this._minutes = minutes; 112 return this; 113 } 114 115 Interval seconds(int seconds) { 116 this._seconds = seconds; 117 return this; 118 } 119 120 Interval microseconds(int microseconds) { 121 this._microseconds = microseconds; 122 return this; 123 } 124 125 int getYears() { 126 return _years; 127 } 128 129 void setYears(int years) { 130 this._years = years; 131 } 132 133 int getMonths() { 134 return _months; 135 } 136 137 void setMonths(int months) { 138 this._months = months; 139 } 140 141 int getDays() { 142 return _days; 143 } 144 145 void setDays(int days) { 146 this._days = days; 147 } 148 149 int getHours() { 150 return _hours; 151 } 152 153 void setHours(int hours) { 154 this._hours = hours; 155 } 156 157 int getMinutes() { 158 return _minutes; 159 } 160 161 void setMinutes(int minutes) { 162 this._minutes = minutes; 163 } 164 165 int getSeconds() { 166 return _seconds; 167 } 168 169 void setSeconds(int seconds) { 170 this._seconds = seconds; 171 } 172 173 int getMicroseconds() { 174 return _microseconds; 175 } 176 177 void setMicroseconds(int microseconds) { 178 this._microseconds = microseconds; 179 } 180 181 override 182 bool opEquals(Object o) { 183 if (this is o) return true; 184 Interval interval = cast(Interval) o; 185 if(interval is null) return false; 186 187 return _years == interval._years && 188 _months == interval._months && 189 _days == interval._days && 190 _hours == interval._hours && 191 _minutes == interval._minutes && 192 _seconds == interval._seconds && 193 _microseconds == interval._microseconds; 194 } 195 196 override 197 size_t toHash() @trusted nothrow { 198 size_t result = _years; 199 result = 31 * result + _months; 200 result = 31 * result + _days; 201 result = 31 * result + _hours; 202 result = 31 * result + _minutes; 203 result = 31 * result + _seconds; 204 result = 31 * result + _microseconds; 205 return result; 206 } 207 208 override 209 string toString() { 210 import std.format; 211 import std.math : abs; 212 string r = format("Interval( %d years %d months %d days %d hours %d minutes %d%s seconds )", 213 _years, _months, _days, _hours, _minutes, _seconds, 214 (_microseconds == 0 ? "" : "." ~ abs(_microseconds).to!string())); 215 return r; 216 } 217 218 // JsonObject toJson() { 219 // JsonObject json = new JsonObject(); 220 // IntervalConverter.toJson(this, json); 221 // return json; 222 // } 223 }