-
Notifications
You must be signed in to change notification settings - Fork 110
[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
Open
Shamzik
wants to merge
21
commits into
master
Choose a base branch
from
kshamazov/datetime
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
91759b3
stash
Shamzik c35762c
stash
Shamzik 01f2476
stash
Shamzik b29deda
stash
Shamzik 6c3b902
fixes
Shamzik d9da675
datetime methods
Shamzik 9825b2b
kphp::memory::libc_alloc_guard
Shamzik 7161d44
some tests
Shamzik 283b554
fixes
Shamzik a7ac9db
format
Shamzik 40b60bd
get_last_errors
Shamzik d5bb003
error messages
Shamzik 6273913
test 12_create_interval.php
Shamzik cc608cb
remove extra ';'
Shamzik 028e647
auto -> int32_t
Shamzik dc43023
fixes
Shamzik 5362352
implement setISODate method
Shamzik 9b7918d
implement sub methods
Shamzik a96050c
implement getOffset methods
Shamzik 39cfffc
construct -> parse
Shamzik befbf8d
use destructor{}
Shamzik 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
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 |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ inline constexpr std::string_view COMMA_ = ","; | |
| 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"; | ||
|
Contributor
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. It's the context for strings part of standard library. Let's move this to one specific for time functions |
||
| inline constexpr std::string_view SPACE_ = " "; | ||
| inline constexpr std::string_view WHAT_ = " \n\r\t\v\0"sv; | ||
| inline constexpr std::string_view ONE_ = "1"; | ||
|
|
@@ -60,6 +61,7 @@ struct StringLibConstants final : vk::not_copyable { | |
| string BACKSLASH_STR{string_context_impl_::BACKSLASH_.data(), static_cast<string::size_type>(string_context_impl_::BACKSLASH_.size())}; | ||
| string QUOTE_STR{string_context_impl_::QUOTE_.data(), static_cast<string::size_type>(string_context_impl_::QUOTE_.size())}; | ||
| string NEWLINE_STR{string_context_impl_::NEWLINE_.data(), static_cast<string::size_type>(string_context_impl_::NEWLINE_.size())}; | ||
| string NOW_STR{string_context_impl_::NOW_.data(), static_cast<string::size_type>(string_context_impl_::NOW_.size())}; | ||
| string SPACE_STR{string_context_impl_::SPACE_.data(), static_cast<string::size_type>(string_context_impl_::SPACE_.size())}; | ||
| string WHAT_STR{string_context_impl_::WHAT_.data(), static_cast<string::size_type>(string_context_impl_::WHAT_.size())}; | ||
| string ONE_STR{string_context_impl_::ONE_.data(), static_cast<string::size_type>(string_context_impl_::ONE_.size())}; | ||
|
|
@@ -85,6 +87,7 @@ struct StringLibConstants final : vk::not_copyable { | |
| BACKSLASH_STR.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| QUOTE_STR.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| NEWLINE_STR.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| NOW_STR.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| SPACE_STR.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| WHAT_STR.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
| ONE_STR.set_reference_counter_to(ExtraRefCnt::for_global_const); | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2020 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #include "runtime-light/stdlib/time/dateinterval.h" | ||
|
|
||
| #include <format> | ||
| #include <iterator> | ||
|
|
||
| #include "common/containers/final_action.h" | ||
| #include "runtime-common/core/allocator/script-allocator.h" | ||
| #include "runtime-common/core/runtime-core.h" | ||
| #include "runtime-light/stdlib/diagnostics/exception-functions.h" | ||
| #include "runtime-light/stdlib/diagnostics/logs.h" | ||
|
|
||
| class_instance<C$DateInterval> f$DateInterval$$__construct(const class_instance<C$DateInterval>& self, const string& duration) noexcept { | ||
| auto expected_rel_time{kphp::timelib::parse_interval({duration.c_str(), duration.size()})}; | ||
| if (!expected_rel_time.has_value()) [[unlikely]] { | ||
| string err_msg{"DateInterval::__construct(): "}; | ||
| format_to(std::back_inserter(err_msg), expected_rel_time.error(), duration.c_str()); | ||
| THROW_EXCEPTION(kphp::exception::make_throwable<C$Exception>(err_msg)); | ||
| return {}; | ||
| } | ||
| self->rel_time = std::move(*expected_rel_time); | ||
| return self; | ||
| } | ||
|
|
||
| class_instance<C$DateInterval> f$DateInterval$$createFromDateString(const string& datetime) noexcept { | ||
| auto [time, errors]{kphp::timelib::parse_time({datetime.c_str(), datetime.size()})}; | ||
| if (time == nullptr) [[unlikely]] { | ||
| kphp::log::warning("DateInterval::createFromDateString(): Unknown or bad format ({}) {}", datetime.c_str(), errors); | ||
| return {}; | ||
| } | ||
| class_instance<C$DateInterval> date_interval; | ||
| date_interval.alloc(); | ||
| date_interval->rel_time = kphp::timelib::clone(time->relative); | ||
| return date_interval; | ||
| } | ||
|
|
||
| string f$DateInterval$$format(const class_instance<C$DateInterval>& self, const string& format) noexcept { | ||
| string str; | ||
| kphp::timelib::format_to(std::back_inserter(str), {format.c_str(), format.size()}, *self->rel_time); | ||
| return str; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Compiler for PHP (aka KPHP) | ||
| // Copyright (c) 2022 LLC «V Kontakte» | ||
| // Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <string_view> | ||
|
|
||
| #include "common/algorithms/hashes.h" | ||
| #include "runtime-common/core/class-instance/refcountable-php-classes.h" | ||
| #include "runtime-common/core/runtime-core.h" | ||
| #include "runtime-common/stdlib/visitors/dummy-visitor-methods.h" | ||
| #include "runtime-light/stdlib/time/timelib-functions.h" | ||
|
|
||
| struct C$DateInterval : public refcountable_polymorphic_php_classes_virt<>, private DummyVisitorMethods { | ||
| using DummyVisitorMethods::accept; | ||
|
|
||
| kphp::timelib::rel_time_t rel_time{nullptr}; | ||
|
|
||
| virtual const char* get_class() const noexcept { | ||
| return R"(DateInterval)"; | ||
| } | ||
|
|
||
| virtual int32_t get_hash() const noexcept { | ||
| std::string_view name_view{C$DateInterval::get_class()}; | ||
| return static_cast<int32_t>(vk::murmur_hash<uint32_t>(name_view.data(), name_view.size())); | ||
| } | ||
| }; | ||
|
|
||
| class_instance<C$DateInterval> f$DateInterval$$__construct(const class_instance<C$DateInterval>& self, const string& duration) noexcept; | ||
|
|
||
| class_instance<C$DateInterval> f$DateInterval$$createFromDateString(const string& datetime) noexcept; | ||
|
|
||
| string f$DateInterval$$format(const class_instance<C$DateInterval>& self, const string& format) noexcept; |
Oops, something went wrong.
Oops, something went wrong.
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.
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