Skip to content
Myr

Fix gmtime and localtime on negative timestamps with fractional seconds (#3634)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The whole seconds were truncated toward zero while the fractional part was measured from the floor, so any negative non-integral timestamp was rendered one second late; gmtime was not monotonic across the epoch.

authored by itchyny · committed by GitHub · parent 57f5976 · Browse files · 2 files+12 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+12 added−4 removed

  • tests:+8 added−0 removed
  • other:+4 added−4 removed

2 files+12 added−4 removed

modified:src/builtin.c+4 added-4 removed
1755 unmodified lines
1756 return ret_error(a, jv_string("gmtime() requires numeric inputs"));1757 struct tm tm, *tmp;1758 memset(&tm, 0, sizeof(tm));1759 double fsecs = jv_number_value(a);1760 time_t secs = fsecs;1760 time_t secs = floor(fsecs);1761 jv_free(a);1762 tmp = gmtime_r(&secs, &tm);1763 if (tmp == NULL)1764 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));
5 unmodified lines
1770 return ret_error(a, jv_string("gmtime requires numeric inputs"));1771 struct tm tm, *tmp;1772 memset(&tm, 0, sizeof(tm));1773 double fsecs = jv_number_value(a);1774 time_t secs = fsecs;1774 time_t secs = floor(fsecs);1775 jv_free(a);1776 tmp = gmtime(&secs);1777 if (tmp == NULL)1778 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));
12 unmodified lines
1791 return ret_error(a, jv_string("localtime() requires numeric inputs"));1792 struct tm tm, *tmp;1793 memset(&tm, 0, sizeof(tm));1794 double fsecs = jv_number_value(a);1795 time_t secs = fsecs;1795 time_t secs = floor(fsecs);1796 jv_free(a);1797 tmp = localtime_r(&secs, &tm);1798 if (tmp == NULL)1799 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));
5 unmodified lines
1805 return ret_error(a, jv_string("localtime requires numeric inputs"));1806 struct tm tm, *tmp;1807 memset(&tm, 0, sizeof(tm));1808 double fsecs = jv_number_value(a);1809 time_t secs = fsecs;1809 time_t secs = floor(fsecs);1810 jv_free(a);1811 tmp = localtime(&secs);1812 if (tmp == NULL)1813 return jv_invalid_with_msg(jv_string("error converting number of seconds since epoch to datetime"));
350 unmodified lines
modified:tests/optional.test+8 added
8 unmodified lines
9strftime("%A, %B %e, %Y")101435677542.82235111"Tuesday, June 30, 2015"1213# Timestamps before the epoch are not supported on mingw/WIN3214gmtime15-1.2516[1969,11,31,23,59,58.75,3,364]1718map(gmtime) | . == sort19[-1.5,-1,-0.5,0,0.5,1,1.5]20true