timeSinceInto

Express how much time has passed in a Duration, in natural (English) language. Overload that writes the result to the passed output range sink.

pure
void
timeSinceInto
(
uint numUnits = 7
uint truncateUnits = 0
Sink
)
(
const Duration signedDuration
,
auto ref Sink sink
,
const Flag!"abbreviate" abbreviate = No.abbreviate
,
const Flag!"roundUp" roundUp = Yes.roundUp
)
if (
isOutputRange!(Sink, char[])
)

Parameters

abbreviate Flag!"abbreviate"

Whether or not to abbreviate the output, using h instead of hours, m instead of minutes, etc.

numUnits

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.

truncateUnits

Number of units to skip from output, going from least significant (seconds) to most significant (years).

roundUp Flag!"roundUp"

Whether to round up or floor seconds, minutes and hours. Larger units are floored regardless of this setting.

signedDuration Duration

A period of time.

sink Sink

Output buffer sink to write to.

Examples

Appender!(char[]) sink;

immutable then = Clock.currTime;
Thread.sleep(1.seconds);
immutable now = Clock.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, Yes.abbreviate);
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, No.abbreviate,  No.roundUp);
19     assert((sink.data == "36 days, 8 hours and 38 minutes"), sink.data);
20     sink.clear();
21     dur.timeSinceInto!(4, 1)(sink, Yes.abbreviate,  No.roundUp);
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, No.abbreviate, Yes.roundUp);
28     assert((sink.data == "36 days, 8 hours and 39 minutes"), sink.data);
29     sink.clear();
30     dur.timeSinceInto!(4, 1)(sink, Yes.abbreviate, Yes.roundUp);
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, No.abbreviate, No.roundUp);
37     assert((sink.data == "59 minutes"), sink.data);
38     sink.clear();
39     dur.timeSinceInto!(2, 1)(sink, Yes.abbreviate, No.roundUp);
40     assert((sink.data == "59m"), sink.data);
41     sink.clear();
42 }
43 {
44     immutable dur = 3599.seconds;
45     dur.timeSinceInto!(2, 1)(sink, No.abbreviate, Yes.roundUp);
46     assert((sink.data == "60 minutes"), sink.data);
47     sink.clear();
48     dur.timeSinceInto!(2, 1)(sink, Yes.abbreviate, Yes.roundUp);
49     assert((sink.data == "60m"), sink.data);
50     sink.clear();
51 }
52 {
53     immutable dur = 3599.seconds;
54     dur.timeSinceInto!(3, 1)(sink, No.abbreviate, Yes.roundUp);
55     assert((sink.data == "1 hour"), sink.data);
56     sink.clear();
57     dur.timeSinceInto!(3, 1)(sink, Yes.abbreviate, Yes.roundUp);
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, No.abbreviate, No.roundUp);
64     assert((sink.data == "3 days and 35 minutes"), sink.data);
65     sink.clear();
66     dur.timeSinceInto!(4, 1)(sink, Yes.abbreviate, No.roundUp);
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, No.abbreviate, Yes.roundUp);
73     assert((sink.data == "3 days and 1 hour"), sink.data);
74     sink.clear();
75     dur.timeSinceInto!(4, 2)(sink, Yes.abbreviate, Yes.roundUp);
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, No.abbreviate);
82     assert((sink.data == "1 year, 1 month and 1 week"), sink.data);
83     sink.clear();
84     dur.timeSinceInto!(7, 4)(sink, Yes.abbreviate);
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, No.abbreviate);
91     assert((sink.data == "4 seconds"), sink.data);
92     sink.clear();
93     dur.timeSinceInto!(7, 4)(sink, Yes.abbreviate);
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, No.abbreviate);
100     assert((sink.data == "2 hours and 28 minutes"), sink.data);
101     sink.clear();
102     dur.timeSinceInto!(7, 1)(sink, Yes.abbreviate);
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, No.abbreviate);
109     assert((sink.data == "-1 minute and 1 second"), sink.data);
110     sink.clear();
111     dur.timeSinceInto!(2, 0)(sink, Yes.abbreviate);
112     assert((sink.data == "-1m 1s"), sink.data);
113     sink.clear();
114 }
115 {
116     immutable dur = 30.seconds;
117     dur.timeSinceInto!(3, 1)(sink, No.abbreviate, No.roundUp);
118     assert((sink.data == "30 seconds"), sink.data);
119     sink.clear();
120     dur.timeSinceInto!(3, 1)(sink, Yes.abbreviate, No.roundUp);
121     assert((sink.data == "30s"), sink.data);
122     sink.clear();
123 }
124 {
125     immutable dur = 30.seconds;
126     dur.timeSinceInto!(3, 1)(sink, No.abbreviate, Yes.roundUp);
127     assert((sink.data == "1 minute"), sink.data);
128     sink.clear();
129     dur.timeSinceInto!(3, 1)(sink, Yes.abbreviate, Yes.roundUp);
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, No.abbreviate, Yes.roundUp);
136     assert((sink.data == "1 day"), sink.data);
137     sink.clear();
138     dur.timeSinceInto!(5, 3)(sink, Yes.abbreviate, Yes.roundUp);
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, No.abbreviate, No.roundUp);
145     assert((sink.data == "6 days"), sink.data);
146     sink.clear();
147     dur.timeSinceInto!(5, 4)(sink, Yes.abbreviate, No.roundUp);
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, No.abbreviate, Yes.roundUp);
154     assert((sink.data == "1 week"), sink.data);
155     sink.clear();
156     dur.timeSinceInto!(5, 4)(sink, Yes.abbreviate, Yes.roundUp);
157     assert((sink.data == "1w"), sink.data);
158     sink.clear();
159 }

Meta