Skip to content

Commit b61435f

Browse files
authored
[k2] add Date/Time classes to light runtime (#1481)
1 parent 9a800aa commit b61435f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1990
-769
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ include(${THIRD_PARTY_DIR}/re2-cmake/re2.cmake)
4040
include(${THIRD_PARTY_DIR}/yaml-cpp-cmake/yaml-cpp.cmake)
4141
include(${THIRD_PARTY_DIR}/uber-h3-cmake/uber-h3.cmake)
4242

43+
# Timelib shoud use manualy specified allocations routines name prefix
44+
if(COMPILE_RUNTIME_LIGHT)
45+
set(TIMELIB_ALLOC_FUNC_PREFIX timelib_)
46+
else()
47+
set(TIMELIB_ALLOC_FUNC_PREFIX)
48+
endif()
49+
include(${THIRD_PARTY_DIR}/timelib-cmake/timelib.cmake)
50+
4351
# TODO: use FetchContent_Declare instead of include_directories
4452
include_directories(.)
4553
include_directories(${INCLUDE_DIR})

builtin-functions/kphp-light/stdlib/time-functions.txt

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,19 @@ function set_timer(int $timeout, callable():void $callback) ::: void;
3030

3131
function checkdate ($month ::: int, $day ::: int, $year ::: int) ::: bool;
3232

33-
// ===== UNSUPPORTED =====
34-
35-
/** @kphp-generate-stub-class */
3633
class DateInterval {
37-
/** @kphp-extern-func-info can_throw stub generation-required */
34+
/** @kphp-extern-func-info can_throw */
3835
public function __construct(string $duration);
39-
/** @kphp-extern-func-info stub generation-required */
4036
public static function createFromDateString(string $datetime): ?DateInterval;
41-
/** @kphp-extern-func-info stub generation-required */
4237
public function format(string $format): string;
4338
}
4439

45-
/** @kphp-generate-stub-class */
4640
class DateTimeZone {
47-
/** @kphp-extern-func-info can_throw stub generation-required */
41+
/** @kphp-extern-func-info can_throw */
4842
public function __construct(string $timezone);
49-
/** @kphp-extern-func-info stub generation-required */
5043
public function getName(): string;
5144
}
5245

53-
/** @kphp-generate-stub-class */
5446
interface DateTimeInterface {
5547
/* Constants */
5648
const ATOM = "Y-m-d\TH:i:sP";
@@ -86,84 +78,56 @@ interface DateTimeInterface {
8678
public function getTimestamp(): int;
8779
}
8880

89-
/** @kphp-generate-stub-class */
90-
class DateTime implements DateTimeInterface {
91-
/** @kphp-extern-func-info can_throw stub generation-required */
81+
class DateTimeImmutable implements DateTimeInterface {
82+
/** @kphp-extern-func-info can_throw */
9283
public function __construct(string $datetime = "now", ?DateTimeZone $timezone = null);
93-
/** @kphp-extern-func-info stub generation-required */
94-
public function add(DateInterval $interval): DateTime;
95-
/** @kphp-extern-func-info stub generation-required */
96-
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): ?DateTime;
97-
/** @kphp-extern-func-info stub generation-required */
98-
public static function createFromImmutable(DateTimeImmutable $object): DateTime;
99-
/** @kphp-extern-func-info stub generation-required */
84+
public function add(DateInterval $interval): DateTimeImmutable;
85+
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): ?DateTimeImmutable;
86+
public static function createFromMutable(DateTime $object): DateTimeImmutable;
10087
public static function getLastErrors(): array|false;
101-
/** @kphp-extern-func-info stub generation-required */
102-
public function modify(string $modifier): ?DateTime;
103-
/** @kphp-extern-func-info stub generation-required */
104-
public function setDate(int $year, int $month, int $day): DateTime;
105-
/** @kphp-extern-func-info stub generation-required */
106-
public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTime;
107-
/** @kphp-extern-func-info stub generation-required */
88+
public function modify(string $modifier): ?DateTimeImmutable;
89+
public function setDate(int $year, int $month, int $day): DateTimeImmutable;
90+
public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeImmutable;
10891
public function setTime(
10992
int $hour,
11093
int $minute,
11194
int $second = 0,
11295
int $microsecond = 0
113-
): DateTime;
114-
/** @kphp-extern-func-info stub generation-required */
115-
public function setTimestamp(int $timestamp): DateTime;
116-
/** @kphp-extern-func-info stub generation-required */
117-
public function sub(DateInterval $interval): DateTime;
118-
/** @kphp-extern-func-info stub generation-required */
96+
): DateTimeImmutable;
97+
public function setTimestamp(int $timestamp): DateTimeImmutable;
98+
public function sub(DateInterval $interval): DateTimeImmutable;
11999
public function diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval;
120-
/** @kphp-extern-func-info stub generation-required */
121100
public function format(string $format): string;
122-
/** @kphp-extern-func-info stub generation-required */
123101
public function getOffset(): int;
124-
/** @kphp-extern-func-info stub generation-required */
125102
public function getTimestamp(): int;
126103
}
127104

