#! /usr/bin/env python # def doomsday_gregorian ( y, m, d ): #*****************************************************************************80 # ## DOOMSDAY_GREGORIAN: weekday given any date in Gregorian calendar. # # Discussion: # # This procedure does not include any procedure to switch to the Julian # calendar for dates early enough that that calendar was used instead. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 August 2015 # # Author: # # John Burkardt # # Reference: # # John Conway, # Tomorrow is the Day After Doomsday, # Eureka, # Volume 36, October 1973, pages 28-31. # # Parameters: # # Input, integer ( kind = 4 ) Y, M, D, the year, month and day of the date. # Note that the year must be positive. # # Output, integer ( kind = 4 ) W, the weekday of the date. # import numpy as np from i4_wrap import i4_wrap from year_is_leap_gregorian import year_is_leap_gregorian anchor = np.array ( [ 1, 6, 4, 3 ] ) mdoom = np.array ( [ 3, 28, 0, 4, 9, 6, 11, 8, 5, 10, 7, 12 ] ) # # Refuse to handle Y <= 0. # if ( y <= 0 ): print ( '' ) print ( 'DOOMSDAY_GREGORIAN - Fatal error!' ) print ( ' Y <= 0.' ) exit ( 'DOOMSDAY_GREGORIAN - Fatal error!' ) # # Force Y to be an integer value. # y = int ( y ) # # Determine the century C. # c = y // 100 # # Determine the last two digits of the year, YY # yy = ( y % 100 ) # # Divide the last two digits of the year by 12. # yy = ( y % 100 ) yy12d = ( yy // 12 ) yy12r = ( yy % 12 ) yy12r4d = ( yy12r // 4 ) drd = yy12d + yy12r + yy12r4d drdr = ( drd % 7 ) t = ( ( c - 1 ) % 4 ) ydoom = anchor[t] + drdr ydoom = i4_wrap ( ydoom, 1, 7 ) # # If M = 1 or 2, and leap year, add 1. # if ( ( m == 1 or m == 2 ) and year_is_leap_gregorian ( y ) ): l = 1 else: l = 0 w = ydoom + ( d - mdoom[m-1] - l ) w = i4_wrap ( w, 1, 7 ) return w def doomsday_gregorian_test01 ( ): #*****************************************************************************80 # ## DOOMSDAY_GREGORIAN_TEST01 tests DOOMSDAY_GREGORIAN against a couple of test dates. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 August 2015 # # Author: # # John Burkardt # import platform from weekday_to_name_common import weekday_to_name_common print ( ' ' ) print ( 'DOOMSDAY_GREGORIAN_TEST01' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Try a couple selected dates.' ) print ( ' ' ) print ( ' YYYY MM DD Weekday Weekday' ) print ( ' Tabulated Computed' ) print ( ' ' ) y = 1989 m = 7 d = 13 w = doomsday_gregorian ( y, m, d ) s1 = weekday_to_name_common ( w ) s2 = 'Thursday' print ( ' %4d %2d %2d %10s %10s' % ( y, m, d, s1, s2 ) ) y = 2012 m = 5 d = 26 w = doomsday_gregorian ( y, m, d ) s1 = weekday_to_name_common ( w ) s2 = 'Saturday' print ( ' %4d %2d %2d %10s %10s' % ( y, m, d, s1, s2 ) ) # # Terminate. # print ( '' ) print ( 'DOOMSDAY_GREGORIAN_TEST01' ) print ( ' Normal end of execution.' ) return def doomsday_gregorian_test02 ( ): #*****************************************************************************80 # ## DOOMSDAY_GREGORIAN_TEST02 tests DOOMSDAY_GREGORIAN against a number of known values. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 May 2012 # # Author: # # John Burkardt # import platform from weekday_to_name_common import weekday_to_name_common from weekday_values import weekday_values print ( ' ' ) print ( 'DOOMSDAY_GREGORIAN_TEST02' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' WEEKDAY_VALUES supplies a list of dates and weekdays.' ) print ( ' ' ) print ( ' YYYY MM DD Weekday Weekday' ) print ( ' Tabulated Computed' ) print ( ' ' ) n_data = 0 while ( True ): n_data, y, m, d, w1 = weekday_values ( n_data ) if ( n_data <= 0 ): break # # The transition from Julian to Gregorian calendars occurred in 1582 # (for some people). The data in "WEEKDAY_VALUES" before the transition # is stored in Julian format, which DOOMSDAY_GREGORIAN can't handle. # So let's just refuse to handle 1582 or earlier# # if ( y <= 1582 ): continue w2 = doomsday_gregorian ( y, m, d ) s1 = weekday_to_name_common ( w1 ) s2 = weekday_to_name_common ( w2 ) print ( ' %4d %2d %2d %10s %10s' % ( y, m, d, s1, s2 ) ) # # Terminate. # print ( '' ) print ( 'DOOMSDAY_GREGORIAN_TEST02' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) doomsday_gregorian_test01 ( ) doomsday_gregorian_test02 ( ) timestamp ( )