localtime.c 862 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * localtime - convert a calendar time into broken down time
  3. */
  4. /* $Id$ */
  5. #include <time.h>
  6. #include "loc_time.h"
  7. /* We must be careful, since an int can't represent all the seconds in a day.
  8. * Hence the adjustment of minutes when adding timezone and dst information.
  9. * This assumes that both must be expressable in multiples of a minute.
  10. * Furthermore, it is assumed that both fit into an integer when expressed as
  11. * minutes (this is about 22 days, so this should not cause any problems).
  12. */
  13. struct tm *
  14. localtime(const time_t *timer)
  15. {
  16. struct tm *timep;
  17. unsigned dst;
  18. _tzset();
  19. timep = gmtime(timer); /* tm->tm_isdst == 0 */
  20. timep->tm_min -= _timezone / 60;
  21. timep->tm_sec -= _timezone % 60;
  22. mktime(timep);
  23. dst = _dstget(timep);
  24. if (dst) {
  25. timep->tm_min += dst / 60;
  26. timep->tm_sec += dst % 60;
  27. mktime(timep);
  28. }
  29. return timep;
  30. }