]> gitweb.hhaalo.de Git - feierabend.git/commitdiff
refactor use stack vars for feierabend
authorBastian Dehn <hhaalo@arcor.de>
Sat, 14 Feb 2026 07:47:48 +0000 (08:47 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 14 Feb 2026 07:47:48 +0000 (08:47 +0100)
src/feierabend.c

index 542f862e826dab95e67c5cf3984edbc1415150e8..0d2d9a38e54d109703343edf8666298c308f7b61 100644 (file)
 
 #define MAX_TIME_STR_LENGTH 7
 
-void _print_german_long_date(const struct tm* time_info)
+void _print_german_long_date(const struct tm time_info)
 {
-       char* weekday = get_weekday_str(time_info->tm_wday);
+       char* weekday = get_weekday_str(time_info.tm_wday);
 
        printf("%s %02d.%02d.%04d %02d:%02d",
                weekday,
-               time_info->tm_mday,
-               time_info->tm_mon + 1,
-               time_info->tm_year + 1900,
-               time_info->tm_hour,
-               time_info->tm_min);
+               time_info.tm_mday,
+               time_info.tm_mon + 1,
+               time_info.tm_year + 1900,
+               time_info.tm_hour,
+               time_info.tm_min);
 
        free(weekday);
        weekday = NULL;
 }
 
-time_t _set_time(time_t value, struct tm* tm_value, int hour, int minutes)
-{
-       value = time(NULL);
-
-       const struct tm* tm_tmp = localtime(&value);
-       memcpy(tm_value, tm_tmp, sizeof(struct tm));
-       tm_value->tm_hour = hour;
-       tm_value->tm_min = minutes;
-
-       return mktime(tm_value);
-}
-
-void _print_work_begin(const feierabend* fabend)
+void _print_work_begin(const struct tm begin_tm)
 {
        printf("%-27s", "Arbeitsbeginn:");
-       _print_german_long_date(fabend->begin_tm);
+       _print_german_long_date(begin_tm);
        printf("\n");
 }
 
-void _print_time_now(const feierabend* fabend)
+void _print_time_now(const struct tm now_tm)
 {
        printf("%-27s", "Aktuelle Zeit:");
-       _print_german_long_date(fabend->now_tm);
+       _print_german_long_date(now_tm);
        printf("\n");
 }
 
-void _print_work_end(const feierabend* fabend)
+void _print_work_end(const struct tm work_end_tm)
 {
        printf("%s %02d:%02d %-9s", "Arbeitsende",
                SOLL_HOUR,
                SOLL_MINUTES,
                "Std:");
-       _print_german_long_date(fabend->work_end_tm);
+       _print_german_long_date(work_end_tm);
        printf("\n");
 }
 
-void _print_max_work_end(const feierabend* fabend)
+void _print_max_work_end(const struct tm max_work_end_tm)
 {
        printf("%-27s", "Arbeitsende 10:00 Std:");
-       _print_german_long_date(fabend->max_work_end_tm);
+       _print_german_long_date(max_work_end_tm);
        printf("\n");
 }
 
-void _print_brutto_worktime(feierabend* fabend)
+void _print_brutto_worktime(const time_t begin, const time_t end)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
        if (timestr == NULL)
                return;
 
-       fabend->worktime = get_brutto_worktime(fabend->begin, fabend->now);
-       get_time_str(fabend->worktime, timestr);
+       const time_t butto_worktime = get_brutto_worktime(begin, end);
+       get_time_str(butto_worktime, timestr);
        printf("%-26s%6s\n", "Brutto Arbeitzeit:", timestr);
 
        free(timestr);
+       timestr = NULL;
 }
 
-void _print_netto_worktime(feierabend* fabend)
+void _print_netto_worktime(const time_t begin, const time_t end)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
        if (timestr == NULL)
                return;
 
-       fabend->worktime = get_current_worktime(fabend->begin, fabend->now);
-       get_time_str(fabend->worktime, timestr);
+       const time_t netto_worktime = get_current_worktime(begin, end);
+       get_time_str(netto_worktime, timestr);
        printf("%-26s%6s\n", "Netto Arbeitzeit:", timestr);
 
        free(timestr);
        timestr = NULL;
 }
 
-void _print_current_breaktime(feierabend* fabend)
+void _print_current_breaktime(const time_t begin, const time_t end)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
        if (timestr == NULL)
                return;
 
-       fabend->worktime = get_brutto_worktime(fabend->begin, fabend->now);
-       fabend->worktime = get_break_time(fabend->worktime);
-       get_time_str(fabend->worktime, timestr);
+       const time_t brutto_worktime = get_brutto_worktime(begin, end);
+       const time_t break_time = get_break_time(brutto_worktime);
+       get_time_str(break_time, timestr);
        printf("%-26s%6s\n", "Aktuelle Pausenzeit:", timestr);
 
        free(timestr);
        timestr = NULL;
 }
 
