11/* ------------------------------------------------------------------------- */
22
33#include <Python.h>
4+ #include <stdint.h>
45
56#ifndef PyVarObject_HEAD_INIT
67#define PyVarObject_HEAD_INIT (type , size ) PyObject_HEAD_INIT(type) size,
2526
2627// 400-year chunks always have 146097 days (20871 weeks).
2728#define DAYS_PER_400_YEARS 146097L
28- #define SECS_PER_400_YEARS (DAYS_PER_400_YEARS * SECS_PER_DAY)
29+ #define SECS_PER_400_YEARS ((int64_t) DAYS_PER_400_YEARS * (int64_t) SECS_PER_DAY)
2930
3031// The number of seconds in an aligned 100-year chunk, for those that
3132// do not begin with a leap year and those that do respectively.
32- const long SECS_PER_100_YEARS [2 ] = {
33- (76L * DAYS_PER_N_YEAR + 24L * DAYS_PER_L_YEAR ) * SECS_PER_DAY ,
34- (75L * DAYS_PER_N_YEAR + 25L * DAYS_PER_L_YEAR ) * SECS_PER_DAY
33+ const int64_t SECS_PER_100_YEARS [2 ] = {
34+ (uint64_t )( 76L * DAYS_PER_N_YEAR + 24L * DAYS_PER_L_YEAR ) * SECS_PER_DAY ,
35+ (uint64_t )( 75L * DAYS_PER_N_YEAR + 25L * DAYS_PER_L_YEAR ) * SECS_PER_DAY
3536};
3637
3738// The number of seconds in an aligned 4-year chunk, for those that
3839// do not begin with a leap year and those that do respectively.
39- const int SECS_PER_4_YEARS [2 ] = {
40+ const int32_t SECS_PER_4_YEARS [2 ] = {
4041 (4 * DAYS_PER_N_YEAR + 0 * DAYS_PER_L_YEAR ) * SECS_PER_DAY ,
4142 (3 * DAYS_PER_N_YEAR + 1 * DAYS_PER_L_YEAR ) * SECS_PER_DAY
4243};
4344
4445// The number of seconds in non-leap and leap years respectively.
45- const int SECS_PER_YEAR [2 ] = {
46+ const int32_t SECS_PER_YEAR [2 ] = {
4647 DAYS_PER_N_YEAR * SECS_PER_DAY ,
4748 DAYS_PER_L_YEAR * SECS_PER_DAY
4849};
4950
5051#define MONTHS_PER_YEAR 12
5152
5253// The month lengths in non-leap and leap years respectively.
53- const int DAYS_PER_MONTHS [2 ][13 ] = {
54+ const int32_t DAYS_PER_MONTHS [2 ][13 ] = {
5455 {-1 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 },
5556 {-1 , 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }
5657};
5758
5859// The day offsets of the beginning of each (1-based) month in non-leap
5960// and leap years respectively.
6061// For example, in a leap year there are 335 days before December.
61- const int MONTHS_OFFSETS [2 ][14 ] = {
62+ const int32_t MONTHS_OFFSETS [2 ][14 ] = {
6263 {-1 , 0 , 31 , 59 , 90 , 120 , 151 , 181 , 212 , 243 , 273 , 304 , 334 , 365 },
6364 {-1 , 0 , 31 , 60 , 91 , 121 , 152 , 182 , 213 , 244 , 274 , 305 , 335 , 366 }
6465};
@@ -88,20 +89,20 @@ const int MONTHS_OFFSETS[2][14] = {
8889
8990PyObject * local_time (PyObject * self , PyObject * args ) {
9091 double unix_time ;
91- int utc_offset ;
92- int year ;
93- long microsecond ;
94- long seconds ;
95- int leap_year ;
96- long sec_per_100years ;
97- long sec_per_4years ;
98- int sec_per_year ;
99- int month ;
100- int day ;
101- int month_offset ;
102- int hour ;
103- int minute ;
104- int second ;
92+ int32_t utc_offset ;
93+ int32_t year ;
94+ int64_t microsecond ;
95+ int64_t seconds ;
96+ int32_t leap_year ;
97+ int64_t sec_per_100years ;
98+ int64_t sec_per_4years ;
99+ int32_t sec_per_year ;
100+ int32_t month ;
101+ int32_t day ;
102+ int32_t month_offset ;
103+ int32_t hour ;
104+ int32_t minute ;
105+ int32_t second ;
105106
106107 if (!PyArg_ParseTuple (args , "di" , & unix_time , & utc_offset )) {
107108 PyErr_SetString (
@@ -111,18 +112,18 @@ PyObject* local_time(PyObject *self, PyObject *args) {
111112 }
112113
113114 year = EPOCH_YEAR ;
114- microsecond = (long ) (unix_time * 1000000 ) % 1000000 ;
115+ microsecond = (int64_t ) (unix_time * 1000000 ) % 1000000 ;
115116 if (microsecond < 0 ) {
116117 microsecond += 1000000 ;
117118 }
118- seconds = (long ) unix_time ;
119+ seconds = (int64_t ) unix_time ;
119120
120121 // Shift to a base year that is 400-year aligned.
121122 if (seconds >= 0 ) {
122123 seconds -= 10957L * SECS_PER_DAY ;
123124 year += 30 ; // == 2000;
124125 } else {
125- seconds += (146097L - 10957L ) * SECS_PER_DAY ;
126+ seconds += (int64_t )( 146097L - 10957L ) * SECS_PER_DAY ;
126127 year -= 370 ; // == 1600;
127128 }
128129
0 commit comments