1 /*
2  * hunt-time: A time library for D programming language.
3  *
4  * Copyright (C) 2015-2018 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.time.Instant;
13 
14 import hunt.time.LocalTime;
15 import hunt.time.temporal.ChronoField;
16 import hunt.time.temporal.ChronoUnit;
17 import hunt.util.Comparator;
18 import hunt.stream.DataInput;
19 import hunt.stream.DataOutput;
20 import hunt.Exceptions;
21 import hunt.Long;
22 import hunt.math.Helper;
23 import hunt.util.Common;
24 import hunt.time.Exceptions;
25 import hunt.stream.Common;
26 // import hunt.time.format.DateTimeFormatter;
27 import hunt.time.format.DateTimeParseException;
28 import hunt.time.temporal.ChronoField;
29 import hunt.time.temporal.ChronoUnit;
30 import hunt.time.temporal.Temporal;
31 import hunt.time.temporal.TemporalAccessor;
32 import hunt.time.temporal.TemporalAdjuster;
33 import hunt.time.temporal.TemporalAmount;
34 import hunt.time.temporal.TemporalField;
35 import hunt.time.temporal.TemporalQueries;
36 import hunt.time.temporal.TemporalQuery;
37 import hunt.time.temporal.TemporalUnit;
38 import hunt.time.Exceptions;
39 import hunt.time.temporal.ValueRange;
40 import hunt.time.OffsetDateTime;
41 // import hunt.time.Duration;
42 import hunt.time.Ser;
43 import hunt.time.Year;
44 import hunt.time.Exceptions;
45 import std.math;
46 /**
47  * An instantaneous point on the time-line.
48  * !(p)
49  * This class models a single instantaneous point on the time-line.
50  * This might be used to record event time-stamps _in the application.
51  * !(p)
52  * The range of an instant requires the storage of a number larger than a {@code long}.
53  * To achieve this, the class stores a {@code long} representing epoch-seconds and an
54  * {@code int} representing nanosecond-of-second, which will always be between 0 and 999,999,999.
55  * The epoch-seconds are measured from the standard Java epoch of {@code 1970-01-01T00:00:00Z}
56  * where instants after the epoch have positive values, and earlier instants have negative values.
57  * For both the epoch-second and nanosecond parts, a larger value is always later on the time-line
58  * than a smaller value.
59  *
60  * !(h3)Time-scale</h3>
61  * !(p)
62  * The length of the solar day is the standard way that humans measure time.
63  * This has traditionally been subdivided into 24 hours of 60 minutes of 60 seconds,
64  * forming a 86400 second day.
65  * !(p)
66  * Modern timekeeping is based on atomic clocks which precisely define an SI second
67  * relative to the transitions of a Caesium atom. The length of an SI second was defined
68  * to be very close to the 86400th fraction of a day.
69  * !(p)
70  * Unfortunately, as the Earth rotates the length of the day varies.
71  * In addition, over time the average length of the day is getting longer as the Earth slows.
72  * As a result, the length of a solar day _in 2012 is slightly longer than 86400 SI seconds.
73  * The actual length of any given day and the amount by which the Earth is slowing
74  * are not predictable and can only be determined by measurement.
75  * The UT1 time-scale captures the accurate length of day, but is only available some
76  * time after the day has completed.
77  * !(p)
78  * The UTC time-scale is a standard approach to bundle up all the additional fractions
79  * of a second from UT1 into whole seconds, known as !(i)leap-seconds</i>.
80  * A leap-second may be added or removed depending on the Earth's rotational changes.
81  * As such, UTC permits a day to have 86399 SI seconds or 86401 SI seconds where
82  * necessary _in order to keep the day aligned with the Sun.
83  * !(p)
84  * The modern UTC time-scale was introduced _in 1972, introducing the concept of whole leap-seconds.
85  * Between 1958 and 1972, the definition of UTC was complex, with minor sub-second leaps and
86  * alterations to the length of the notional second. As of 2012, discussions are underway
87  * to change the definition of UTC again, with the potential to remove leap seconds or
88  * introduce other changes.
89  * !(p)
90  * Given the complexity of accurate timekeeping described above, this Java API defines
91  * its own time-scale, the !(i)Java Time-Scale</i>.
92  * !(p)
93  * The Java Time-Scale divides each calendar day into exactly 86400
94  * subdivisions, known as seconds.  These seconds may differ from the
95  * SI second.  It closely matches the de facto international civil time
96  * scale, the definition of which changes from time to time.
97  * !(p)
98  * The Java Time-Scale has slightly different definitions for different
99  * segments of the time-line, each based on the consensus international
100  * time scale that is used as the basis for civil time. Whenever the
101  * internationally-agreed time scale is modified or replaced, a new
102  * segment of the Java Time-Scale must be defined for it.  Each segment
103  * must meet these requirements:
104  * !(ul)
105  * !(li)the Java Time-Scale shall closely match the underlying international
106  *  civil time scale;</li>
107  * !(li)the Java Time-Scale shall exactly match the international civil
108  *  time scale at noon each day;</li>
109  * !(li)the Java Time-Scale shall have a precisely-defined relationship to
110  *  the international civil time scale.</li>
111  * </ul>
112  * There are currently, as of 2013, two segments _in the Java time-scale.
113  * !(p)
114  * For the segment from 1972-11-03 (exact boundary discussed below) until
115  * further notice, the consensus international time scale is UTC (with
116  * leap seconds).  In this segment, the Java Time-Scale is identical to
117  * <a href="http://www.cl.cam.ac.uk/~mgk25/time/utc-sls/">UTC-SLS</a>.
118  * This is identical to UTC on days that do not have a leap second.
119  * On days that do have a leap second, the leap second is spread equally
120  * over the last 1000 seconds of the day, maintaining the appearance of
121  * exactly 86400 seconds per day.
122  * !(p)
123  * For the segment prior to 1972-11-03, extending back arbitrarily far,
124  * the consensus international time scale is defined to be UT1, applied
125  * proleptically, which is equivalent to the (mean) solar time on the
126  * prime meridian (Greenwich). In this segment, the Java Time-Scale is
127  * identical to the consensus international time scale. The exact
128  * boundary between the two segments is the instant where UT1 = UTC
129  * between 1972-11-03T00:00 and 1972-11-04T12:00.
130  * !(p)
131  * Implementations of the Java time-scale using the JSR-310 API are not
132  * required to provide any clock that is sub-second accurate, or that
133  * progresses monotonically or smoothly. Implementations are therefore
134  * not required to actually perform the UTC-SLS slew or to otherwise be
135  * aware of leap seconds. JSR-310 does, however, require that
136  * implementations must document the approach they use when defining a
137  * clock representing the current instant.
138  * See {@link Clock} for details on the available clocks.
139  * !(p)
140  * The Java time-scale is used for all date-time classes.
141  * This includes {@code Instant}, {@code LocalDate}, {@code LocalTime}, {@code OffsetDateTime},
142  * {@code ZonedDateTime} and {@code Duration}.
143  *
144  * !(p)
145  * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
146  * class; use of identity-sensitive operations (including reference equality
147  * ({@code ==}), identity hash code, or synchronization) on instances of
148  * {@code Instant} may have unpredictable results and should be avoided.
149  * The {@code equals} method should be used for comparisons.
150  *
151  * @implSpec
152  * This class is immutable and thread-safe.
153  *
154  * @since 1.8
155  */
156 
157 import hunt.Functions;
158 import hunt.time.Clock;
159 import hunt.time.ZoneOffset;
160 import hunt.time.ZonedDateTime;
161 import hunt.time.ZoneId;
162 import hunt.time.util.Common;
163 
164 import std.concurrency : initOnce;
165 
166 final class Instant
167         : Temporal, TemporalAdjuster, Comparable!(Instant) { // , Serializable
168 
169     /**
170      * The minimum supported epoch second.
171      */
172      enum long MIN_SECOND = -31557014167219200L;
173 
174     /**
175      * The maximum supported epoch second.
176      */
177      enum long MAX_SECOND = 31556889864403199L;
178 
179     /**
180      * Constant for the 1970-01-01T00:00:00Z epoch instant.
181      */
182     static Instant EPOCH() {
183         __gshared Instant _EPOCH;
184         return initOnce!(_EPOCH)(new Instant(0, 0));
185     }
186 
187     /**
188      * The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
189      * This could be used by an application as a "far past" instant.
190      * !(p)
191      * This is one year earlier than the minimum {@code LocalDateTime}.
192      * This provides sufficient values to handle the range of {@code ZoneOffset}
193      * which affect the instant _in addition to the local date-time.
194      * The value is also chosen such that the value of the year fits _in
195      * an {@code int}.
196      */
197     static Instant MIN() {
198         __gshared Instant _MIN;
199         return initOnce!(_MIN)(Instant.ofEpochSecond(MIN_SECOND, 0));
200     }    
201     
202     /**
203      * The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
204      * This could be used by an application as a "far future" instant.
205      * !(p)
206      * This is one year later than the maximum {@code LocalDateTime}.
207      * This provides sufficient values to handle the range of {@code ZoneOffset}
208      * which affect the instant _in addition to the local date-time.
209      * The value is also chosen such that the value of the year fits _in
210      * an {@code int}.
211      */
212     static Instant MAX() {
213         __gshared Instant _MAX;
214         return initOnce!(_MAX)(Instant.ofEpochSecond(MAX_SECOND, 999_999_999));
215     }    
216 
217     /**
218      * The number of seconds from the epoch of 1970-01-01T00:00:00Z.
219      */
220     private  long seconds;
221     /**
222      * The number of nanoseconds, later along the time-line, from the seconds field.
223      * This is always positive, and never exceeds 999,999,999.
224      */
225     private  int nanos;
226 
227 
228     //-----------------------------------------------------------------------
229     /**
230      * Obtains the current instant from the system clock.
231      * !(p)
232      * This will query the {@link Clock#systemUTC() system UTC clock} to
233      * obtain the current instant.
234      * !(p)
235      * Using this method will prevent the ability to use an alternate time-source for
236      * testing because the clock is effectively hard-coded.
237      *
238      * @return the current instant using the system clock, not null
239      */
240     static Instant now() {
241         return Clock.systemUTC().instant();
242     }
243 
244     /**
245      * Obtains the current instant from the specified clock.
246      * !(p)
247      * This will query the specified clock to obtain the current time.
248      * !(p)
249      * Using this method allows the use of an alternate clock for testing.
250      * The alternate clock may be introduced using {@link Clock dependency injection}.
251      *
252      * @param clock  the clock to use, not null
253      * @return the current instant, not null
254      */
255     static Instant now(Clock clock) {
256         assert(clock, "clock");
257         return clock.instant();
258     }
259 
260     //-----------------------------------------------------------------------
261     /**
262      * Obtains an instance of {@code Instant} using seconds from the
263      * epoch of 1970-01-01T00:00:00Z.
264      * !(p)
265      * The nanosecond field is set to zero.
266      *
267      * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
268      * @return an instant, not null
269      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
270      */
271     static Instant ofEpochSecond(long epochSecond) {
272         return create(epochSecond, 0);
273     }
274 
275     /**
276      * Obtains an instance of {@code Instant} using seconds from the
277      * epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
278      * !(p)
279      * This method allows an arbitrary number of nanoseconds to be passed _in.
280      * The factory will alter the values of the second and nanosecond _in order
281      * to ensure that the stored nanosecond is _in the range 0 to 999,999,999.
282      * For example, the following will result _in exactly the same instant:
283      * !(pre)
284      *  Instant.ofEpochSecond(3, 1);
285      *  Instant.ofEpochSecond(4, -999_999_999);
286      *  Instant.ofEpochSecond(2, 1000_000_001);
287      * </pre>
288      *
289      * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
290      * @param nanoAdjustment  the nanosecond adjustment to the number of seconds, positive or negative
291      * @return an instant, not null
292      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
293      * @throws ArithmeticException if numeric overflow occurs
294      */
295     static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
296         long secs = MathHelper.addExact(epochSecond , MathHelper.floorDiv(nanoAdjustment , LocalTime.NANOS_PER_SECOND));
297         int nos = cast(int)(MathHelper.floorMod(nanoAdjustment , LocalTime.NANOS_PER_SECOND));
298         return create(secs, nos);
299     }
300 
301     /**
302      * Obtains an instance of {@code Instant} using milliseconds from the
303      * epoch of 1970-01-01T00:00:00Z.
304      * !(p)
305      * The seconds and nanoseconds are extracted from the specified milliseconds.
306      *
307      * @param epochMilli  the number of milliseconds from 1970-01-01T00:00:00Z
308      * @return an instant, not null
309      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
310      */
311     static Instant ofEpochMilli(long epochMilli) {
312         long secs = MathHelper.floorDiv(epochMilli , 1000);
313         int mos = MathHelper.floorMod(epochMilli , 1000);
314         return create(secs, mos * 1000_000);
315     }
316 
317     //-----------------------------------------------------------------------
318     /**
319      * Obtains an instance of {@code Instant} from a temporal object.
320      * !(p)
321      * This obtains an instant based on the specified temporal.
322      * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
323      * which this factory converts to an instance of {@code Instant}.
324      * !(p)
325      * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
326      * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
327      * !(p)
328      * This method matches the signature of the functional interface {@link TemporalQuery}
329      * allowing it to be used as a query via method reference, {@code Instant::from}.
330      *
331      * @param temporal  the temporal object to convert, not null
332      * @return the instant, not null
333      * @throws DateTimeException if unable to convert to an {@code Instant}
334      */
335     static Instant from(TemporalAccessor temporal) {
336         if (cast(Instant)(temporal) !is null) {
337             return cast(Instant) temporal;
338         }
339         assert(temporal, "temporal");
340         try {
341             long instantSecs = temporal.getLong(ChronoField.INSTANT_SECONDS);
342             int nanoOfSecond = temporal.get(ChronoField.NANO_OF_SECOND);
343             return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
344         } catch (DateTimeException ex) {
345             throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " ~
346                     typeid(temporal).stringof ~ " of type " ~ typeid(temporal).stringof, ex);
347         }
348     }
349 
350     //-----------------------------------------------------------------------
351     /**
352      * Obtains an instance of {@code Instant} from a text string such as
353      * {@code 2007-12-03T10:15:30.00Z}.
354      * !(p)
355      * The string must represent a valid instant _in UTC and is parsed using
356      * {@link DateTimeFormatter#ISO_INSTANT}.
357      *
358      * @param text  the text to parse, not null
359      * @return the parsed instant, not null
360      * @throws DateTimeParseException if the text cannot be parsed
361      */
362     // static Instant parse(const string text) {
363     //     return DateTimeFormatter.ISO_INSTANT.parse!Instant(text, new class TemporalQuery!Instant{
364     //          Instant queryFrom(TemporalAccessor temporal)
365     //          {
366     //                  if (cast(Instant)(temporal) !is null) {
367     //                     return cast(Instant) temporal;
368     //                 }
369     //                 assert(temporal, "temporal");
370     //                 try {
371     //                     long instantSecs = temporal.getLong(ChronoField.INSTANT_SECONDS);
372     //                     int nanoOfSecond = temporal.get(ChronoField.NANO_OF_SECOND);
373     //                     return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
374     //                 } catch (DateTimeException ex) {
375     //                     throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " ~
376     //                             typeid(temporal).stringof ~ " of type " ~ typeid(temporal).stringof, ex);
377     //                 }
378     //          }
379     //     });
380     // }
381 
382     //-----------------------------------------------------------------------
383     /**
384      * Obtains an instance of {@code Instant} using seconds and nanoseconds.
385      *
386      * @param seconds  the length of the duration _in seconds
387      * @param nanoOfSecond  the nano-of-second, from 0 to 999,999,999
388      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
389      */
390     private static Instant create(long seconds, int nanoOfSecond) {
391         if ((seconds | nanoOfSecond) == 0) {
392             return EPOCH;
393         }
394         if (seconds < MIN_SECOND || seconds > MAX_SECOND) {
395             throw new DateTimeException("Instant exceeds minimum or maximum instant");
396         }
397         return new Instant(seconds, nanoOfSecond);
398     }
399 
400     /**
401      * Constructs an instance of {@code Instant} using seconds from the epoch of
402      * 1970-01-01T00:00:00Z and nanosecond fraction of second.
403      *
404      * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
405      * @param nanos  the nanoseconds within the second, must be positive
406      */
407     this(long epochSecond, int nanos = 0) {
408         // super();
409         this.seconds = epochSecond;
410         this.nanos = nanos;
411     }
412 
413     //-----------------------------------------------------------------------
414     /**
415      * Checks if the specified field is supported.
416      * !(p)
417      * This checks if this instant can be queried for the specified field.
418      * If false, then calling the {@link #range(TemporalField) range},
419      * {@link #get(TemporalField) get} and {@link #_with(TemporalField, long)}
420      * methods will throw an exception.
421      * !(p)
422      * If the field is a {@link ChronoField} then the query is implemented here.
423      * The supported fields are:
424      * !(ul)
425      * !(li){@code NANO_OF_SECOND}
426      * !(li){@code MICRO_OF_SECOND}
427      * !(li){@code MILLI_OF_SECOND}
428      * !(li){@code INSTANT_SECONDS}
429      * </ul>
430      * All other {@code ChronoField} instances will return false.
431      * !(p)
432      * If the field is not a {@code ChronoField}, then the result of this method
433      * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
434      * passing {@code this} as the argument.
435      * Whether the field is supported is determined by the field.
436      *
437      * @param field  the field to check, null returns false
438      * @return true if the field is supported on this instant, false if not
439      */
440     override
441     bool isSupported(TemporalField field) {
442         if (cast(ChronoField)(field) !is null) {
443             return field == ChronoField.INSTANT_SECONDS || field == ChronoField.NANO_OF_SECOND || field == ChronoField.MICRO_OF_SECOND || field == ChronoField.MILLI_OF_SECOND;
444         }
445         return field !is null && field.isSupportedBy(this);
446     }
447 
448     /**
449      * Checks if the specified unit is supported.
450      * !(p)
451      * This checks if the specified unit can be added to, or subtracted from, this date-time.
452      * If false, then calling the {@link #plus(long, TemporalUnit)} and
453      * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
454      * !(p)
455      * If the unit is a {@link ChronoUnit} then the query is implemented here.
456      * The supported units are:
457      * !(ul)
458      * !(li){@code NANOS}
459      * !(li){@code MICROS}
460      * !(li){@code MILLIS}
461      * !(li){@code SECONDS}
462      * !(li){@code MINUTES}
463      * !(li){@code HOURS}
464      * !(li){@code HALF_DAYS}
465      * !(li){@code DAYS}
466      * </ul>
467      * All other {@code ChronoUnit} instances will return false.
468      * !(p)
469      * If the unit is not a {@code ChronoUnit}, then the result of this method
470      * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
471      * passing {@code this} as the argument.
472      * Whether the unit is supported is determined by the unit.
473      *
474      * @param unit  the unit to check, null returns false
475      * @return true if the unit can be added/subtracted, false if not
476      */
477     override
478     bool isSupported(TemporalUnit unit) {
479         if (cast(ChronoUnit)(unit) !is null) {
480             return unit.isTimeBased() || unit == ChronoUnit.DAYS;
481         }
482         return unit !is null && unit.isSupportedBy(this);
483     }
484 
485     //-----------------------------------------------------------------------
486     /**
487      * Gets the range of valid values for the specified field.
488      * !(p)
489      * The range object expresses the minimum and maximum valid values for a field.
490      * This instant is used to enhance the accuracy of the returned range.
491      * If it is not possible to return the range, because the field is not supported
492      * or for some other reason, an exception is thrown.
493      * !(p)
494      * If the field is a {@link ChronoField} then the query is implemented here.
495      * The {@link #isSupported(TemporalField) supported fields} will return
496      * appropriate range instances.
497      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
498      * !(p)
499      * If the field is not a {@code ChronoField}, then the result of this method
500      * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
501      * passing {@code this} as the argument.
502      * Whether the range can be obtained is determined by the field.
503      *
504      * @param field  the field to query the range for, not null
505      * @return the range of valid values for the field, not null
506      * @throws DateTimeException if the range for the field cannot be obtained
507      * @throws UnsupportedTemporalTypeException if the field is not supported
508      */
509     override  // override for Javadoc
510     ValueRange range(TemporalField field) {
511         return /* Temporal. super.*/super_range(field);
512     }
513 
514       ValueRange super_range(TemporalField field) {
515         if (cast(ChronoField)(field) !is null) {
516             if (isSupported(field)) {
517                 return field.range();
518             }
519             throw new UnsupportedTemporalTypeException("Unsupported field: " ~ typeid(field).stringof);
520         }
521         assert(field, "field");
522         return field.rangeRefinedBy(this);
523     }
524     /**
525      * Gets the value of the specified field from this instant as an {@code int}.
526      * !(p)
527      * This queries this instant for the value of the specified field.
528      * The returned value will always be within the valid range of values for the field.
529      * If it is not possible to return the value, because the field is not supported
530      * or for some other reason, an exception is thrown.
531      * !(p)
532      * If the field is a {@link ChronoField} then the query is implemented here.
533      * The {@link #isSupported(TemporalField) supported fields} will return valid
534      * values based on this date-time, except {@code INSTANT_SECONDS} which is too
535      * large to fit _in an {@code int} and throws a {@code DateTimeException}.
536      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
537      * !(p)
538      * If the field is not a {@code ChronoField}, then the result of this method
539      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
540      * passing {@code this} as the argument. Whether the value can be obtained,
541      * and what the value represents, is determined by the field.
542      *
543      * @param field  the field to get, not null
544      * @return the value for the field
545      * @throws DateTimeException if a value for the field cannot be obtained or
546      *         the value is outside the range of valid values for the field
547      * @throws UnsupportedTemporalTypeException if the field is not supported or
548      *         the range of values exceeds an {@code int}
549      * @throws ArithmeticException if numeric overflow occurs
550      */
551     override  // override for Javadoc and performance
552     int get(TemporalField field) {
553         if (cast(ChronoField)(field) !is null) {
554             auto name = (cast(ChronoField) field).toString;
555              {
556                 if(name == ChronoField.NANO_OF_SECOND.toString) return nanos;
557                 if(name == ChronoField.MICRO_OF_SECOND.toString) return nanos / 1000;
558                 if(name == ChronoField.MILLI_OF_SECOND.toString) return nanos / 1000_000;
559             }
560             throw new UnsupportedTemporalTypeException("Unsupported field: " ~ typeid(field).stringof);
561         }
562         return range(field).checkValidIntValue(field.getFrom(this), field);
563     }
564 
565     /**
566      * Gets the value of the specified field from this instant as a {@code long}.
567      * !(p)
568      * This queries this instant for the value of the specified field.
569      * If it is not possible to return the value, because the field is not supported
570      * or for some other reason, an exception is thrown.
571      * !(p)
572      * If the field is a {@link ChronoField} then the query is implemented here.
573      * The {@link #isSupported(TemporalField) supported fields} will return valid
574      * values based on this date-time.
575      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
576      * !(p)
577      * If the field is not a {@code ChronoField}, then the result of this method
578      * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
579      * passing {@code this} as the argument. Whether the value can be obtained,
580      * and what the value represents, is determined by the field.
581      *
582      * @param field  the field to get, not null
583      * @return the value for the field
584      * @throws DateTimeException if a value for the field cannot be obtained
585      * @throws UnsupportedTemporalTypeException if the field is not supported
586      * @throws ArithmeticException if numeric overflow occurs
587      */
588     override
589     long getLong(TemporalField field) {
590         if (cast(ChronoField)(field) !is null) {
591             auto name = (cast(ChronoField) field).toString;
592             {
593                 if(name == ChronoField.NANO_OF_SECOND.toString) return nanos;
594                 if(name == ChronoField.MICRO_OF_SECOND.toString) return nanos / 1000;
595                 if(name == ChronoField.MILLI_OF_SECOND.toString) return nanos / 1000_000;
596                 if(name == ChronoField.INSTANT_SECONDS.toString) return seconds;
597             }
598             throw new UnsupportedTemporalTypeException("Unsupported field: " ~ typeid(field).stringof);
599         }
600         return field.getFrom(this);
601     }
602 
603     //-----------------------------------------------------------------------
604     /**
605      * Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
606      * !(p)
607      * The epoch second count is a simple incrementing count of seconds where
608      * second 0 is 1970-01-01T00:00:00Z.
609      * The nanosecond part is returned by {@link #getNano}.
610      *
611      * @return the seconds from the epoch of 1970-01-01T00:00:00Z
612      */
613     long getEpochSecond() {
614         return seconds;
615     }
616 
617     /**
618      * Gets the number of nanoseconds, later along the time-line, from the start
619      * of the second.
620      * !(p)
621      * The nanosecond-of-second value measures the total number of nanoseconds from
622      * the second returned by {@link #getEpochSecond}.
623      *
624      * @return the nanoseconds within the second, always positive, never exceeds 999,999,999
625      */
626     int getNano() {
627         return nanos;
628     }
629 
630     //-------------------------------------------------------------------------
631     /**
632      * Returns an adjusted copy of this instant.
633      * !(p)
634      * This returns an {@code Instant}, based on this one, with the instant adjusted.
635      * The adjustment takes place using the specified adjuster strategy object.
636      * Read the documentation of the adjuster to understand what adjustment will be made.
637      * !(p)
638      * The result of this method is obtained by invoking the
639      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
640      * specified adjuster passing {@code this} as the argument.
641      * !(p)
642      * This instance is immutable and unaffected by this method call.
643      *
644      * @param adjuster the adjuster to use, not null
645      * @return an {@code Instant} based on {@code this} with the adjustment made, not null
646      * @throws DateTimeException if the adjustment cannot be made
647      * @throws ArithmeticException if numeric overflow occurs
648      */
649     override
650     Instant _with(TemporalAdjuster adjuster) {
651         return cast(Instant) adjuster.adjustInto(this);
652     }
653 
654     /**
655      * Returns a copy of this instant with the specified field set to a new value.
656      * !(p)
657      * This returns an {@code Instant}, based on this one, with the value
658      * for the specified field changed.
659      * If it is not possible to set the value, because the field is not supported or for
660      * some other reason, an exception is thrown.
661      * !(p)
662      * If the field is a {@link ChronoField} then the adjustment is implemented here.
663      * The supported fields behave as follows:
664      * !(ul)
665      * !(li){@code NANO_OF_SECOND} -
666      *  Returns an {@code Instant} with the specified nano-of-second.
667      *  The epoch-second will be unchanged.
668      * !(li){@code MICRO_OF_SECOND} -
669      *  Returns an {@code Instant} with the nano-of-second replaced by the specified
670      *  micro-of-second multiplied by 1,000. The epoch-second will be unchanged.
671      * !(li){@code MILLI_OF_SECOND} -
672      *  Returns an {@code Instant} with the nano-of-second replaced by the specified
673      *  milli-of-second multiplied by 1,000,000. The epoch-second will be unchanged.
674      * !(li){@code INSTANT_SECONDS} -
675      *  Returns an {@code Instant} with the specified epoch-second.
676      *  The nano-of-second will be unchanged.
677      * </ul>
678      * !(p)
679      * In all cases, if the new value is outside the valid range of values for the field
680      * then a {@code DateTimeException} will be thrown.
681      * !(p)
682      * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
683      * !(p)
684      * If the field is not a {@code ChronoField}, then the result of this method
685      * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
686      * passing {@code this} as the argument. In this case, the field determines
687      * whether and how to adjust the instant.
688      * !(p)
689      * This instance is immutable and unaffected by this method call.
690      *
691      * @param field  the field to set _in the result, not null
692      * @param newValue  the new value of the field _in the result
693      * @return an {@code Instant} based on {@code this} with the specified field set, not null
694      * @throws DateTimeException if the field cannot be set
695      * @throws UnsupportedTemporalTypeException if the field is not supported
696      * @throws ArithmeticException if numeric overflow occurs
697      */
698     override
699     Instant _with(TemporalField field, long newValue) {
700         if (cast(ChronoField)(field) !is null) {
701             ChronoField f = cast(ChronoField) field;
702             f.checkValidValue(newValue);
703             auto name = f.toString;
704              {
705                 if(name == ChronoField.MILLI_OF_SECOND.toString) {
706                     int nval = cast(int) newValue * 1000_000;
707                     return (nval != nanos ? create(seconds, nval) : this);
708                 }
709                 if(name == ChronoField.MICRO_OF_SECOND.toString) {
710                     int nval = cast(int) newValue * 1000;
711                     return (nval != nanos ? create(seconds, nval) : this);
712                 }
713                 if(name == ChronoField.NANO_OF_SECOND.toString) return (newValue != nanos ? create(seconds, cast(int) newValue) : this);
714                 if(name == ChronoField.INSTANT_SECONDS.toString) return (newValue != seconds ? create(newValue, nanos) : this);
715             }
716             throw new UnsupportedTemporalTypeException("Unsupported field: " ~ typeid(field).stringof);
717         }
718         return cast(Instant)(field.adjustInto(this, newValue));
719     }
720 
721     //-----------------------------------------------------------------------
722     /**
723      * Returns a copy of this {@code Instant} truncated to the specified unit.
724      * !(p)
725      * Truncating the instant returns a copy of the original with fields
726      * smaller than the specified unit set to zero.
727      * The fields are calculated on the basis of using a UTC offset as seen
728      * _in {@code toString}.
729      * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
730      * round down to the nearest minute, setting the seconds and nanoseconds to zero.
731      * !(p)
732      * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
733      * that divides into the length of a standard day without remainder.
734      * This includes all supplied time units on {@link ChronoUnit} and
735      * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
736      * !(p)
737      * This instance is immutable and unaffected by this method call.
738      *
739      * @param unit  the unit to truncate to, not null
740      * @return an {@code Instant} based on this instant with the time truncated, not null
741      * @throws DateTimeException if the unit is invalid for truncation
742      * @throws UnsupportedTemporalTypeException if the unit is not supported
743      */
744     // Instant truncatedTo(TemporalUnit unit) {
745     //     if (unit == ChronoUnit.NANOS) {
746     //         return this;
747     //     }
748     //     Duration unitDur = unit.getDuration();
749     //     if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
750     //         throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
751     //     }
752     //     long dur = unitDur.toNanos();
753     //     if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
754     //         throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
755     //     }
756     //     long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
757     //     long result =/*  MathHelper.floorDiv */(nod / (dur)) * dur;
758     //     return plusNanos(result - nod);
759     // }
760 
761     //-----------------------------------------------------------------------
762     /**
763      * Returns a copy of this instant with the specified amount added.
764      * !(p)
765      * This returns an {@code Instant}, based on this one, with the specified amount added.
766      * The amount is typically {@link Duration} but may be any other type implementing
767      * the {@link TemporalAmount} interface.
768      * !(p)
769      * The calculation is delegated to the amount object by calling
770      * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
771      * to implement the addition _in any way it wishes, however it typically
772      * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
773      * of the amount implementation to determine if it can be successfully added.
774      * !(p)
775      * This instance is immutable and unaffected by this method call.
776      *
777      * @param amountToAdd  the amount to add, not null
778      * @return an {@code Instant} based on this instant with the addition made, not null
779      * @throws DateTimeException if the addition cannot be made
780      * @throws ArithmeticException if numeric overflow occurs
781      */
782     override
783     Instant plus(TemporalAmount amountToAdd) {
784         return cast(Instant) amountToAdd.addTo(this);
785     }
786 
787     /**
788      * Returns a copy of this instant with the specified amount added.
789      * !(p)
790      * This returns an {@code Instant}, based on this one, with the amount
791      * _in terms of the unit added. If it is not possible to add the amount, because the
792      * unit is not supported or for some other reason, an exception is thrown.
793      * !(p)
794      * If the field is a {@link ChronoUnit} then the addition is implemented here.
795      * The supported fields behave as follows:
796      * !(ul)
797      * !(li){@code NANOS} -
798      *  Returns an {@code Instant} with the specified number of nanoseconds added.
799      *  This is equivalent to {@link #plusNanos(long)}.
800      * !(li){@code MICROS} -
801      *  Returns an {@code Instant} with the specified number of microseconds added.
802      *  This is equivalent to {@link #plusNanos(long)} with the amount
803      *  multiplied by 1,000.
804      * !(li){@code MILLIS} -
805      *  Returns an {@code Instant} with the specified number of milliseconds added.
806      *  This is equivalent to {@link #plusNanos(long)} with the amount
807      *  multiplied by 1,000,000.
808      * !(li){@code SECONDS} -
809      *  Returns an {@code Instant} with the specified number of seconds added.
810      *  This is equivalent to {@link #plusSeconds(long)}.
811      * !(li){@code MINUTES} -
812      *  Returns an {@code Instant} with the specified number of minutes added.
813      *  This is equivalent to {@link #plusSeconds(long)} with the amount
814      *  multiplied by 60.
815      * !(li){@code HOURS} -
816      *  Returns an {@code Instant} with the specified number of hours added.
817      *  This is equivalent to {@link #plusSeconds(long)} with the amount
818      *  multiplied by 3,600.
819      * !(li){@code HALF_DAYS} -
820      *  Returns an {@code Instant} with the specified number of half-days added.
821      *  This is equivalent to {@link #plusSeconds(long)} with the amount
822      *  multiplied by 43,200 (12 hours).
823      * !(li){@code DAYS} -
824      *  Returns an {@code Instant} with the specified number of days added.
825      *  This is equivalent to {@link #plusSeconds(long)} with the amount
826      *  multiplied by 86,400 (24 hours).
827      * </ul>
828      * !(p)
829      * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
830      * !(p)
831      * If the field is not a {@code ChronoUnit}, then the result of this method
832      * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
833      * passing {@code this} as the argument. In this case, the unit determines
834      * whether and how to perform the addition.
835      * !(p)
836      * This instance is immutable and unaffected by this method call.
837      *
838      * @param amountToAdd  the amount of the unit to add to the result, may be negative
839      * @param unit  the unit of the amount to add, not null
840      * @return an {@code Instant} based on this instant with the specified amount added, not null
841      * @throws DateTimeException if the addition cannot be made
842      * @throws UnsupportedTemporalTypeException if the unit is not supported
843      * @throws ArithmeticException if numeric overflow occurs
844      */
845     override
846     Instant plus(long amountToAdd, TemporalUnit unit) {
847         if (cast(ChronoUnit)(unit) !is null) {
848             auto name = (cast(ChronoUnit) unit).toString;
849              {
850                 if(name ==  ChronoUnit.NANOS.toString) return plusNanos(amountToAdd);
851                 if(name ==  ChronoUnit.MICROS.toString) return plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000);
852                 if(name ==  ChronoUnit.MILLIS.toString) return plusMillis(amountToAdd);
853                 if(name ==  ChronoUnit.SECONDS.toString) return plusSeconds(amountToAdd);
854                 if(name ==  ChronoUnit.MINUTES.toString) return plusSeconds(MathHelper.multiplyExact(amountToAdd , LocalTime.SECONDS_PER_MINUTE));
855                 if(name ==  ChronoUnit.HOURS.toString) return plusSeconds(MathHelper.multiplyExact(amountToAdd , LocalTime.SECONDS_PER_HOUR));
856                 if(name ==  ChronoUnit.HALF_DAYS.toString) return plusSeconds(MathHelper.multiplyExact(amountToAdd , LocalTime.SECONDS_PER_DAY / 2));
857                 if(name ==  ChronoUnit.DAYS.toString) return plusSeconds(MathHelper.multiplyExact(amountToAdd , LocalTime.SECONDS_PER_DAY));
858             }
859             throw new UnsupportedTemporalTypeException("Unsupported unit: " ~ typeid(unit).stringof);
860         }
861         return cast(Instant)(unit.addTo(this, amountToAdd));
862     }
863 
864     //-----------------------------------------------------------------------
865     /**
866      * Returns a copy of this instant with the specified duration _in seconds added.
867      * !(p)
868      * This instance is immutable and unaffected by this method call.
869      *
870      * @param secondsToAdd  the seconds to add, positive or negative
871      * @return an {@code Instant} based on this instant with the specified seconds added, not null
872      * @throws DateTimeException if the result exceeds the maximum or minimum instant
873      * @throws ArithmeticException if numeric overflow occurs
874      */
875     Instant plusSeconds(long secondsToAdd) {
876         return plus(secondsToAdd, 0);
877     }
878 
879     /**
880      * Returns a copy of this instant with the specified duration _in milliseconds added.
881      * !(p)
882      * This instance is immutable and unaffected by this method call.
883      *
884      * @param millisToAdd  the milliseconds to add, positive or negative
885      * @return an {@code Instant} based on this instant with the specified milliseconds added, not null
886      * @throws DateTimeException if the result exceeds the maximum or minimum instant
887      * @throws ArithmeticException if numeric overflow occurs
888      */
889     Instant plusMillis(long millisToAdd) {
890         return plus(millisToAdd / 1000, (millisToAdd % 1000) * 1000_000);
891     }
892 
893     /**
894      * Returns a copy of this instant with the specified duration _in nanoseconds added.
895      * !(p)
896      * This instance is immutable and unaffected by this method call.
897      *
898      * @param nanosToAdd  the nanoseconds to add, positive or negative
899      * @return an {@code Instant} based on this instant with the specified nanoseconds added, not null
900      * @throws DateTimeException if the result exceeds the maximum or minimum instant
901      * @throws ArithmeticException if numeric overflow occurs
902      */
903     Instant plusNanos(long nanosToAdd) {
904         return plus(0, nanosToAdd);
905     }
906 
907     /**
908      * Returns a copy of this instant with the specified duration added.
909      * !(p)
910      * This instance is immutable and unaffected by this method call.
911      *
912      * @param secondsToAdd  the seconds to add, positive or negative
913      * @param nanosToAdd  the nanos to add, positive or negative
914      * @return an {@code Instant} based on this instant with the specified seconds added, not null
915      * @throws DateTimeException if the result exceeds the maximum or minimum instant
916      * @throws ArithmeticException if numeric overflow occurs
917      */
918     private Instant plus(long secondsToAdd, long nanosToAdd) {
919         if ((secondsToAdd | nanosToAdd) == 0) {
920             return this;
921         }
922         long epochSec = MathHelper.addExact(seconds , secondsToAdd);
923         epochSec = MathHelper.addExact(epochSec , nanosToAdd / LocalTime.NANOS_PER_SECOND);
924         nanosToAdd = nanosToAdd % LocalTime.NANOS_PER_SECOND;
925         long nanoAdjustment = nanos + nanosToAdd;  // safe int+NANOS_PER_SECOND
926         return ofEpochSecond(epochSec, nanoAdjustment);
927     }
928 
929     //-----------------------------------------------------------------------
930     /**
931      * Returns a copy of this instant with the specified amount subtracted.
932      * !(p)
933      * This returns an {@code Instant}, based on this one, with the specified amount subtracted.
934      * The amount is typically {@link Duration} but may be any other type implementing
935      * the {@link TemporalAmount} interface.
936      * !(p)
937      * The calculation is delegated to the amount object by calling
938      * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
939      * to implement the subtraction _in any way it wishes, however it typically
940      * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
941      * of the amount implementation to determine if it can be successfully subtracted.
942      * !(p)
943      * This instance is immutable and unaffected by this method call.
944      *
945      * @param amountToSubtract  the amount to subtract, not null
946      * @return an {@code Instant} based on this instant with the subtraction made, not null
947      * @throws DateTimeException if the subtraction cannot be made
948      * @throws ArithmeticException if numeric overflow occurs
949      */
950     override
951     Instant minus(TemporalAmount amountToSubtract) {
952         return cast(Instant) amountToSubtract.subtractFrom(this);
953     }
954 
955     /**
956      * Returns a copy of this instant with the specified amount subtracted.
957      * !(p)
958      * This returns an {@code Instant}, based on this one, with the amount
959      * _in terms of the unit subtracted. If it is not possible to subtract the amount,
960      * because the unit is not supported or for some other reason, an exception is thrown.
961      * !(p)
962      * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
963      * See that method for a full description of how addition, and thus subtraction, works.
964      * !(p)
965      * This instance is immutable and unaffected by this method call.
966      *
967      * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
968      * @param unit  the unit of the amount to subtract, not null
969      * @return an {@code Instant} based on this instant with the specified amount subtracted, not null
970      * @throws DateTimeException if the subtraction cannot be made
971      * @throws UnsupportedTemporalTypeException if the unit is not supported
972      * @throws ArithmeticException if numeric overflow occurs
973      */
974     override
975     Instant minus(long amountToSubtract, TemporalUnit unit) {
976         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
977     }
978 
979     //-----------------------------------------------------------------------
980     /**
981      * Returns a copy of this instant with the specified duration _in seconds subtracted.
982      * !(p)
983      * This instance is immutable and unaffected by this method call.
984      *
985      * @param secondsToSubtract  the seconds to subtract, positive or negative
986      * @return an {@code Instant} based on this instant with the specified seconds subtracted, not null
987      * @throws DateTimeException if the result exceeds the maximum or minimum instant
988      * @throws ArithmeticException if numeric overflow occurs
989      */
990     Instant minusSeconds(long secondsToSubtract) {
991         if (secondsToSubtract == Long.MIN_VALUE) {
992             return plusSeconds(Long.MAX_VALUE).plusSeconds(1);
993         }
994         return plusSeconds(-secondsToSubtract);
995     }
996 
997     /**
998      * Returns a copy of this instant with the specified duration _in milliseconds subtracted.
999      * !(p)
1000      * This instance is immutable and unaffected by this method call.
1001      *
1002      * @param millisToSubtract  the milliseconds to subtract, positive or negative
1003      * @return an {@code Instant} based on this instant with the specified milliseconds subtracted, not null
1004      * @throws DateTimeException if the result exceeds the maximum or minimum instant
1005      * @throws ArithmeticException if numeric overflow occurs
1006      */
1007     Instant minusMillis(long millisToSubtract) {
1008         if (millisToSubtract == Long.MIN_VALUE) {
1009             return plusMillis(Long.MAX_VALUE).plusMillis(1);
1010         }
1011         return plusMillis(-millisToSubtract);
1012     }
1013 
1014     /**
1015      * Returns a copy of this instant with the specified duration _in nanoseconds subtracted.
1016      * !(p)
1017      * This instance is immutable and unaffected by this method call.
1018      *
1019      * @param nanosToSubtract  the nanoseconds to subtract, positive or negative
1020      * @return an {@code Instant} based on this instant with the specified nanoseconds subtracted, not null
1021      * @throws DateTimeException if the result exceeds the maximum or minimum instant
1022      * @throws ArithmeticException if numeric overflow occurs
1023      */
1024     Instant minusNanos(long nanosToSubtract) {
1025         if (nanosToSubtract == Long.MIN_VALUE) {
1026             return plusNanos(Long.MAX_VALUE).plusNanos(1);
1027         }
1028         return plusNanos(-nanosToSubtract);
1029     }
1030 
1031     //-------------------------------------------------------------------------
1032     /**
1033      * Queries this instant using the specified query.
1034      * !(p)
1035      * This queries this instant using the specified query strategy object.
1036      * The {@code TemporalQuery} object defines the logic to be used to
1037      * obtain the result. Read the documentation of the query to understand
1038      * what the result of this method will be.
1039      * !(p)
1040      * The result of this method is obtained by invoking the
1041      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
1042      * specified query passing {@code this} as the argument.
1043      *
1044      * @param !(R) the type of the result
1045      * @param query  the query to invoke, not null
1046      * @return the query result, null may be returned (defined by the query)
1047      * @throws DateTimeException if unable to query (defined by the query)
1048      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
1049      */
1050     /*@SuppressWarnings("unchecked")*/
1051     // override
1052     R query(R)(TemporalQuery!(R) query) {
1053         if (query == TemporalQueries.precision()) {
1054             return cast(R) (ChronoUnit.NANOS);
1055         }
1056         // inline TemporalAccessor.super.query(query) as an optimization
1057         if (query == TemporalQueries.chronology() || query == TemporalQueries.zoneId() ||
1058                 query == TemporalQueries.zone() || query == TemporalQueries.offset() ||
1059                 query == TemporalQueries.localDate() || query == TemporalQueries.localTime()) {
1060             return null;
1061         }
1062         return query.queryFrom(this);
1063     }
1064 
1065     /**
1066      * Adjusts the specified temporal object to have this instant.
1067      * !(p)
1068      * This returns a temporal object of the same observable type as the input
1069      * with the instant changed to be the same as this.
1070      * !(p)
1071      * The adjustment is equivalent to using {@link Temporal#_with(TemporalField, long)}
1072      * twice, passing {@link ChronoField#INSTANT_SECONDS} and
1073      * {@link ChronoField#NANO_OF_SECOND} as the fields.
1074      * !(p)
1075      * In most cases, it is clearer to reverse the calling pattern by using
1076      * {@link Temporal#_with(TemporalAdjuster)}:
1077      * !(pre)
1078      *   // these two lines are equivalent, but the second approach is recommended
1079      *   temporal = thisInstant.adjustInto(temporal);
1080      *   temporal = temporal._with(thisInstant);
1081      * </pre>
1082      * !(p)
1083      * This instance is immutable and unaffected by this method call.
1084      *
1085      * @param temporal  the target object to be adjusted, not null
1086      * @return the adjusted object, not null
1087      * @throws DateTimeException if unable to make the adjustment
1088      * @throws ArithmeticException if numeric overflow occurs
1089      */
1090     override
1091     Temporal adjustInto(Temporal temporal) {
1092         return temporal._with(ChronoField.INSTANT_SECONDS, seconds)._with(ChronoField.NANO_OF_SECOND, nanos);
1093     }
1094 
1095     /**
1096      * Calculates the amount of time until another instant _in terms of the specified unit.
1097      * !(p)
1098      * This calculates the amount of time between two {@code Instant}
1099      * objects _in terms of a single {@code TemporalUnit}.
1100      * The start and end points are {@code this} and the specified instant.
1101      * The result will be negative if the end is before the start.
1102      * The calculation returns a whole number, representing the number of
1103      * complete units between the two instants.
1104      * The {@code Temporal} passed to this method is converted to a
1105      * {@code Instant} using {@link #from(TemporalAccessor)}.
1106      * For example, the amount _in seconds between two dates can be calculated
1107      * using {@code startInstant.until(endInstant, SECONDS)}.
1108      * !(p)
1109      * There are two equivalent ways of using this method.
1110      * The first is to invoke this method.
1111      * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
1112      * !(pre)
1113      *   // these two lines are equivalent
1114      *   amount = start.until(end, SECONDS);
1115      *   amount = SECONDS.between(start, end);
1116      * </pre>
1117      * The choice should be made based on which makes the code more readable.
1118      * !(p)
1119      * The calculation is implemented _in this method for {@link ChronoUnit}.
1120      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
1121      * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS}
1122      * are supported. Other {@code ChronoUnit} values will throw an exception.
1123      * !(p)
1124      * If the unit is not a {@code ChronoUnit}, then the result of this method
1125      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
1126      * passing {@code this} as the first argument and the converted input temporal
1127      * as the second argument.
1128      * !(p)
1129      * This instance is immutable and unaffected by this method call.
1130      *
1131      * @param endExclusive  the end date, exclusive, which is converted to an {@code Instant}, not null
1132      * @param unit  the unit to measure the amount _in, not null
1133      * @return the amount of time between this instant and the end instant
1134      * @throws DateTimeException if the amount cannot be calculated, or the end
1135      *  temporal cannot be converted to an {@code Instant}
1136      * @throws UnsupportedTemporalTypeException if the unit is not supported
1137      * @throws ArithmeticException if numeric overflow occurs
1138      */
1139     override
1140     long until(Temporal endExclusive, TemporalUnit unit) {
1141         Instant end = Instant.from(endExclusive);
1142         if (cast(ChronoUnit)(unit) !is null) {
1143             auto name = (cast(ChronoUnit) unit).toString;
1144              {
1145                 if(name == ChronoUnit.NANOS.toString) return nanosUntil(end);
1146                 if(name == ChronoUnit.MICROS.toString) return nanosUntil(end) / 1000;
1147                 if(name == ChronoUnit.MILLIS.toString) return MathHelper.subtractExact(end.toEpochMilli() , toEpochMilli());
1148                 if(name == ChronoUnit.SECONDS.toString) return secondsUntil(end);
1149                 if(name == ChronoUnit.MINUTES.toString) return secondsUntil(end) / LocalTime.SECONDS_PER_MINUTE;
1150                 if(name == ChronoUnit.HOURS.toString) return secondsUntil(end) / LocalTime.SECONDS_PER_HOUR;
1151                 if(name == ChronoUnit.HALF_DAYS.toString) return secondsUntil(end) / (12 * LocalTime.SECONDS_PER_HOUR);
1152                 if(name == ChronoUnit.DAYS.toString) return secondsUntil(end) / (LocalTime.SECONDS_PER_DAY);
1153             }
1154             throw new UnsupportedTemporalTypeException("Unsupported unit: " ~ name);
1155         }
1156         return unit.between(this, end);
1157     }
1158 
1159     private long nanosUntil(Instant end) {
1160         long secsDiff = MathHelper.subtractExact(end.seconds , seconds);
1161         long totalNanos = MathHelper.multiplyExact(secsDiff , LocalTime.NANOS_PER_SECOND);
1162         return MathHelper.addExact(totalNanos , end.nanos - nanos);
1163     }
1164 
1165     private long secondsUntil(Instant end) {
1166         long secsDiff = MathHelper.subtractExact(end.seconds , seconds);
1167         long nanosDiff = end.nanos - nanos;
1168         if (secsDiff > 0 && nanosDiff < 0) {
1169             secsDiff--;
1170         } else if (secsDiff < 0 && nanosDiff > 0) {
1171             secsDiff++;
1172         }
1173         return secsDiff;
1174     }
1175 
1176     //-----------------------------------------------------------------------
1177     /**
1178      * Combines this instant with an offset to create an {@code OffsetDateTime}.
1179      * !(p)
1180      * This returns an {@code OffsetDateTime} formed from this instant at the
1181      * specified offset from UTC/Greenwich. An exception will be thrown if the
1182      * instant is too large to fit into an offset date-time.
1183      * !(p)
1184      * This method is equivalent to
1185      * {@link OffsetDateTime#ofInstant(Instant, ZoneId) OffsetDateTime.ofInstant(this, offset)}.
1186      *
1187      * @param offset  the offset to combine with, not null
1188      * @return the offset date-time formed from this instant and the specified offset, not null
1189      * @throws DateTimeException if the result exceeds the supported range
1190      */
1191     OffsetDateTime atOffset(ZoneOffset offset) {
1192         return OffsetDateTime.ofInstant(this, offset);
1193     }
1194 
1195     /**
1196      * Combines this instant with a time-zone to create a {@code ZonedDateTime}.
1197      * !(p)
1198      * This returns an {@code ZonedDateTime} formed from this instant at the
1199      * specified time-zone. An exception will be thrown if the instant is too
1200      * large to fit into a zoned date-time.
1201      * !(p)
1202      * This method is equivalent to
1203      * {@link ZonedDateTime#ofInstant(Instant, ZoneId) ZonedDateTime.ofInstant(this, zone)}.
1204      *
1205      * @param zone  the zone to combine with, not null
1206      * @return the zoned date-time formed from this instant and the specified zone, not null
1207      * @throws DateTimeException if the result exceeds the supported range
1208      */
1209     ZonedDateTime atZone(ZoneId zone) {
1210         return ZonedDateTime.ofInstant(this, zone);
1211     }
1212 
1213     //-----------------------------------------------------------------------
1214     /**
1215      * Converts this instant to the number of milliseconds from the epoch
1216      * of 1970-01-01T00:00:00Z.
1217      * !(p)
1218      * If this instant represents a point on the time-line too far _in the future
1219      * or past to fit _in a {@code long} milliseconds, then an exception is thrown.
1220      * !(p)
1221      * If this instant has greater than millisecond precision, then the conversion
1222      * will drop any excess precision information as though the amount _in nanoseconds
1223      * was subject to integer division by one million.
1224      *
1225      * @return the number of milliseconds since the epoch of 1970-01-01T00:00:00Z
1226      * @throws ArithmeticException if numeric overflow occurs
1227      */
1228     long toEpochMilli() {
1229         if (seconds < 0 && nanos > 0) {
1230             long millis = MathHelper.multiplyExact((seconds+1) , 1000);
1231             long adjustment = nanos / 1000_000 - 1000;
1232             return MathHelper.addExact(millis , adjustment);
1233         } else {
1234             long millis = MathHelper.multiplyExact(seconds , 1000);
1235             return MathHelper.addExact(millis , nanos / 1000_000);
1236         }
1237     }
1238 
1239     //-----------------------------------------------------------------------
1240     /**
1241      * Compares this instant to the specified instant.
1242      * !(p)
1243      * The comparison is based on the time-line position of the instants.
1244      * It is "consistent with equals", as defined by {@link Comparable}.
1245      *
1246      * @param otherInstant  the other instant to compare to, not null
1247      * @return the comparator value, negative if less, positive if greater
1248      * @throws NullPointerException if otherInstant is null
1249      */
1250     // override
1251     int compareTo(Instant otherInstant) {
1252         int cmp = compare(seconds, otherInstant.seconds);
1253         if (cmp != 0) {
1254             return cmp;
1255         }
1256         return nanos - otherInstant.nanos;
1257     }
1258 
1259     /**
1260      * Checks if this instant is after the specified instant.
1261      * !(p)
1262      * The comparison is based on the time-line position of the instants.
1263      *
1264      * @param otherInstant  the other instant to compare to, not null
1265      * @return true if this instant is after the specified instant
1266      * @throws NullPointerException if otherInstant is null
1267      */
1268     bool isAfter(Instant otherInstant) {
1269         return compareTo(otherInstant) > 0;
1270     }
1271 
1272     /**
1273      * Checks if this instant is before the specified instant.
1274      * !(p)
1275      * The comparison is based on the time-line position of the instants.
1276      *
1277      * @param otherInstant  the other instant to compare to, not null
1278      * @return true if this instant is before the specified instant
1279      * @throws NullPointerException if otherInstant is null
1280      */
1281     bool isBefore(Instant otherInstant) {
1282         return compareTo(otherInstant) < 0;
1283     }
1284 
1285     //-----------------------------------------------------------------------
1286     /**
1287      * Checks if this instant is equal to the specified instant.
1288      * !(p)
1289      * The comparison is based on the time-line position of the instants.
1290      *
1291      * @param otherInstant  the other instant, null returns false
1292      * @return true if the other instant is equal to this one
1293      */
1294     override
1295     bool opEquals(Object otherInstant) {
1296         if (this == otherInstant) {
1297             return true;
1298         }
1299         if (cast(Instant)(otherInstant) !is null) {
1300             Instant other = cast(Instant) otherInstant;
1301             return this.seconds == other.seconds &&
1302                    this.nanos == other.nanos;
1303         }
1304         return false;
1305     }
1306 
1307     /**
1308      * Returns a hash code for this instant.
1309      *
1310      * @return a suitable hash code
1311      */
1312     override
1313     size_t toHash() @trusted nothrow {
1314         return (cast(int) (seconds ^ (seconds >>> 32))) + 51 * nanos;
1315     }
1316 
1317     //-----------------------------------------------------------------------
1318     /**
1319      * A string representation of this instant using ISO-8601 representation.
1320      * !(p)
1321      * The format used is the same as {@link DateTimeFormatter#ISO_INSTANT}.
1322      *
1323      * @return an ISO-8601 representation of this instant, not null
1324      */
1325     override
1326     string toString() {
1327         // TODO: Tasks pending completion -@zxp at 12/27/2018, 8:05:39 PM
1328         // 
1329         return "TODO";
1330         // return DateTimeFormatter.ISO_INSTANT.format(this);
1331     }
1332 
1333     // -----------------------------------------------------------------------
1334     /**
1335      * Writes the object using a
1336      * <a href="{@docRoot}/serialized-form.html#hunt.time.Ser">dedicated serialized form</a>.
1337      * @serialData
1338      * !(pre)
1339      *  _out.writeByte(2);  // identifies an Instant
1340      *  _out.writeLong(seconds);
1341      *  _out.writeInt(nanos);
1342      * </pre>
1343      *
1344      * @return the instance of {@code Ser}, not null
1345      */
1346     private Object writeReplace() {
1347         return new Ser(Ser.INSTANT_TYPE, this);
1348     }
1349 
1350     /**
1351      * Defend against malicious streams.
1352      *
1353      * @param s the stream to read
1354      * @throws InvalidObjectException always
1355      */
1356      ///@gxc
1357     // private void readObject(ObjectInputStream s) /*throws InvalidObjectException*/ {
1358     //     throw new InvalidObjectException("Deserialization via serialization delegate");
1359     // }
1360 
1361     void writeExternal(DataOutput _out) /*throws IOException*/ {
1362         _out.writeLong(seconds);
1363         _out.writeInt(nanos);
1364     }
1365 
1366     static Instant readExternal(DataInput _in) /*throws IOException*/ {
1367         long seconds = _in.readLong();
1368         int nanos = _in.readInt();
1369         return Instant.ofEpochSecond(seconds, nanos);
1370     }
1371 
1372     override int opCmp(Instant o)
1373     {
1374         auto res = compare(this.seconds,o.seconds);
1375         if(res == 0)
1376             res = compare(this.nanos,o.nanos);
1377         return res;
1378     }
1379 
1380 }