-void _print_time_to_end_worktime(feierabend* fabend)
+void _print_time_to_end_worktime(const time_t begin, const time_t end)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
        if (timestr == NULL)
                return;
 
-       fabend->worktime = get_current_worktime_diff_to_end_eight_hour(fabend->begin, fabend->now);
-       get_time_str(fabend->worktime, timestr);
+       const time_t diff_to_end = get_current_worktime_diff_to_end_eight_hour(begin, end);
+       get_time_str(diff_to_end, timestr);
        printf("Arbeitzeit bis %02d:%02d Std: %6s\n",
                SOLL_HOUR,
                SOLL_MINUTES,
@@ -131,15 +120,15 @@ void _print_time_to_end_worktime(feierabend* fabend)
        timestr = NULL;
 }
 
-void _print_time_to_max_end_worktime(feierabend* fabend)
+void _print_time_to_max_end_worktime(const time_t begin, const time_t end)
 {
        char* timestr = malloc(MAX_TIME_STR_LENGTH);
 
        if (timestr == NULL)
                return;
 
-       fabend->worktime = get_current_worktime_diff_to_end_ten_hour(fabend->begin, fabend->now);
-       get_time_str(fabend->worktime, timestr);
+       const time_t diff_to_end = get_current_worktime_diff_to_end_ten_hour(begin, end);
+       get_time_str(diff_to_end, timestr);
        printf("Arbeitzeit bis 10:00 Std: %6s\n", timestr);
 
        free(timestr);
@@ -167,46 +156,52 @@ int main(int argc, const char* argv[])
                }
        }
 
-       feierabend* fabend = feierabend_init();
-
        printf("Version: %s\n\n", PROJECT_VERSION);
 
-       fabend->begin = _set_time(fabend->begin, fabend->begin_tm,  atoi(argv[1]),  atoi(argv[2]));
+       const time_t now = time(NULL);
+       const struct tm* tmp_now_tm = localtime(&now);
+
+       struct tm begin_tm;
+       struct tm end_tm;
+       memcpy(&begin_tm, tmp_now_tm, sizeof(struct tm));
+       memcpy(&end_tm, tmp_now_tm, sizeof(struct tm));
+
+       begin_tm.tm_hour = atoi(argv[1]);
+       begin_tm.tm_min = atoi(argv[2]);
+       if (argc == 5) {
+               end_tm.tm_hour = atoi(argv[3]);
+               end_tm.tm_min = atoi(argv[4]);
+       }
 
-       fabend->now = time(NULL);
-       const struct tm* tmp_now = localtime(&fabend->now);
-       memcpy(fabend->now_tm, tmp_now, sizeof(struct tm));
+       const time_t begin = mktime(&begin_tm);
+       const time_t end = mktime(&end_tm);
 
-       if (argc == 5)
-               fabend->now = _set_time(fabend->now, fabend->now_tm, atoi(argv[3]), atoi(argv[4]));
+       const time_t worktime_eight_end = get_eight_hour_end_worktime(begin);
+       const time_t worktime_max_end = get_ten_hour_end_worktime(begin);
 
-       fabend->worktime = get_eight_hour_end_worktime(fabend->begin);
-       const struct tm* tmp_work_end = localtime(&fabend->worktime);
-       memcpy(fabend->work_end_tm, tmp_work_end, sizeof(struct tm));
+       const struct tm* tmp_work_eight_end = localtime(&worktime_eight_end);
+       struct tm work_eight_end_tm;
+       memcpy(&work_eight_end_tm, tmp_work_eight_end, sizeof(struct tm));
 
-       fabend->worktime = get_ten_hour_end_worktime(fabend->begin);
-       const struct tm* tmp_max_work_end = localtime(&fabend->worktime);
-       memcpy(fabend->max_work_end_tm, tmp_max_work_end, sizeof(struct tm));
+       const struct tm* tmp_max_work_end = localtime(&worktime_max_end);
+       struct tm max_work_end_tm;
+       memcpy(&max_work_end_tm, tmp_max_work_end, sizeof(struct tm));
 
-       _print_time_now(fabend);
+       _print_time_now(end_tm);
        printf("\n");
 
-       _print_work_begin(fabend);
-       _print_work_end(fabend);
-       _print_max_work_end(fabend);
+       _print_work_begin(begin_tm);
+       _print_work_end(work_eight_end_tm);
+       _print_max_work_end(max_work_end_tm);
        printf("\n");
 
-       _print_brutto_worktime(fabend);
-       _print_netto_worktime(fabend);
-       _print_current_breaktime(fabend);
+       _print_brutto_worktime(begin, end);
+       _print_netto_worktime(begin, end);
+       _print_current_breaktime(begin, end);
        printf("\n");
 
-       _print_time_to_end_worktime(fabend);
-       _print_time_to_max_end_worktime(fabend);
-
-       feierabend_free(fabend);
-       free(fabend);
-       fabend = NULL;
+       _print_time_to_end_worktime(begin, end);
+       _print_time_to_max_end_worktime(begin, end);
 
        return 0;
 }
\ No newline at end of file