You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <time.h>
  21. /** @file
  22. *
  23. * Date and time
  24. *
  25. * POSIX:2008 section 4.15 defines "seconds since the Epoch" as an
  26. * abstract measure approximating the number of seconds that have
  27. * elapsed since the Epoch, excluding leap seconds. The formula given
  28. * is
  29. *
  30. * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
  31. * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
  32. * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
  33. *
  34. * This calculation assumes that leap years occur in each year that is
  35. * either divisible by 4 but not divisible by 100, or is divisible by
  36. * 400.
  37. */
  38. /** Days of week (for debugging) */
  39. static const char *weekdays[] = {
  40. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  41. };
  42. /**
  43. * Determine whether or not year is a leap year
  44. *
  45. * @v tm_year Years since 1900
  46. * @v is_leap_year Year is a leap year
  47. */
  48. static int is_leap_year ( int tm_year ) {
  49. int leap_year = 0;
  50. if ( ( tm_year % 4 ) == 0 )
  51. leap_year = 1;
  52. if ( ( tm_year % 100 ) == 0 )
  53. leap_year = 0;
  54. if ( ( tm_year % 400 ) == 100 )
  55. leap_year = 1;
  56. return leap_year;
  57. }
  58. /**
  59. * Calculate number of leap years since 1900
  60. *
  61. * @v tm_year Years since 1900
  62. * @v num_leap_years Number of leap years
  63. */
  64. static int leap_years_to_end ( int tm_year ) {
  65. int leap_years = 0;
  66. leap_years += ( tm_year / 4 );
  67. leap_years -= ( tm_year / 100 );
  68. leap_years += ( ( tm_year + 300 ) / 400 );
  69. return leap_years;
  70. }
  71. /**
  72. * Calculate day of week
  73. *
  74. * @v tm_year Years since 1900
  75. * @v tm_mon Month of year [0,11]
  76. * @v tm_day Day of month [1,31]
  77. */
  78. static int day_of_week ( int tm_year, int tm_mon, int tm_mday ) {
  79. static const uint8_t offset[12] =
  80. { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
  81. int pseudo_year = tm_year;
  82. if ( tm_mon < 2 )
  83. pseudo_year--;
  84. return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
  85. offset[tm_mon] + tm_mday ) % 7 );
  86. }
  87. /** Days from start of year until start of months (in non-leap years) */
  88. static const uint16_t days_to_month_start[] =
  89. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  90. /**
  91. * Calculate seconds since the Epoch
  92. *
  93. * @v tm Broken-down time
  94. * @ret time Seconds since the Epoch
  95. */
  96. time_t mktime ( struct tm *tm ) {
  97. int days_since_epoch;
  98. int seconds_since_day;
  99. time_t seconds;
  100. /* Calculate day of year */
  101. tm->tm_yday = ( ( tm->tm_mday - 1 ) +
  102. days_to_month_start[ tm->tm_mon ] );
  103. if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
  104. tm->tm_yday++;
  105. /* Calculate day of week */
  106. tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );
  107. /* Calculate seconds since the Epoch */
  108. days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
  109. leap_years_to_end ( tm->tm_year - 1 ) );
  110. seconds_since_day =
  111. ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
  112. seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
  113. seconds_since_day );
  114. DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
  115. "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
  116. tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, seconds,
  117. weekdays[ tm->tm_wday ], tm->tm_yday );
  118. return seconds;
  119. }