properly display duration in badges
Costa Tsaousis (ktsaou) committed
Jan 23, 2017 at 19:23 UTC
6c6ffcccd60fb3868d009be1d68d181768d60517
1 file changed
+28
-9
src/web_buffer_svg.c
+28
-9
@@ -502,6 +502,17 @@ static inline void calc_colorz(const char *color, char *final, size_t len, calcu
502
#define COLOR_STRING_SIZE 100
503
504
void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const char *units, const char *label_color, const char *value_color, int value_is_null, int precision) {
505
+ static uint32_t hash_seconds = 0, hash_seconds_ago = 0, hash_minutes = 0, hash_minutes_ago = 0, hash_hours = 0, hash_hours_ago = 0;
506
+
507
+ if(unlikely(!hash_seconds)) {
508
+ hash_seconds = simple_hash("seconds");
509
+ hash_seconds_ago = simple_hash("seconds ago");
510
+ hash_minutes = simple_hash("minutes");
511
+ hash_minutes_ago = simple_hash("minutes ago");
512
+ hash_hours = simple_hash("hours");
513
+ hash_hours_ago = simple_hash("hours ago");
514
+ }
515
+
516
char label_buffer[LABEL_STRING_SIZE + 1]
517
, value_color_buffer[COLOR_STRING_SIZE + 1]
518
, value_string[VALUE_STRING_SIZE + 1]
@@ -525,7 +536,11 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
536
if(unlikely(isalnum(*units)))
537
separator = " ";
538
528
- if(unlikely(!strcmp(units, "seconds"))) {
539
+ uint32_t hash_units = simple_hash(units);
540
+
541
+ if(unlikely((hash_units == hash_seconds && !strcmp(units, "seconds")) || (hash_units == hash_seconds_ago && !strcmp(units, "seconds ago")))) {
542
+ char *suffix = (hash_units == hash_seconds_ago)?" ago":"";
543
+
544
size_t s = (size_t)value;
545
size_t d = s / 86400;
546
s = s % 86400;
@@ -537,12 +552,14 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
552
s = s % 60;
553
554
if(d)
540
- snprintfz(value_string, VALUE_STRING_SIZE, "%zu %s %02zu:%02zu:%02zu", d, (d == 1)?"day":"days", h, m, s);
555
+ snprintfz(value_string, VALUE_STRING_SIZE, "%zu %s %02zu:%02zu:%02zu%s", d, (d == 1)?"day":"days", h, m, s, suffix);
556
else
542
- snprintfz(value_string, VALUE_STRING_SIZE, "%02zu:%02zu:%02zu", h, m, s);
557
+ snprintfz(value_string, VALUE_STRING_SIZE, "%02zu:%02zu:%02zu%s", h, m, s, suffix);
558
}
559
545
- else if(unlikely(!strcmp(units, "minutes"))) {
560
+ else if(unlikely((hash_units == hash_minutes && !strcmp(units, "minutes")) || (hash_units == hash_minutes_ago && !strcmp(units, "minutes ago")))) {
561
+ char *suffix = (hash_units == hash_minutes_ago)?" ago":"";
562
+
563
size_t m = (size_t)value;
564
size_t d = m / (60 * 24);
565
m = m % (60 * 24);
@@ -551,20 +568,22 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
568
m = m % 60;
569
570
if(d)
554
- snprintfz(value_string, VALUE_STRING_SIZE, "%zud %02zuh %02zum", d, h, m);
571
+ snprintfz(value_string, VALUE_STRING_SIZE, "%zud %02zuh %02zum%s", d, h, m, suffix);
572
else
556
- snprintfz(value_string, VALUE_STRING_SIZE, "%zuh %zum", h, m);
573
+ snprintfz(value_string, VALUE_STRING_SIZE, "%zuh %zum%s", h, m, suffix);
574
}
575
559
- else if(unlikely(!strcmp(units, "hours"))) {
576
+ else if(unlikely((hash_units == hash_hours && !strcmp(units, "hours")) || (hash_units == hash_hours_ago && !strcmp(units, "hours ago")))) {
577
+ char *suffix = (hash_units == hash_hours_ago)?" ago":"";
578
+
579
size_t h = (size_t)value;
580
size_t d = h / 24;
581
h = h % 24;
582
583
if(d)
565
- snprintfz(value_string, VALUE_STRING_SIZE, "%zud %zuh", d, h);
584
+ snprintfz(value_string, VALUE_STRING_SIZE, "%zud %zuh%s", d, h, suffix);
585
else
567
- snprintfz(value_string, VALUE_STRING_SIZE, "%zuh", h);
586
+ snprintfz(value_string, VALUE_STRING_SIZE, "%zuh%s", h, suffix);
587
}
588
589
else if(unlikely(value_is_null))