Add day filter param to day report

This commit is contained in:
Simon Lieb 2022-05-02 20:38:49 +02:00
parent 3b6970481a
commit 7c8702bb63
3 changed files with 41 additions and 16 deletions

View file

@ -12,9 +12,9 @@ Build & Install
Usage Usage
----- -----
$ stt -a task # start task $ stt -a task # start task
$ stt -s # stop task $ stt -s # stop task
$ stt [-l] # print report $ stt [-l [Y-m-d]] # print report
task: task task: task
started at: Wed Oct 2 19:04:04 2019 started at: Wed Oct 2 19:04:04 2019
ended at: Wed Oct 2 19:05:04 2019 ended at: Wed Oct 2 19:05:04 2019

5
stt.1
View file

@ -8,7 +8,7 @@ stt \- Simple time tracker
.RB [ \-a .RB [ \-a
.IR task " |" .IR task " |"
.RB \-s " |" .RB \-s " |"
.RB \-l ] .RB \-l [Y-m-d]]
.SH DESCRIPTION .SH DESCRIPTION
.B stt .B stt
keeps track of tasks during a work day. keeps track of tasks during a work day.
@ -24,9 +24,10 @@ Registers given task as active. Will also stop previous active task.
.B \-s .B \-s
Stops current active task. Stops current active task.
.TP .TP
.B \-l .B \-l [Y-m-d]
Prints day report, for each tasks name, start time, end time or state if Prints day report, for each tasks name, start time, end time or state if
still running and duration (in decimal hours) is displayed. still running and duration (in decimal hours) is displayed.
Also accept a date as filter, default display today report.
.TP .TP
.B \-h .B \-h
Prints synopsis help. Prints synopsis help.

46
stt.c
View file

@ -2,6 +2,14 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#endif #endif
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
@ -48,7 +56,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); int timesnode_print(struct timesnode *, time_t aftertime, time_t beforetime);
void timesnode_free(struct timesnode *); void timesnode_free(struct timesnode *);
void void
@ -267,7 +275,7 @@ timesnode_stop(struct timesnode * p, time_t endtime)
} }
int int
timesnode_print(struct timesnode * p, time_t aftertime) timesnode_print(struct timesnode * p, time_t aftertime, time_t beforetime)
{ {
time_t *nowtime; time_t *nowtime;
int duration = 0; int duration = 0;
@ -277,9 +285,9 @@ timesnode_print(struct timesnode * p, time_t aftertime)
return 0; return 0;
} }
if (p->left != NULL) { if (p->left != NULL) {
totalDuration += timesnode_print(p->left, aftertime); totalDuration += timesnode_print(p->left, aftertime, beforetime);
} }
if (p->starttime > aftertime || p->endtime > aftertime || p->endtime == 0) { if ((p->starttime > aftertime && p->starttime < beforetime) && (p->endtime < beforetime || p->endtime == 0)) {
printf("task: %s\n", p->task); printf("task: %s\n", p->task);
printf("started at: %s", ctime(&p->starttime)); printf("started at: %s", ctime(&p->starttime));
@ -303,7 +311,7 @@ timesnode_print(struct timesnode * p, time_t aftertime)
totalDuration += duration; totalDuration += duration;
} }
if (p->right != NULL) { if (p->right != NULL) {
totalDuration += timesnode_print(p->right, aftertime); totalDuration += timesnode_print(p->right, aftertime, beforetime);
} }
return totalDuration; return totalDuration;
} }
@ -328,14 +336,16 @@ main(int argc, char *argv[])
{ {
unsigned int start, stop, list; unsigned int start, stop, list;
char *task; char *task;
char *datefilter;
FILE *fp; FILE *fp;
struct timesnode *timestree; struct timesnode *timestree;
time_t *nowtime; time_t *nowtime;
int changed; int changed;
struct tm today; struct tm startfilter, endfilter;
timestree = NULL; timestree = NULL;
task = NULL; task = NULL;
datefilter = NULL;
changed = start = stop = 0; changed = start = stop = 0;
list = 1; list = 1;
@ -358,6 +368,8 @@ case 'l': /* list task */
start = 0; start = 0;
stop = 0; stop = 0;
list = 1; list = 1;
datefilter = ARGF();
break; break;
case 'v': case 'v':
fprintf(stderr, "%s-" VERSION " © 2016 Simon Lieb, see LICENSE for details\n", argv0); fprintf(stderr, "%s-" VERSION " © 2016 Simon Lieb, see LICENSE for details\n", argv0);
@ -388,13 +400,25 @@ default:
writetimes(fp, timestree); writetimes(fp, timestree);
} }
if (list) { if (list) {
localtime_r(nowtime, &today); if (datefilter != NULL) {
strptime(datefilter, "%Y-%m-%d", &startfilter);
strptime(datefilter, "%Y-%m-%d", &endfilter);
} else {
localtime_r(nowtime, &startfilter);
localtime_r(nowtime, &endfilter);
}
today.tm_sec = 0; startfilter.tm_sec = 0;
today.tm_min = 0; startfilter.tm_min = 0;
today.tm_hour = 0; startfilter.tm_hour = 0;
//startfilter.tm_mday-=4;
printf("total: %.2f\n", timesnode_print(timestree, mktime(&today)) / 3600.0); endfilter.tm_sec = 0;
endfilter.tm_min = 0;
endfilter.tm_hour = 0;
endfilter.tm_mday++;
printf("total: %.2f\n", timesnode_print(timestree, mktime(&startfilter), mktime(&endfilter)) / 3600.0);
} }
fclose(fp); fclose(fp);