locutusjs / locutus

Bringing stdlibs of other programming languages to JavaScript for educational purposes

Home Page:https://locutus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Is strtotime Algorithmically Equivalent to the PHP Version?

astorm opened this issue · comments

Hello! I recently discovered this repository while working on an article/tutorial about javascript date libraries. It looks pretty useful. I have a question about one of the implementations and I wasn't sure where/who to ask -- my apologies if this is an abuse of the issues format.

I'm curious about the relationship between this repository's implementation of strtotime and PHP's native implementation. Specifically, is the locutus implementation of strtotime algorithmically equivalent to the C code that powers PHP strtotime, or is the locutus implementation an alternate implementation that returns the same results? Or is the relationship murkier with the C implementation inform some of of what's going on in the javascript code, but the language differences means locutus is effictivly it's own thing?

I started trying to answer this myself and was quickly reminded why it's so rare for folks to regularly program in raw C these days. I figured/hoped the author would be able to give me a high level answer before I burn cycles deciphering a C based scanner.

Hi. First of all, please consider locutus implementation of strtotime a WIP. It still lacks some features that native strtotime offers (especially timezone support and several complex date/time formats).
To answer your question, locutus implementation is based on PHP implementation, but they're not 100% identical. It's done by trying to understand what's happening in C code and trying to write it in JS-way. This is one reason why the implementation is still incomplete.
As for details, the pattern matching follows same rules, but some regexes and their order differ for various reasons, it will be aligned in near future.
The time computation also follows PHP logic, but uses JS APIs instead of calculating "by hand" wherever possible. This may result in different output timestamps in some corner cases.
To summarize, at the moment you can't count on high compatibility, but we're trying to get as close as possible.

If you have additional questions, please feel free to ask here on contact me directly.

Thank you @kukawski, that's a perfect answer to my question. I'll say hello if I have more to ask or run into any edge cases. Thanks for contributing this code! (closing)