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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /** Current system clock offset */
  43. signed long time_offset;
  44. /** Days of week (for debugging) */
  45. static const char *weekdays[] = {
  46. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  47. };
  48. /**
  49. * Determine whether or not year is a leap year
  50. *
  51. * @v tm_year Years since 1900
  52. * @v is_leap_year Year is a leap year
  53. */
  54. static int is_leap_year ( int tm_year ) {
  55. int leap_year = 0;
  56. if ( ( tm_year % 4 ) == 0 )
  57. leap_year = 1;
  58. if ( ( tm_year % 100 ) == 0 )
  59. leap_year = 0;
  60. if ( ( tm_year % 400 ) == 100 )
  61. leap_year = 1;
  62. return leap_year;
  63. }
  64. /**
  65. * Calculate number of leap years since 1900
  66. *
  67. * @v tm_year Years since 1900
  68. * @v num_leap_years Number of leap years
  69. */
  70. static int leap_years_to_end ( int tm_year ) {
  71. int leap_years = 0;
  72. leap_years += ( tm_year / 4 );
  73. leap_years -= ( tm_year / 100 );
  74. leap_years += ( ( tm_year + 300 ) / 400 );
  75. return leap_years;
  76. }
  77. /**
  78. * Calculate day of week
  79. *
  80. * @v tm_year Years since 1900
  81. * @v tm_mon Month of year [0,11]
  82. * @v tm_day Day of month [1,31]
  83. */
  84. static int day_of_week ( int tm_year, int tm_mon, int tm_mday ) {
  85. static const uint8_t offset[12] =
  86. { 1, 4, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 };
  87. int pseudo_year = tm_year;
  88. if ( tm_mon < 2 )
  89. pseudo_year--;
  90. return ( ( pseudo_year + leap_years_to_end ( pseudo_year ) +
  91. offset[tm_mon] + tm_mday ) % 7 );
  92. }
  93. /** Days from start of year until start of months (in non-leap years) */
  94. static const uint16_t days_to_month_start[] =
  95. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  96. /**
  97. * Calculate seconds since the Epoch
  98. *
  99. * @v tm Broken-down time
  100. * @ret time Seconds since the Epoch
  101. */
  102. time_t mktime ( struct tm *tm ) {
  103. int days_since_epoch;
  104. int seconds_since_day;
  105. time_t seconds;
  106. /* Calculate day of year */
  107. tm->tm_yday = ( ( tm->tm_mday - 1 ) +
  108. days_to_month_start[ tm->tm_mon ] );
  109. if ( ( tm->tm_mon >= 2 ) && is_leap_year ( tm->tm_year ) )
  110. tm->tm_yday++;
  111. /* Calculate day of week */
  112. tm->tm_wday = day_of_week ( tm->tm_year, tm->tm_mon, tm->tm_mday );
  113. /* Calculate seconds since the Epoch */
  114. days_since_epoch = ( tm->tm_yday + ( 365 * tm->tm_year ) - 25567 +
  115. leap_years_to_end ( tm->tm_year - 1 ) );
  116. seconds_since_day =
  117. ( ( ( ( tm->tm_hour * 60 ) + tm->tm_min ) * 60 ) + tm->tm_sec );
  118. seconds = ( ( ( ( time_t ) days_since_epoch ) * ( ( time_t ) 86400 ) ) +
  119. seconds_since_day );
  120. DBGC ( &weekdays, "TIME %04d-%02d-%02d %02d:%02d:%02d => %lld (%s, "
  121. "day %d)\n", ( tm->tm_year + 1900 ), ( tm->tm_mon + 1 ),
  122. tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, seconds,
  123. weekdays[ tm->tm_wday ], tm->tm_yday );
  124. return seconds;
  125. }