/contrib/ntp/libntp/ymd2yd.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 37 lines · 25 code · 5 blank · 7 comment · 11 complexity · d789ca103b91677e335e20b5de660752 MD5 · raw file

  1. /*
  2. * ymd2yd - compute the date in the year from y/m/d
  3. */
  4. #include "ntp_fp.h"
  5. #include "ntp_unixtime.h"
  6. #include "ntp_stdlib.h"
  7. /*
  8. * Tables to compute the day of year from yyyymmdd timecode.
  9. * Viva la leap.
  10. */
  11. static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  12. static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  13. int
  14. ymd2yd(
  15. int y,
  16. int m,
  17. int d
  18. )
  19. {
  20. int i, *t;
  21. if (m < 1 || m > 12 || d < 1)
  22. return (-1);
  23. if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))
  24. t = day2tab; /* leap year */
  25. else
  26. t = day1tab; /* not a leap year */
  27. if (d > t[m - 1])
  28. return (-1);
  29. for (i = 0; i < m - 1; i++)
  30. d += t[i];
  31. return d;
  32. }