-
Notifications
You must be signed in to change notification settings - Fork 111
[k2] add Date/Time classes to light runtime #1481
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
base: master
Are you sure you want to change the base?
Conversation
d1df9ec to
244c045
Compare
| class string { | ||
| public: | ||
| using size_type = string_size_type; | ||
| using value_type = char; |
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.
What's the purpose of this change?
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 was added to be able to use std::back_inserter
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.
Let's not change core KPHP types
49fc0f4 to
a96050c
Compare
apolyakov
left a comment
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.
I've reviewed and noted some comments. Please, take into account that these comments should be applied to whole PR, not only the places where I left those comments
| inline constexpr std::string_view BACKSLASH_ = "\\"; | ||
| inline constexpr std::string_view QUOTE_ = "\""; | ||
| inline constexpr std::string_view NEWLINE_ = "\n"; | ||
| inline constexpr std::string_view NOW_ = "now"; |
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.
It's the context for strings part of standard library. Let's move this to one specific for time functions
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.
fixed
|
|
||
| #include "kphp/timelib/timelib.h" | ||
|
|
||
| #include "common/containers/final_action.h" |
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.
Please, remove all the unused includes
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.
fixed
|
|
||
| using error_container_t = std::unique_ptr<timelib_error_container, destructor>; | ||
| using rel_time_t = std::unique_ptr<timelib_rel_time, destructor>; | ||
| using time_t = std::unique_ptr<timelib_time, destructor>; |
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.
Now we have a mix of raw timelib types and our own wrappers in the API. It doesn't look like a good solution.
Also, please refactor exisiting code that uses raw types in case you add wrappers. Mixing types is always bad
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.
fixed except clone function
|
|
||
| time_offset_t construct_time_offset(timelib_time& t) noexcept; | ||
|
|
||
| std::pair<time_t, error_container_t> parse_time(std::string_view time_sv) noexcept; |
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.
Is it possible for us to have both valid time_t and error_container_t?
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.
error_container_t is always non-null here. time_t is null in case of an error.
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.
So, it seems that std::expected<std::pair<time, error>, error> is the right choice here
|
|
||
| time_t add(timelib_time& t, timelib_rel_time& interval) noexcept; | ||
|
|
||
| rel_time_t clone(timelib_rel_time& rt) noexcept; |
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.
Isn't it better to just create a wrapper class? As far as I see, it might be worth it since you already have clone and destruct
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.
I've refactored the header to make it easier to read. This library isn't very friendly to creating wrapper classes around it.
|
|
||
| } // namespace timezones | ||
|
|
||
| inline constexpr std::string_view NOW_ = "now"; |
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.
It doesn't look like it should be public
| constexpr inline std::array<std::string_view, 7> DAY_FULL_NAMES = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; | ||
| constexpr inline std::array<std::string_view, 7> DAY_SHORT_NAMES = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | ||
|
|
||
| struct error_container_destructor { |
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.
Is it supposed to be manually used by a user of kphp::timelib?
| } | ||
| }; | ||
|
|
||
| using error_container_t = std::unique_ptr<timelib_error_container, kphp::timelib::error_container_destructor>; |
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.
I'd suggest to get rid of _t suffix
| using time_t = std::unique_ptr<timelib_time, kphp::timelib::time_destructor>; | ||
|
|
||
| /* === rel_time === */ | ||
| std::expected<kphp::timelib::rel_time_t, kphp::timelib::error_container_t> parse_interval(std::string_view format) noexcept; |
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.
I don't understand what does this function do. Its name is parse_interval while it accepts some format? What does format mean here?
|
|
||
| /* === rel_time === */ | ||
| std::expected<kphp::timelib::rel_time_t, kphp::timelib::error_container_t> parse_interval(std::string_view format) noexcept; | ||
| kphp::timelib::rel_time_t clone_time_interval(const kphp::timelib::time_t& t) noexcept; |
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.
Is kphp::timelib::time_t an interval? It seems more like a timepoint
| class string { | ||
| public: | ||
| using size_type = string_size_type; | ||
| using value_type = char; |
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.
Let's not change core KPHP types
| timelib_sll isoweek{}; | ||
| timelib_sll isoyear{}; | ||
|
|
||
| for (std::size_t i{0}; i < format.size(); ++i) { |
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.
We don't use std:: for primitive types
|
|
||
| template<typename OutputIt> | ||
| OutputIt format_to(OutputIt out, std::string_view format, const kphp::timelib::time_t& t) noexcept { | ||
| return kphp::timelib::details::format_to<OutputIt>(out, format, t, t->is_localtime); |
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.
Why the approach to implement format_to for time and rel_time differs?
| options |= TIMELIB_OVERRIDE_TIME; | ||
| } | ||
|
|
||
| kphp::memory::libc_alloc_guard{}, timelib_fill_holes(time.get(), now_time.get(), options); |
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.
libc_alloc_guard{} is not free in terms of performance. Try to use it as less as possible
| case 'o': | ||
| if (!weekYearSet) { | ||
| timelib_isoweek_from_date(t->y, t->m, t->d, std::addressof(isoweek), std::addressof(isoyear)); | ||
| weekYearSet = 1; |
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.
Implicit cast here
| } | ||
|
|
||
| void update_last_errors(kphp::timelib::error_container_t&& new_errors) noexcept { | ||
| last_errors.swap(new_errors); |
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.
Is seems unnecessary to waste CPU cycles to move a value back to new_errors
| @@ -0,0 +1,34 @@ | |||
| // Compiler for PHP (aka KPHP) | |||
| // Copyright (c) 2022 LLC «V Kontakte» | |||
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.
Please, do not let copy paste introduce errors. Change the year to 2025
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.
Let's follow our style and name files like datetime-zone.h
| return self; | ||
| } | ||
|
|
||
| string f$DateTimeZone$$getName(const class_instance<C$DateTimeZone>& self) noexcept { |
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.
What was the reason to move these definitions into the cpp file?
| kphp::log::warning("DateInterval::createFromDateString(): Unknown or bad format ({}) {}", datetime.c_str(), errors); | ||
| return {}; | ||
| } | ||
| class_instance<C$DateInterval> date_interval; |
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.
There is make_instance alternative
This first part of implementing Date/Time classes. Here implemented: