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.8KB

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