Hey guys so Im trying to create a timestamp with epoch time. So far the Hours minutes seconds and milliseconds have been no problem. however I am at a crossroads with what to do with figuring out the days months and hours. First off there are 2 problems. The number of days in each month varies with no consistent pattern and the matter of tackling leap years. I've been struggling with how to implement a proper system. I suppose I could go the conditional route however perhaps there is some sort of table I can implement as well (a hint given by the instructor.
So far this is the algorithm I have just up to hours if anyone is interested.
28 ExpandedTime* localTime(struct timeval* tv, ExpandedTime* etime)
29 {
30 tzset(); // Corrects timezone
31
32 int epochT = (tv->tv_sec) - timezone; // Epoch seconds with
33 int epochUT = tv->tv_usec; // Timezone correction
34
35 int seconds = epochT % 60;
36 epochT /= 60;
37 etime->et_sec = seconds;
38 etime->et_usec = epochUT;
39
40 int minutes = epochT % 60;
41 epochT /= 60;
42 etime->et_min = minutes;
43
44 int hours = (epochT % 24) + daylight; // Hours with DST correction
45 epochT /= 24;
46 etime->et_hour = hours;
47
48
49 printf("%d,%d,%d\n", seconds, minutes, hours);
50 printf("%d\n", epochUT);
51 printf("%d\n", timezone);
52 printf("%d\n", daylight);
53 return etime;
54
55 }
56
57 char* formatTime(struct timeval* tv, char* buf, size_t len)
58 {
59
60 struct ExpandedTime etime2;
61 localTime(tv, &etime2);
62 snprintf();
63 }
Its unfinished i just am currently stuck on how to implement years months days. if someone could just lead me in the right direction it would be much appreciated.