Skip to content

Commit

Permalink
Fixed timelib_hms[f]_to_decimal_hour for hour=0 case
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Nov 18, 2021
1 parent 435326a commit b8885ed
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ C_TESTS=tests/c/timelib_get_current_offset_test.o tests/c/timelib_decimal_hour.o
tests/c/warn_on_slim.o tests/c/parse_posix.o tests/c/transitions.o \
tests/c/parse_tz.o tests/c/render.o tests/c/create_ts_from_string.o \
tests/c/parse_date.o tests/c/php-rfc.o tests/c/diff.o tests/c/interval.o \
tests/c/timezones_same.o tests/c/diff_days.o
tests/c/timezones_same.o tests/c/diff_days.o \
tests/c/timelib_hmsf_to_decimal_hour.o

TEST_BINARIES=${MANUAL_TESTS} ${AUTO_TESTS}

Expand Down
8 changes: 7 additions & 1 deletion tests/c/diff_days.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ TEST(diff_days, a_year)
->setTimezone(new DateTimeZone('UTC'));
$second = new DateTime('2018-07-02 00:00:00.000000 America/Toronto');
*/
TEST(diff_days, php81458)
TEST(diff_days, php81458_1)
{
test_parse_with_tz("2018-07-01 04:00 GMT+0000", "2018-07-02 00:00", NULL, "America/Toronto");
LONGS_EQUAL(1, days);
}

TEST(diff_days, php81458_2)
{
test_parse_with_tz("2018-12-01 00:00", "2018-12-02 00:01", "UTC", "UTC");
LONGS_EQUAL(1, days);
}

TEST(diff_days, php78452)
{
test_parse_with_tz("2019-09-24 11:47:24", "2019-08-21 12:47:24", "Asia/Tehran", "Asia/Tehran");
Expand Down
87 changes: 87 additions & 0 deletions tests/c/timelib_hmsf_to_decimal_hour.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "CppUTest/TestHarness.h"
#include "timelib.h"
#include <stdio.h>
#include <string.h>


TEST_GROUP(hmsf_to_decimal_hour)
{
double result;

void test_convert(int hour, int min, int sec, int us)
{
timelib_hmsf_to_decimal_hour(hour, min, sec, us, &result);
}
};

TEST(hmsf_to_decimal_hour, zero)
{
test_convert(0, 0, 0, 0);
DOUBLES_EQUAL(0, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, smallest_positive)
{
test_convert(0, 0, 0, 1);
DOUBLES_EQUAL(2.777778e-10, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, one_second_positive)
{
test_convert(0, 0, 1, 0);
DOUBLES_EQUAL(0.00027777778, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, six_minute_positive)
{
test_convert(0, 6, 0, 0);
DOUBLES_EQUAL(0.1, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, three_hours_positive)
{
test_convert(3, 0, 0, 0);
DOUBLES_EQUAL(3, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, three_hours_fifteen_minutes_positive)
{
test_convert(3, 15, 0, 0);
DOUBLES_EQUAL(3.25, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, smallest_negative)
{
test_convert(0, 0, 0, 1);
DOUBLES_EQUAL(2.777778e-10, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, one_second_negative)
{
test_convert(0, 0, -1, 0);
DOUBLES_EQUAL(-0.00027777778, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, six_minute_negative)
{
test_convert(0, -6, 0, 0);
DOUBLES_EQUAL(-0.1, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, three_hours_negative)
{
test_convert(-3, 0, 0, 0);
DOUBLES_EQUAL(-3, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, three_hours_fifteen_minutes_negative)
{
test_convert(-3, 15, 0, 0);
DOUBLES_EQUAL(-3.25, result, 0.00000000001);
}

TEST(hmsf_to_decimal_hour, three_hours_negative_fifteen_minutes_negative)
{
test_convert(-3, -15, 0, 0);
DOUBLES_EQUAL(-2.75, result, 0.00000000001);
}
4 changes: 2 additions & 2 deletions timelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec)

void timelib_hms_to_decimal_hour(int hour, int min, int sec, double *h)
{
if (hour > 0) {
if (hour >= 0) {
*h = ((double)hour + (double)min / 60 + (double)sec / 3600);
} else {
*h = ((double)hour - (double)min / 60 - (double)sec / 3600);
Expand All @@ -209,7 +209,7 @@ void timelib_hms_to_decimal_hour(int hour, int min, int sec, double *h)

void timelib_hmsf_to_decimal_hour(int hour, int min, int sec, int us, double *h)
{
if (hour > 0) {
if (hour >= 0) {
*h = ((double)hour + (double)min / MINS_PER_HOUR + (double)sec / SECS_PER_HOUR) + (double)us / USECS_PER_HOUR;
} else {
*h = ((double)hour - (double)min / MINS_PER_HOUR - (double)sec / SECS_PER_HOUR) - (double)us / USECS_PER_HOUR;
Expand Down

0 comments on commit b8885ed

Please sign in to comment.