Created
February 22, 2023 01:28
-
-
Save prot0man/ea6e7383331ecde6becbfc74c82626bc to your computer and use it in GitHub Desktop.
Function that substracts days, hours, minutes, and seconds from the current system time in epoch seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <inttypes.h> | |
#include <time.h> | |
time_t days_to_seconds(uint32_t days) | |
{ | |
return days * 24 * 60 * 60; | |
} | |
time_t hours_to_seconds(uint32_t hours) | |
{ | |
return hours * 60 * 60; | |
} | |
time_t minutes_to_seconds(uint32_t minutes) | |
{ | |
return minutes * 60; | |
} | |
time_t timediff(uint32_t days, uint32_t hours, uint32_t minutes, uint32_t seconds) | |
{ | |
time_t current_time = 0, dseconds = 0, hseconds = 0, mseconds = 0; | |
time(¤t_time); | |
dseconds = days_to_seconds(days); | |
hseconds = hours_to_seconds(hours); | |
mseconds = minutes_to_seconds(minutes); | |
return current_time - dseconds - hseconds - mseconds - seconds; | |
} | |
void print_human_time(time_t t) | |
{ | |
struct tm *info = NULL; | |
info = localtime(&t); | |
printf("- %s\n", asctime(info)); | |
} | |
int main(int argc, char **argv) | |
{ | |
time_t current_time = 0; | |
time(¤t_time); | |
time_t result = timediff(1, 1, 1, 1); | |
print_human_time(result); | |
print_human_time(current_time); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment