Skip to content

Commit

Permalink
Add strict type casting / remove tc_timeval_long
Browse files Browse the repository at this point in the history
  • Loading branch information
hnw committed Jul 3, 2017
1 parent bff5a2f commit e3618d2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension=timecop.so
## SYSTEM REQUIREMENTS

- OS: Linux, macOS
- PHP: 5.3.1 - 7.1.x (might work on 5.2.x and 5.3.0, but not tested enough)
- PHP: 5.3.1 - 7.2.x (might work on 5.2.x and 5.3.0, but not tested enough)
- SAPI: Apache, CLI
- Other SAPIs are not tested, but there is no SAPI-dependent code.
- non-ZTS(recommended), ZTS
Expand Down
6 changes: 5 additions & 1 deletion php_timecop.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ ZEND_BEGIN_MODULE_GLOBALS(timecop)
tc_timeval freezed_time;
tc_timeval travel_origin;
tc_timeval travel_offset;
tc_timeval_long scaling_factor;
#if PHP_VERSION_ID >= 70000
zend_long scaling_factor;
#else
long scaling_factor;
#endif
zend_class_entry *ce_DateTimeZone;
zend_class_entry *ce_DateTimeInterface;
zend_class_entry *ce_DateTime;
Expand Down
25 changes: 21 additions & 4 deletions tc_timeval.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ SOFTWARE.

int tc_timeval_add(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2)
{
tc_timeval_long sec, usec;
#if PHP_MAJOR_VERSION >= 7
zend_long sec, usec;
#else
long sec, usec;
#endif
usec = arg1->usec + arg2->usec;
sec = arg1->sec + arg2->sec;
if (usec < 0) {
Expand All @@ -47,7 +51,11 @@ int tc_timeval_add(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *ar
}
int tc_timeval_sub(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2)
{
tc_timeval_long sec, usec;
#if PHP_MAJOR_VERSION >= 7
zend_long sec, usec;
#else
long sec, usec;
#endif
usec = arg1->usec - arg2->usec;
sec = arg1->sec - arg2->sec;
if (usec < 0) {
Expand All @@ -66,9 +74,18 @@ int tc_timeval_sub(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *ar

return 0;
}
int tc_timeval_mul(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval_long arg2)

#if PHP_MAJOR_VERSION >= 7
int tc_timeval_mul(tc_timeval *ret, const tc_timeval *arg1, const zend_long arg2)
#else
int tc_timeval_mul(tc_timeval *ret, const tc_timeval *arg1, const long arg2)
#endif
{
tc_timeval_long sec, usec;
#if PHP_MAJOR_VERSION >= 7
zend_long sec, usec;
#else
long sec, usec;
#endif
usec = arg1->usec * arg2;
sec = arg1->sec * arg2;
if (usec < 0) {
Expand Down
16 changes: 10 additions & 6 deletions tc_timeval.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ SOFTWARE.
# define USEC_PER_SEC 1000000
#endif

typedef struct _tc_timeval {
#if PHP_MAJOR_VERSION >= 7
typedef zend_long tc_timeval_long;
zend_long sec;
zend_long usec;
#else
typedef long tc_timeval_long;
long sec;
long usec;
#endif

typedef struct _tc_timeval {
tc_timeval_long sec;
tc_timeval_long usec;
} tc_timeval;


int tc_timeval_add(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2);
int tc_timeval_sub(tc_timeval *ret, const tc_timeval *arg1, const tc_timeval *arg2);
#if PHP_MAJOR_VERSION >= 7
int tc_timeval_mul(tc_timeval *ret, const tc_timeval *arg1, const zend_long arg2);
#else
int tc_timeval_mul(tc_timeval *ret, const tc_timeval *arg1, const long arg2);
#endif

#endif /* TC_TIMEVAL_H */

Expand Down
2 changes: 1 addition & 1 deletion timecop_php5.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ static int get_current_time(tc_timeval *now TSRMLS_DC)
if (ts == -1) {
ret = -1;
} else {
now->sec = ts;
now->sec = (long)ts;
now->usec = 0;
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions timecop_php7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,15 +1231,15 @@ static int get_current_time(tc_timeval *now)
struct timeval tv;
ret = gettimeofday(&tv, NULL);
if (ret == 0) {
now->sec = (long)tv.tv_sec;
now->usec = (long)tv.tv_usec;
now->sec = (zend_long)tv.tv_sec;
now->usec = (zend_long)tv.tv_usec;
}
#else
time_t ts = time(NULL);
if (ts == -1) {
ret = -1;
} else {
now->sec = ts;
now->sec = (zend_long)ts;
now->usec = 0;
}
#endif
Expand Down

0 comments on commit e3618d2

Please sign in to comment.