|
7 | 7 | #include <array> |
8 | 8 | #include <chrono> |
9 | 9 | #include <climits> |
| 10 | +#include <cstdint> |
10 | 11 | #include <ctime> |
11 | 12 | #include <string_view> |
12 | 13 |
|
13 | | -#include "runtime-light/utils/logs.h" |
| 14 | +#include "runtime-common/core/runtime-core.h" |
| 15 | +#include "runtime-light/stdlib/time/timelib-constants.h" |
14 | 16 |
|
15 | 17 | namespace { |
16 | | -constexpr std::string_view PHP_TIMELIB_TZ_MOSCOW = "Europe/Moscow"; |
17 | | -constexpr std::string_view PHP_TIMELIB_TZ_GMT3 = "Etc/GMT-3"; |
18 | 18 |
|
19 | 19 | constexpr std::array<std::string_view, 12> PHP_TIMELIB_MON_FULL_NAMES = {"January", "February", "March", "April", "May", "June", |
20 | 20 | "July", "August", "September", "October", "November", "December"}; |
@@ -65,6 +65,21 @@ void iso_week_number(int y, int doy, int weekday, int& iw, int& iy) noexcept { |
65 | 65 | } |
66 | 66 | } |
67 | 67 |
|
| 68 | +} // namespace |
| 69 | + |
| 70 | +namespace kphp::time::impl { |
| 71 | + |
| 72 | +int64_t fix_year(int64_t year) noexcept { |
| 73 | + if (year <= 100U) { |
| 74 | + if (year <= 69) { |
| 75 | + year += 2000; |
| 76 | + } else { |
| 77 | + year += 1900; |
| 78 | + } |
| 79 | + } |
| 80 | + return year; |
| 81 | +} |
| 82 | + |
68 | 83 | string date(const string& format, const tm& t, int64_t timestamp, bool local) noexcept { |
69 | 84 | string_buffer& SB{RuntimeContext::get().static_SB}; |
70 | 85 |
|
@@ -196,7 +211,7 @@ string date(const string& format, const tm& t, int64_t timestamp, bool local) no |
196 | 211 | break; |
197 | 212 | case 'e': |
198 | 213 | if (local) { |
199 | | - SB << PHP_TIMELIB_TZ_MOSCOW.data(); |
| 214 | + SB << kphp::timelib::timezones::MOSCOW.data(); |
200 | 215 | } else { |
201 | 216 | SB << "UTC"; |
202 | 217 | } |
@@ -294,59 +309,4 @@ string date(const string& format, const tm& t, int64_t timestamp, bool local) no |
294 | 309 | return SB.str(); |
295 | 310 | } |
296 | 311 |
|
297 | | -int64_t fix_year(int64_t year) noexcept { |
298 | | - if (year <= 100U) { |
299 | | - if (year <= 69) { |
300 | | - year += 2000; |
301 | | - } else { |
302 | | - year += 1900; |
303 | | - } |
304 | | - } |
305 | | - return year; |
306 | | -} |
307 | | - |
308 | | -} // namespace |
309 | | - |
310 | | -int64_t f$mktime(Optional<int64_t> hour, Optional<int64_t> minute, Optional<int64_t> second, Optional<int64_t> month, Optional<int64_t> day, |
311 | | - Optional<int64_t> year) noexcept { |
312 | | - namespace chrono = std::chrono; |
313 | | - const auto time_since_epoch{chrono::system_clock::now().time_since_epoch()}; |
314 | | - chrono::year_month_day current_date{chrono::sys_days{duration_cast<chrono::days>(time_since_epoch)}}; |
315 | | - |
316 | | - const auto hours{chrono::hours(hour.has_value() ? hour.val() : duration_cast<chrono::hours>(time_since_epoch).count() % 24)}; |
317 | | - const auto minutes{chrono::minutes(minute.has_value() ? minute.val() : duration_cast<chrono::minutes>(time_since_epoch).count() % 60)}; |
318 | | - const auto seconds{chrono::seconds(second.has_value() ? second.val() : duration_cast<chrono::seconds>(time_since_epoch).count() % 60)}; |
319 | | - const auto months{chrono::months(month.has_value() ? month.val() : static_cast<unsigned>(current_date.month()))}; |
320 | | - const auto days{chrono::days(day.has_value() ? day.val() : static_cast<unsigned>(current_date.day()))}; |
321 | | - const auto years{chrono::years(year.has_value() ? fix_year(year.val()) : static_cast<int>(current_date.year()) - 1970)}; |
322 | | - |
323 | | - const auto result{hours + minutes + seconds + months + days + years}; |
324 | | - return duration_cast<chrono::seconds>(result).count(); |
325 | | -} |
326 | | - |
327 | | -string f$gmdate(const string& format, Optional<int64_t> timestamp) noexcept { |
328 | | - namespace chrono = std::chrono; |
329 | | - |
330 | | - const time_t now{timestamp.has_value() ? timestamp.val() : duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count()}; |
331 | | - struct tm tm {}; |
332 | | - gmtime_r(&now, &tm); |
333 | | - return date(format, tm, now, false); |
334 | | -} |
335 | | - |
336 | | -string f$date(const string& format, Optional<int64_t> timestamp) noexcept { |
337 | | - namespace chrono = std::chrono; |
338 | | - |
339 | | - const time_t now{timestamp.has_value() ? timestamp.val() : duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count()}; |
340 | | - struct tm tm {}; |
341 | | - localtime_r(&now, &tm); |
342 | | - return date(format, tm, now, true); |
343 | | -} |
344 | | - |
345 | | -bool f$date_default_timezone_set(const string& s) noexcept { |
346 | | - const std::string_view timezone_view{s.c_str(), s.size()}; |
347 | | - if (timezone_view != PHP_TIMELIB_TZ_GMT3 && timezone_view != PHP_TIMELIB_TZ_MOSCOW) { |
348 | | - kphp::log::warning("unsupported default timezone '{}'", s.c_str()); |
349 | | - return false; |
350 | | - } |
351 | | - return true; |
352 | | -} |
| 312 | +} // namespace kphp::time::impl |
0 commit comments