128-
/** @kphp-generate-stub-class */
129-
class DateTimeImmutable implements DateTimeInterface {
130-
/** @kphp-extern-func-info can_throw stub generation-required */
105+
class DateTime implements DateTimeInterface {
106+
/** @kphp-extern-func-info can_throw */
131107
public function __construct(string $datetime = "now", ?DateTimeZone $timezone = null);
132-
/** @kphp-extern-func-info stub generation-required */
133-
public function add(DateInterval $interval): DateTimeImmutable;
134-
/** @kphp-extern-func-info stub generation-required */
135-
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): ?DateTimeImmutable;
136-
/** @kphp-extern-func-info stub generation-required */
137-
public static function createFromMutable(DateTime $object): DateTimeImmutable;
138-
/** @kphp-extern-func-info stub generation-required */
108+
public function add(DateInterval $interval): DateTime;
109+
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): ?DateTime;
110+
public static function createFromImmutable(DateTimeImmutable $object): DateTime;
139111
public static function getLastErrors(): array|false;
140-
/** @kphp-extern-func-info stub generation-required */
141-
public function modify(string $modifier): ?DateTimeImmutable;
142-
/** @kphp-extern-func-info stub generation-required */
143-
public function setDate(int $year, int $month, int $day): DateTimeImmutable;
144-
/** @kphp-extern-func-info stub generation-required */
145-
public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeImmutable;
146-
/** @kphp-extern-func-info stub generation-required */
112+
public function modify(string $modifier): ?DateTime;
113+
public function setDate(int $year, int $month, int $day): DateTime;
114+
public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTime;
147115
public function setTime(
148116
int $hour,
149117
int $minute,
150118
int $second = 0,
151119
int $microsecond = 0
152-
): DateTimeImmutable;
153-
/** @kphp-extern-func-info stub generation-required */
154-
public function setTimestamp(int $timestamp): DateTimeImmutable;
155-
/** @kphp-extern-func-info stub generation-required */
156-
public function sub(DateInterval $interval): DateTimeImmutable;
157-
/** @kphp-extern-func-info stub generation-required */
120+
): DateTime;
121+
public function setTimestamp(int $timestamp): DateTime;
122+
public function sub(DateInterval $interval): DateTime;
158123
public function diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval;
159-
/** @kphp-extern-func-info stub generation-required */
160124
public function format(string $format): string;
161-
/** @kphp-extern-func-info stub generation-required */
162125
public function getOffset(): int;
163-
/** @kphp-extern-func-info stub generation-required */
164126
public function getTimestamp(): int;
165127
}
166128

129+
// ===== UNSUPPORTED =====
130+
167131
define('DATE_ATOM', "Y-m-d\TH:i:sP");
168132
define('DATE_COOKIE', "l, d-M-y H:i:s T");
169133
define('DATE_ISO8601', "Y-m-d\TH:i:sO");

compiler/compiler-settings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ void CompilerSettings::init() {
340340
if (!dynamic_incremental_linkage.get() && mode.get() != "k2-lib") {
341341
ss << " -fvisibility=hidden";
342342
}
343+
// Temporary solution. Required for allocator functions replacement in timelib
344+
ss << " -DTIMELIB_ALLOC_FUNC_PREFIX=timelib_";
343345
} else {
344346
// default value is false
345347
// when we build using full runtime, we should force to use runtime as static lib

runtime-common/core/allocator/script-malloc-interface.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ inline void* realloc(void* ptr, size_t new_size) noexcept {
7474
return new_ptr;
7575
}
7676

77+
inline char* strdup(const char* str1) noexcept {
78+
auto* str2{static_cast<char*>(kphp::memory::script::alloc(std::strlen(str1) + 1))};
79+
return std::strcpy(str2, str1);
80+
}
81+
7782
} // namespace script
7883

