Fix times sum rounding errors

This commit is contained in:
Simon Lieb 2022-05-19 19:31:07 +02:00
parent d96fe7c813
commit 8c03ec3bdb
2 changed files with 13 additions and 10 deletions

View file

@ -8,7 +8,7 @@ PREFIX = /usr/local
# includes and libs # includes and libs
INCS = INCS =
LIBS = LIBS = -lm
# flags # flags
CPPFLAGS = -DVERSION=\"${VERSION}\" CPPFLAGS = -DVERSION=\"${VERSION}\"

21
stt.c
View file

@ -18,6 +18,7 @@
#include <pwd.h> #include <pwd.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <math.h>
#include "arg.h" #include "arg.h"
@ -56,7 +57,7 @@ int timesnode_heigh(struct timesnode *);
int timesnode_getbalance(struct timesnode *); int timesnode_getbalance(struct timesnode *);
struct timesnode *timesnode_rightrotate(struct timesnode *); struct timesnode *timesnode_rightrotate(struct timesnode *);
struct timesnode *timesnode_leftrotate(struct timesnode *); struct timesnode *timesnode_leftrotate(struct timesnode *);
int timesnode_print(struct timesnode *, time_t aftertime, time_t beforetime, int showinprogress); float timesnode_print(struct timesnode *, time_t aftertime, time_t beforetime, int showinprogress);
void timesnode_free(struct timesnode *); void timesnode_free(struct timesnode *);
void void
@ -274,12 +275,12 @@ timesnode_stop(struct timesnode * p, time_t endtime)
return p; return p;
} }
int float
timesnode_print(struct timesnode * p, time_t aftertime, time_t beforetime, int showinprogress) timesnode_print(struct timesnode * p, time_t aftertime, time_t beforetime, int showinprogress)
{ {
time_t *nowtime; time_t *nowtime;
int duration = 0; float duration = 0.0;
int totalDuration = 0; float totalDuration = 0.0;
if (p == NULL) { if (p == NULL) {
return 0; return 0;
@ -298,17 +299,19 @@ timesnode_print(struct timesnode * p, time_t aftertime, time_t beforetime, int s
if (p->endtime != 0) { if (p->endtime != 0) {
printf("ended at: %s", ctime(&p->endtime)); printf("ended at: %s", ctime(&p->endtime));
duration = difftime(p->endtime, p->starttime); duration = difftime(p->endtime, p->starttime) / 3600.0;
duration = roundf(100.0 * duration) / 100.0;
printf("duration(hours): %.2f\n\n", duration / 3600.0); printf("duration(hours): %.2f\n\n", duration);
} else { } else {
nowtime = malloc(sizeof(time_t)); nowtime = malloc(sizeof(time_t));
time(nowtime); time(nowtime);
printf("still running\n"); printf("still running\n");
duration = difftime(*nowtime, p->starttime); duration = difftime(*nowtime, p->starttime) / 3600.0;
duration = roundf(100.0 * duration) / 100.0;
printf("duration(hours): %.2f\n\n", duration / 3600.0); printf("duration(hours): %.2f\n\n", duration);
free(nowtime); free(nowtime);
} }
@ -434,7 +437,7 @@ default:
endfilter.tm_hour = 0; endfilter.tm_hour = 0;
endfilter.tm_mday++; endfilter.tm_mday++;
printf("total: %.2f\n", timesnode_print(timestree, mktime(&startfilter), mktime(&endfilter), mktime(&startfilter) == mktime(&today)) / 3600.0); printf("total: %.2f\n", timesnode_print(timestree, mktime(&startfilter), mktime(&endfilter), mktime(&startfilter) == mktime(&today)));
} }
fclose(fp); fclose(fp);