mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
add NanoDuration.to_format() (#935)
* lib/std/time: avoid switch in DateTime.compare_to() * lib/std/time: add NanoDuration.to_format() * std/lib: fix #934
This commit is contained in:
@@ -141,7 +141,7 @@ fn void! Formatter.out_substr(&self, String str) @private
|
||||
usz l = conv::utf8_codepoints(str);
|
||||
uint prec = self.prec;
|
||||
if (self.flags.precision && l < prec) l = prec;
|
||||
self.right_adjust(' ')!;
|
||||
self.right_adjust(l)!;
|
||||
usz index = 0;
|
||||
usz chars = str.len;
|
||||
char* ptr = str.ptr;
|
||||
|
||||
@@ -133,24 +133,19 @@ fn Time DateTime.to_time(&self) @inline
|
||||
return self.time;
|
||||
}
|
||||
|
||||
fn bool DateTime.after(&self, DateTime compare)
|
||||
fn bool DateTime.after(&self, DateTime compare) @inline
|
||||
{
|
||||
return self.time > compare.time;
|
||||
}
|
||||
|
||||
fn bool DateTime.before(&self, DateTime compare)
|
||||
fn bool DateTime.before(&self, DateTime compare) @inline
|
||||
{
|
||||
return self.time < compare.time;
|
||||
}
|
||||
|
||||
fn int DateTime.compare_to(&self, DateTime compare)
|
||||
{
|
||||
switch
|
||||
{
|
||||
case self.time > compare.time: return 1;
|
||||
case self.time < compare.time: return -1;
|
||||
default: return 0;
|
||||
}
|
||||
return compare_to(self.time, compare.time);
|
||||
}
|
||||
|
||||
fn int DateTime.diff_years(&self, DateTime from)
|
||||
|
||||
@@ -84,4 +84,71 @@ fn double Time.diff_weeks(time, Time other) => (long)time.diff_us(other) / (doub
|
||||
fn double NanoDuration.to_sec(nd) => (double)nd / 1_000_000_000.0;
|
||||
fn long NanoDuration.to_ms(nd) => (long)nd / 1_000_000;
|
||||
fn TimeDuration NanoDuration.to_duration(nd) => (TimeDuration)nd / 1_000;
|
||||
fn NanoDuration TimeDuration.to_nano(td) => (NanoDuration)td * 1_000;
|
||||
fn NanoDuration TimeDuration.to_nano(td) => (NanoDuration)td * 1_000;
|
||||
|
||||
fn void! NanoDuration.to_format(&self, Formatter* formatter) @dynamic
|
||||
{
|
||||
NanoDuration nd = *self;
|
||||
if (nd == 0)
|
||||
{
|
||||
formatter.printf("0s")!;
|
||||
return;
|
||||
}
|
||||
|
||||
bool neg = nd < 0;
|
||||
if (neg) nd = -nd;
|
||||
|
||||
DString str;
|
||||
str.tinit();
|
||||
if (nd < 1_000_000_000)
|
||||
{
|
||||
// Less than 1s: print milliseconds, microseconds and nanoseconds.
|
||||
NanoDuration ms = nd / 1_000_000;
|
||||
if (ms > 0)
|
||||
{
|
||||
str.printf("%dms", ms);
|
||||
nd -= ms * 1_000_000;
|
||||
}
|
||||
NanoDuration us = nd / 1000;
|
||||
if (us > 0)
|
||||
{
|
||||
str.printf("%dµs", us);
|
||||
nd -= us * 1000;
|
||||
}
|
||||
if (nd > 0)
|
||||
{
|
||||
str.printf("%dns", nd);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// More than 1s: print hours, minutes and seconds.
|
||||
NanoDuration ms = (nd - nd / 1_000_000_000 * 1_000_000_000) / 1_000_000;
|
||||
|
||||
nd /= 1_000_000_000;
|
||||
NanoDuration hour = nd / 3600;
|
||||
if (hour > 0)
|
||||
{
|
||||
str.printf("%dh", hour);
|
||||
nd -= hour * 3600;
|
||||
}
|
||||
NanoDuration min = nd / 60;
|
||||
if (min > 0)
|
||||
{
|
||||
str.printf("%dm", min);
|
||||
nd -= min * 60;
|
||||
}
|
||||
NanoDuration sec = nd;
|
||||
if (ms > 0)
|
||||
{
|
||||
// Ignore trailing zeroes.
|
||||
while (ms / 10 * 10 == ms) ms /= 10;
|
||||
str.printf("%d.%ds", sec, ms);
|
||||
}
|
||||
else
|
||||
{
|
||||
str.printf("%ds", sec);
|
||||
}
|
||||
}
|
||||
formatter.printf(str.as_str())!;
|
||||
}
|
||||
Reference in New Issue
Block a user