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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <time.h>
  25. /** @file
  26. *
  27. * Date and time
  28. *
  29. * POSIX:2008 section 4.15 defines "seconds since the Epoch" as an
  30. * abstract measure approximating the number of seconds that have
  31. * elapsed since the Epoch, excluding leap seconds. The formula given
  32. * is
  33. *
  34. * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
  35. * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
  36. * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
  37. *
  38. * This calculation assumes that leap years occur in each year that is
  39. * either divisible by 4 but not divisible by 100, or is divisible by
  40. * 400.
  41. */
  42. /** Days of week (for debugging) */
  43. static const char *weekdays[] = {
  44. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  45. };
  46. /**
  47. * Determine whether or not year is a leap year
  48. *
  49. * @v tm_year Years since 1900
  50. * @v is_leap_year Year is a leap year
  51. */
  52. static int is_leap_year ( int tm_year ) {
  53. int leap_year = 0;
  54. if ( ( tm_year % 4 ) == 0 )
  55. leap_year = 1;
  56. if ( ( tm_year % 100 ) == 0 )
  57. leap_year = 0;
  58. if ( ( tm_year % 400 ) == 100 )
  59. leap_year = 1;
  60. return leap_year;
  61. }
  62. /**
  63. * Calculate number of leap years since 1900
  64. *
  65. * @v tm_year Years since 1900
  66. * @v num_leap_years Number of leap years
  67. */
  68. static int leap_years_to_end ( int tm_year ) {
  69. int leap_years = 0;
  70. leap_years += ( tm_year / 4 );
  71. leap_years -= ( tm_year / 100 );
  72. leap_years += ( ( tm_year + 300 ) / 400 );
  73. return leap_years;
  74. }
  75. /**
  76. * Calculate day of week
  77. *
  78. * @v tm_year Years since 1900
  79. * @v tm_mon Month of year [0,11]
  80. * @v tm_day Day of month [1,31]
  81. */
  82. static int day_of_week ( int tm_year, int tm_mon, int tm_mday ) {
  83. static const uint8_t offset[12] =
  84. { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
  85. int pseudo_year = tm_year;
  86. if ( tm_mon < 2 )
  87. pseudo_year--;
  88. return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
  89. offset[tm_mon] + tm_mday ) % 7 );
  90. }
  91. /** Days from start of year until start of months (in non-leap years) */
  92. static const uint16_t days_to_month_start[] =
  93. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  94. /**
  95. * Calculate seconds since the Epoch
  96. *
  97. * @v tm Broken-down time
  98. * @ret time Seconds since the Epoch
  99. */
  100. time_t mktime ( struct tm *tm ) {
  101. int days_since_epoch;
  102. int seconds_since_day;
  103. time_t seconds;
  104. /* Calculate day of year */
  105. tm->tm_yday = ( ( tm->tm_mday - 1 ) +
  106. days_to_month_start[ tm->tm_mon ] );
  107. if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
  108. tm->tm_yday++;
  109. /* Calculate day of week */
  110. tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );
  111. /* Calculate seconds since the Epoch */
  112. days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
  113. leap_years_to_end ( tm->tm_year - 1 ) );
  114. seconds_since_day =
  115. ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
  116. seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
  117. seconds_since_day );
  118. DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
  119. "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
  120. tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, seconds,
  121. weekdays[ tm->tm_wday ], tm->tm_yday );
  122. return seconds;
  123. }