0

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.

PresidentRFresh
  • 93
  • 1
  • 2
  • 10
  • 1
    Are you doing this as an exercise, or because you couldn't find a suitable class that already does this? – omer schleifer Mar 14 '13 at 16:58
  • it is an exercise...theres probably a much easier way to go about this however I am limited to the exercise restrictions. If it helps here is the hint he gives us for the exercise: "It is easy to determine the number of days since the UNIX Epoch. But beyond that it becomes complicated. The Key is to realize the next larger uniform cycle is not months or years but four years. For units between days and four years you need special cases; these can be coded with conditionals or tables" – PresidentRFresh Mar 14 '13 at 17:03
  • Goes on to say : "You probably want a table containing days per month and month names, or perhaps you'll want two tables, one for leap years and one for non leap years. You may perhaps combine the two tables or have a single table for the 4 year cycle. In any case once you determine which four year cycle you're in use the number of remaining days to walk through the tables to determine the month name and day of month." Is he referring to structures when he says tables? im just confused at to what he's getting at here. – PresidentRFresh Mar 14 '13 at 17:05
  • *"You probably want a table containing days per month"* Have you tried creating that? – Drew Dormann Mar 14 '13 at 17:07
  • Possible duplicate of [Math to convert seconds since 1970 into date and vice versa](https://stackoverflow.com/questions/7960318/math-to-convert-seconds-since-1970-into-date-and-vice-versa) – lmiguelvargasf Mar 17 '19 at 00:52

1 Answers1

0

Convert the Unix epoch date (1/1/1970 00:00) to its equivalent Julian Date, convert the time from seconds to days and add the result to the epoch's Julian Date. This gives you the Julian date of the desired time. Finally, convert the Julian date back into date and time.

You can find the appropriate formulas to convert to and from the julian date on the web.

sizzzzlerz
  • 4,277
  • 3
  • 27
  • 35