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.
A string with the passed duration expressed in natural English language.
immutable then = MonoTime.currTime; Thread.sleep(1.seconds); immutable now = MonoTime.currTime; immutable duration = (now - then); immutable inEnglish = timeSince(duration);
1 import core.time; 2 3 { 4 immutable dur = 789_383.seconds; // 1 week, 2 days, 3 hours, 16 minutes, and 23 secs 5 immutable since = dur.timeSince!(4, 1)(abbreviate: false); 6 immutable abbrev = dur.timeSince!(4, 1)(abbreviate: true); 7 assert((since == "9 days, 3 hours and 16 minutes"), since); 8 assert((abbrev == "9d 3h 16m"), abbrev); 9 } 10 { 11 immutable dur = 789_383.seconds; // 1 week, 2 days, 3 hours, 16 minutes, and 23 secs 12 immutable since = dur.timeSince!(5, 1)(abbreviate: false); 13 immutable abbrev = dur.timeSince!(5, 1)(abbreviate: true); 14 assert((since == "1 week, 2 days, 3 hours and 16 minutes"), since); 15 assert((abbrev == "1w 2d 3h 16m"), abbrev); 16 } 17 { 18 immutable dur = 789_383.seconds; 19 immutable since = dur.timeSince!(1)(abbreviate: false); 20 immutable abbrev = dur.timeSince!(1)(abbreviate: true); 21 assert((since == "789383 seconds"), since); 22 assert((abbrev == "789383s"), abbrev); 23 } 24 { 25 immutable dur = 789_383.seconds; 26 immutable since = dur.timeSince!(2, 0)(abbreviate: false); 27 immutable abbrev = dur.timeSince!(2, 0)(abbreviate: true); 28 assert((since == "13156 minutes and 23 seconds"), since); 29 assert((abbrev == "13156m 23s"), abbrev); 30 } 31 { 32 immutable dur = 3_620.seconds; // 1 hour and 20 secs 33 immutable since = dur.timeSince!(7, 1)(abbreviate: false); 34 immutable abbrev = dur.timeSince!(7, 1)(abbreviate: true); 35 assert((since == "1 hour"), since); 36 assert((abbrev == "1h"), abbrev); 37 } 38 { 39 immutable dur = 30.seconds; // 30 secs 40 immutable since = dur.timeSince; 41 immutable abbrev = dur.timeSince(abbreviate: true); 42 assert((since == "30 seconds"), since); 43 assert((abbrev == "30s"), abbrev); 44 } 45 { 46 immutable dur = 1.seconds; 47 immutable since = dur.timeSince; 48 immutable abbrev = dur.timeSince(abbreviate: true); 49 assert((since == "1 second"), since); 50 assert((abbrev == "1s"), abbrev); 51 } 52 { 53 immutable dur = 1.days + 1.minutes + 1.seconds; 54 immutable since = dur.timeSince!(7, 0)(abbreviate: false); 55 immutable abbrev = dur.timeSince!(7, 0)(abbreviate: true); 56 assert((since == "1 day, 1 minute and 1 second"), since); 57 assert((abbrev == "1d 1m 1s"), abbrev); 58 } 59 { 60 immutable dur = 3.weeks + 6.days + 10.hours; 61 immutable since = dur.timeSince(abbreviate: false); 62 immutable abbrev = dur.timeSince(abbreviate: true); 63 assert((since == "3 weeks, 6 days and 10 hours"), since); 64 assert((abbrev == "3w 6d 10h"), abbrev); 65 } 66 { 67 immutable dur = 377.days + 11.hours; 68 immutable since = dur.timeSince!(6)(abbreviate: false); 69 immutable abbrev = dur.timeSince!(6)(abbreviate: true); 70 assert((since == "12 months, 2 weeks, 3 days and 11 hours"), since); 71 assert((abbrev == "12m 2w 3d 11h"), abbrev); 72 } 73 { 74 immutable dur = 395.days + 11.seconds; 75 immutable since = dur.timeSince!(7, 1)(abbreviate: false); 76 immutable abbrev = dur.timeSince!(7, 1)(abbreviate: true); 77 assert((since == "1 year, 1 month and 5 days"), since); 78 assert((abbrev == "1y 1m 5d"), abbrev); 79 } 80 { 81 immutable dur = 1.weeks + 9.days; 82 immutable since = dur.timeSince!(5)(abbreviate: false); 83 immutable abbrev = dur.timeSince!(5)(abbreviate: true); 84 assert((since == "2 weeks and 2 days"), since); 85 assert((abbrev == "2w 2d"), abbrev); 86 } 87 { 88 immutable dur = 30.days + 1.weeks; 89 immutable since = dur.timeSince!(5)(abbreviate: false); 90 immutable abbrev = dur.timeSince!(5)(abbreviate: true); 91 assert((since == "5 weeks and 2 days"), since); 92 assert((abbrev == "5w 2d"), abbrev); 93 } 94 { 95 immutable dur = 30.days + 1.weeks + 1.seconds; 96 immutable since = dur.timeSince!(4, 0)(abbreviate: false); 97 immutable abbrev = dur.timeSince!(4, 0)(abbreviate: true); 98 assert((since == "37 days and 1 second"), since); 99 assert((abbrev == "37d 1s"), abbrev); 100 } 101 { 102 immutable dur = 267.weeks + 4.days + 9.hours + 15.minutes + 1.seconds; 103 immutable since = dur.timeSince!(7, 0)(abbreviate: false); 104 immutable abbrev = dur.timeSince!(7, 0)(abbreviate: true); 105 assert((since == "5 years, 2 months, 1 week, 6 days, 9 hours, 15 minutes and 1 second"), since); 106 assert((abbrev == "5y 2m 1w 6d 9h 15m 1s"), abbrev); 107 } 108 { 109 immutable dur = 360.days + 350.days; 110 immutable since = dur.timeSince!(7, 6)(abbreviate: false); 111 immutable abbrev = dur.timeSince!(7, 6)(abbreviate: true); 112 assert((since == "1 year"), since); 113 assert((abbrev == "1y"), abbrev); 114 } 115 { 116 immutable dur = 267.weeks + 4.days + 9.hours + 15.minutes + 1.seconds; 117 immutable since = dur.timeSince!(7, 3)(abbreviate: false); 118 immutable abbrev = dur.timeSince!(7, 3)(abbreviate: true); 119 assert((since == "5 years, 2 months, 1 week and 6 days"), since); 120 assert((abbrev == "5y 2m 1w 6d"), abbrev); 121 }
Express how much time has passed in a Duration, in natural (English) language. Overload that returns the result as a new string.