Whether or not to abbreviate the output, using h instead of hours, m instead of minutes, etc.
Number of units to include in the output text, where such is "weeks", "days", "hours", "minutes" and "seconds", a fake approximate unit "months", and a fake "years" based on it. Passing a numUnits of 7 will express the time difference using all units. Passing one of 4 will only express it in days, hours, minutes and seconds. Passing 1 will express it in only seconds.
Number of units to skip from output, going from least significant (seconds) to most significant (years).
Whether to round up or floor seconds, minutes and hours. Larger units are floored regardless of this setting.
A period of time.
Output buffer sink to write to.
Appender!(char[]) sink; immutable then = MonoTime.currTime; Thread.sleep(1.seconds); immutable now = MonoTime.currTime; immutable duration = (now - then); immutable inEnglish = duration.timeSinceInto(sink);
1 import std.array : Appender; 2 import core.time; 3 4 Appender!(char[]) sink; 5 sink.reserve(64); // workaround for formattedWrite < 2.076 6 7 { 8 immutable dur = Duration.zero; 9 dur.timeSinceInto(sink); 10 assert((sink.data == "0 seconds"), sink.data); 11 sink.clear(); 12 dur.timeSinceInto(sink, abbreviate: true); 13 assert((sink.data == "0s"), sink.data); 14 sink.clear(); 15 } 16 { 17 immutable dur = 3_141_519_265.msecs; 18 dur.timeSinceInto!(4, 1)(sink, abbreviate: false, roundUp: false); 19 assert((sink.data == "36 days, 8 hours and 38 minutes"), sink.data); 20 sink.clear(); 21 dur.timeSinceInto!(4, 1)(sink, abbreviate: true, roundUp: false); 22 assert((sink.data == "36d 8h 38m"), sink.data); 23 sink.clear(); 24 } 25 { 26 immutable dur = 3_141_519_265.msecs; 27 dur.timeSinceInto!(4, 1)(sink, abbreviate: false, roundUp: true); 28 assert((sink.data == "36 days, 8 hours and 39 minutes"), sink.data); 29 sink.clear(); 30 dur.timeSinceInto!(4, 1)(sink, abbreviate: true, roundUp: true); 31 assert((sink.data == "36d 8h 39m"), sink.data); 32 sink.clear(); 33 } 34 { 35 immutable dur = 3599.seconds; 36 dur.timeSinceInto!(2, 1)(sink, abbreviate: false, roundUp: false); 37 assert((sink.data == "59 minutes"), sink.data); 38 sink.clear(); 39 dur.timeSinceInto!(2, 1)(sink, abbreviate: true, roundUp: false); 40 assert((sink.data == "59m"), sink.data); 41 sink.clear(); 42 } 43 { 44 immutable dur = 3599.seconds; 45 dur.timeSinceInto!(2, 1)(sink, abbreviate: false, roundUp: true); 46 assert((sink.data == "60 minutes"), sink.data); 47 sink.clear(); 48 dur.timeSinceInto!(2, 1)(sink, abbreviate: true, roundUp: true); 49 assert((sink.data == "60m"), sink.data); 50 sink.clear(); 51 } 52 { 53 immutable dur = 3599.seconds; 54 dur.timeSinceInto!(3, 1)(sink, abbreviate: false, roundUp: true); 55 assert((sink.data == "1 hour"), sink.data); 56 sink.clear(); 57 dur.timeSinceInto!(3, 1)(sink, abbreviate: true, roundUp: true); 58 assert((sink.data == "1h"), sink.data); 59 sink.clear(); 60 } 61 { 62 immutable dur = 3.days + 35.minutes; 63 dur.timeSinceInto!(4, 1)(sink, abbreviate: false, roundUp: false); 64 assert((sink.data == "3 days and 35 minutes"), sink.data); 65 sink.clear(); 66 dur.timeSinceInto!(4, 1)(sink, abbreviate: true, roundUp: false); 67 assert((sink.data == "3d 35m"), sink.data); 68 sink.clear(); 69 } 70 { 71 immutable dur = 3.days + 35.minutes; 72 dur.timeSinceInto!(4, 2)(sink, abbreviate: false, roundUp: true); 73 assert((sink.data == "3 days and 1 hour"), sink.data); 74 sink.clear(); 75 dur.timeSinceInto!(4, 2)(sink, abbreviate: true, roundUp: true); 76 assert((sink.data == "3d 1h"), sink.data); 77 sink.clear(); 78 } 79 { 80 immutable dur = 57.weeks + 1.days + 2.hours + 3.minutes + 4.seconds; 81 dur.timeSinceInto!(7, 4)(sink, abbreviate: false); 82 assert((sink.data == "1 year, 1 month and 1 week"), sink.data); 83 sink.clear(); 84 dur.timeSinceInto!(7, 4)(sink, abbreviate: true); 85 assert((sink.data == "1y 1m 1w"), sink.data); 86 sink.clear(); 87 } 88 { 89 immutable dur = 4.seconds; 90 dur.timeSinceInto!(7, 4)(sink, abbreviate: false); 91 assert((sink.data == "4 seconds"), sink.data); 92 sink.clear(); 93 dur.timeSinceInto!(7, 4)(sink, abbreviate: true); 94 assert((sink.data == "4s"), sink.data); 95 sink.clear(); 96 } 97 { 98 immutable dur = 2.hours + 28.minutes + 19.seconds; 99 dur.timeSinceInto!(7, 1)(sink, abbreviate: false); 100 assert((sink.data == "2 hours and 28 minutes"), sink.data); 101 sink.clear(); 102 dur.timeSinceInto!(7, 1)(sink, abbreviate: true); 103 assert((sink.data == "2h 28m"), sink.data); 104 sink.clear(); 105 } 106 { 107 immutable dur = -1.minutes + -1.seconds; 108 dur.timeSinceInto!(2, 0)(sink, abbreviate: false); 109 assert((sink.data == "-1 minute and 1 second"), sink.data); 110 sink.clear(); 111 dur.timeSinceInto!(2, 0)(sink, abbreviate: true); 112 assert((sink.data == "-1m 1s"), sink.data); 113 sink.clear(); 114 } 115 { 116 immutable dur = 30.seconds; 117 dur.timeSinceInto!(3, 1)(sink, abbreviate: false, roundUp: false); 118 assert((sink.data == "30 seconds"), sink.data); 119 sink.clear(); 120 dur.timeSinceInto!(3, 1)(sink, abbreviate: true, roundUp: false); 121 assert((sink.data == "30s"), sink.data); 122 sink.clear(); 123 } 124 { 125 immutable dur = 30.seconds; 126 dur.timeSinceInto!(3, 1)(sink, abbreviate: false, roundUp: true); 127 assert((sink.data == "1 minute"), sink.data); 128 sink.clear(); 129 dur.timeSinceInto!(3, 1)(sink, abbreviate: true, roundUp: true); 130 assert((sink.data == "1m"), sink.data); 131 sink.clear(); 132 } 133 { 134 immutable dur = 23.hours + 59.minutes + 59.seconds; 135 dur.timeSinceInto!(5, 3)(sink, abbreviate: false, roundUp: true); 136 assert((sink.data == "1 day"), sink.data); 137 sink.clear(); 138 dur.timeSinceInto!(5, 3)(sink, abbreviate: true, roundUp: true); 139 assert((sink.data == "1d"), sink.data); 140 sink.clear(); 141 } 142 { 143 immutable dur = 6.days + 23.hours + 59.minutes; 144 dur.timeSinceInto!(5, 4)(sink, abbreviate: false, roundUp: false); 145 assert((sink.data == "6 days"), sink.data); 146 sink.clear(); 147 dur.timeSinceInto!(5, 4)(sink, abbreviate: true, roundUp: false); 148 assert((sink.data == "6d"), sink.data); 149 sink.clear(); 150 } 151 { 152 immutable dur = 6.days + 23.hours + 59.minutes; 153 dur.timeSinceInto!(5, 4)(sink, abbreviate: false, roundUp: true); 154 assert((sink.data == "1 week"), sink.data); 155 sink.clear(); 156 dur.timeSinceInto!(5, 4)(sink, abbreviate: true, roundUp: true); 157 assert((sink.data == "1w"), sink.data); 158 sink.clear(); 159 }
Express how much time has passed in a Duration, in natural (English) language. Overload that writes the result to the passed output range sink.