-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Handle "today" and "now" in cython instead of C #18666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bee93f8
handle now and today in cython instead of C
jbrockmendel 3c49f14
delay encoding to bytes
jbrockmendel 4f6efd5
Merge branch 'master' of https://github.com/pandas-dev/pandas into to…
jbrockmendel 6273f51
delay checks of today and now
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,55 +33,6 @@ This file implements string parsing and creation for NumPy datetime. | |
#include "np_datetime_strings.h" | ||
|
||
|
||
/* Platform-specific time_t typedef */ | ||
typedef time_t NPY_TIME_T; | ||
|
||
/* | ||
* Wraps `localtime` functionality for multiple platforms. This | ||
* converts a time value to a time structure in the local timezone. | ||
* | ||
* Returns 0 on success, -1 on failure. | ||
*/ | ||
static int get_localtime(NPY_TIME_T *ts, struct tm *tms) { | ||
char *func_name = "<unknown>"; | ||
#if defined(_WIN32) | ||
#if defined(_MSC_VER) && (_MSC_VER >= 1400) | ||
if (localtime_s(tms, ts) != 0) { | ||
func_name = "localtime_s"; | ||
goto fail; | ||
} | ||
#elif defined(__GNUC__) && defined(NPY_MINGW_USE_CUSTOM_MSVCR) | ||
if (_localtime64_s(tms, ts) != 0) { | ||
func_name = "_localtime64_s"; | ||
goto fail; | ||
} | ||
#else | ||
struct tm *tms_tmp; | ||
localtime_r(ts, tms_tmp); | ||
if (tms_tmp == NULL) { | ||
func_name = "localtime"; | ||
goto fail; | ||
} | ||
memcpy(tms, tms_tmp, sizeof(struct tm)); | ||
#endif | ||
#else | ||
if (localtime_r(ts, tms) == NULL) { | ||
func_name = "localtime_r"; | ||
goto fail; | ||
} | ||
#endif | ||
|
||
return 0; | ||
|
||
fail: | ||
PyErr_Format(PyExc_OSError, | ||
"Failed to use '%s' to convert " | ||
"to a local time", | ||
func_name); | ||
return -1; | ||
} | ||
|
||
|
||
/* | ||
* Parses (almost) standard ISO 8601 date strings. The differences are: | ||
* | ||
|
@@ -138,59 +89,6 @@ int parse_iso_8601_datetime(char *str, int len, | |
out->month = 1; | ||
out->day = 1; | ||
|
||
/* | ||
* The string "today" means take today's date in local time, and | ||
* convert it to a date representation. This date representation, if | ||
* forced into a time unit, will be at midnight UTC. | ||
* This is perhaps a little weird, but done so that the | ||
* 'datetime64[D]' type produces the date you expect, rather than | ||
* switching to an adjacent day depending on the current time and your | ||
* timezone. | ||
*/ | ||
if (len == 5 && tolower(str[0]) == 't' && tolower(str[1]) == 'o' && | ||
tolower(str[2]) == 'd' && tolower(str[3]) == 'a' && | ||
tolower(str[4]) == 'y') { | ||
NPY_TIME_T rawtime = 0; | ||
struct tm tm_; | ||
|
||
time(&rawtime); | ||
if (get_localtime(&rawtime, &tm_) < 0) { | ||
return -1; | ||
} | ||
out->year = tm_.tm_year + 1900; | ||
out->month = tm_.tm_mon + 1; | ||
out->day = tm_.tm_mday; | ||
|
||
/* | ||
* Indicate that this was a special value, and | ||
* is a date (unit 'D'). | ||
*/ | ||
if (out_local != NULL) { | ||
*out_local = 0; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/* The string "now" resolves to the current UTC time */ | ||
if (len == 3 && tolower(str[0]) == 'n' && tolower(str[1]) == 'o' && | ||
tolower(str[2]) == 'w') { | ||
NPY_TIME_T rawtime = 0; | ||
|
||
time(&rawtime); | ||
|
||
/* | ||
* Indicate that this was a special value, and | ||
* use 's' because the time() function has resolution | ||
* seconds. | ||
*/ | ||
if (out_local != NULL) { | ||
*out_local = 0; | ||
} | ||
|
||
return convert_datetime_to_datetimestruct(PANDAS_FR_s, rawtime, out); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side-benefit: this is the only usage of convert_datetime_to_datetimestruct outside of np_datetime.c, so after this we can get rid of the extra layer there. |
||
} | ||
|
||
substr = str; | ||
sublen = len; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an 49 line version of
time.time()