7984
} // namespace memory

runtime-common/runtime-common.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ prepend(RUNTIME_COMMON_SRC ${RUNTIME_COMMON_DIR}/ "${RUNTIME_COMMON_SRC}")
1313
vk_add_library_no_pic(runtime-common-no-pic OBJECT ${RUNTIME_COMMON_SRC})
1414
vk_add_library_pic(runtime-common-pic OBJECT ${RUNTIME_COMMON_SRC})
1515

16-
target_link_libraries(runtime-common-pic PUBLIC UBER_H3::pic::uber-h3)
17-
add_dependencies(runtime-common-pic UBER_H3::pic::uber-h3)
16+
target_link_libraries(runtime-common-pic PUBLIC UBER_H3::pic::uber-h3 KPHP_TIMELIB::pic::timelib)
17+
add_dependencies(runtime-common-pic UBER_H3::pic::uber-h3 KPHP_TIMELIB::pic::timelib)
1818

19-
target_link_libraries(runtime-common-no-pic PUBLIC UBER_H3::no-pic::uber-h3)
20-
add_dependencies(runtime-common-no-pic UBER_H3::no-pic::uber-h3)
19+
target_link_libraries(runtime-common-no-pic PUBLIC UBER_H3::no-pic::uber-h3 KPHP_TIMELIB::no-pic::timelib)
20+
add_dependencies(runtime-common-no-pic UBER_H3::no-pic::uber-h3 KPHP_TIMELIB::no-pic::timelib)
2121

2222
if(COMPILE_RUNTIME_LIGHT)
2323
target_compile_options(runtime-common-pic PUBLIC -stdlib=libc++ ${RUNTIME_LIGHT_VISIBILITY})

runtime-common/stdlib/stdlib.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ prepend(STDLIB_STRING stdlib/string/ mbstring-functions.cpp
1111
prepend(STDLIB_SYSTEM stdlib/system/ system-functions.cpp)
1212
prepend(STDLIB_SERVER stdlib/server/ url-functions.cpp
1313
net-functions.cpp)
14+
prepend(STDLIB_TIME stdlib/time/ timelib-functions.cpp)
1415
prepend(STDLIB_UBERH3 stdlib/uber-h3/ uber-h3-functions.cpp)
1516
prepend(STDLIB_VKEXT stdlib/vkext/ string-processing.cpp
1617
vkext-functions.cpp vkext-stats.cpp)
@@ -20,4 +21,4 @@ if(COMPILER_CLANG)
2021
endif()
2122

2223
set(STDLIB_SRC ${STDLIB_ARRAY} ${STDLIB_CRYPTO} ${STDLIB_MATH} ${STDLIB_MSGPACK} ${STDLIB_SERIALIZATION}
23-
${STDLIB_STRING} ${STDLIB_SYSTEM} ${STDLIB_SERVER} ${STDLIB_UBERH3} ${STDLIB_VKEXT})
24+
${STDLIB_STRING} ${STDLIB_SYSTEM} ${STDLIB_SERVER} ${STDLIB_TIME} ${STDLIB_UBERH3} ${STDLIB_VKEXT})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Compiler for PHP (aka KPHP)
2+
// Copyright (c) 2026 LLC «V Kontakte»
3+
// Distributed under the GPL v3 License, see LICENSE.notice.txt
4+
5+
#pragma once
6+
7+
#include <array>
8+
9+
namespace kphp::timelib {
10+
11+
namespace timezones {
12+
13+
inline constexpr const char* MOSCOW = "Europe/Moscow";
14+
inline constexpr const char* GMT3 = "Etc/GMT-3";
15+
inline constexpr const char* GMT4 = "Etc/GMT-4";
16+
17+
} // namespace timezones
18+
19+
namespace months {
20+
21+
constexpr inline std::array<const char*, 12> FULL_NAMES = {"January", "February", "March", "April", "May", "June",
22+
"July", "August", "September", "October", "November", "December"};
23+
constexpr inline std::array<const char*, 12> SHORT_NAMES = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
24+
25+
} // namespace months
26+
27+
namespace days {
28+
29+
constexpr inline std::array<const char*, 7> FULL_NAMES = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
30+
constexpr inline std::array<const char*, 7> SHORT_NAMES = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
31+
32+
} // namespace days
33+
34+
} // namespace kphp::timelib

0 commit comments

Comments
 (0)