gmtime.c 946 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * gmtime - convert the calendar time into broken down time
  3. */
  4. /* $Id$ */
  5. #include <time.h>
  6. #include <limits.h>
  7. #include "loc_time.h"
  8. struct tm *
  9. gmtime(register const time_t *timer)
  10. {
  11. static struct tm br_time;
  12. register struct tm *timep = &br_time;
  13. time_t tim = *timer;
  14. register unsigned long dayclock, dayno;
  15. int year = EPOCH_YR;
  16. dayclock = (unsigned long)tim % SECS_DAY;
  17. dayno = (unsigned long)tim / SECS_DAY;
  18. timep->tm_sec = dayclock % 60;
  19. timep->tm_min = (dayclock % 3600) / 60;
  20. timep->tm_hour = dayclock / 3600;
  21. timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
  22. while (dayno >= YEARSIZE(year)) {
  23. dayno -= YEARSIZE(year);
  24. year++;
  25. }
  26. timep->tm_year = year - YEAR0;
  27. timep->tm_yday = dayno;
  28. timep->tm_mon = 0;
  29. while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) {
  30. dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon];
  31. timep->tm_mon++;
  32. }
  33. timep->tm_mday = dayno + 1;
  34. timep->tm_isdst = 0;
  35. return timep;
  36. }