| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- namespace App\Support;
- use Carbon\CarbonImmutable;
- use Illuminate\Support\Facades\DB;
- final class BackflowPeriod
- {
- public const TIMEZONE = 'Asia/Shanghai';
- public const DAILY_FROM_KEY = 'backflow_daily_from';
- public static function dailyFrom(): ?string
- {
- return DB::connection()->getSchemaBuilder()->hasTable('share_system_state')
- ? DB::table('share_system_state')->where('key', self::DAILY_FROM_KEY)->value('value') : null;
- }
- public static function forDate(string $date, ?string $dailyFrom): string
- {
- return $dailyFrom !== null && $date >= $dailyFrom ? $date : substr($date, 0, 7);
- }
- /** Inclusive start and exclusive end, both in Beijing. */
- public static function bounds(string $period, ?string $dailyFrom): array
- {
- if (strlen($period) === 10) {
- return [$period, CarbonImmutable::parse($period, self::TIMEZONE)->addDay()->toDateString()];
- }
- $start = $period . '-01';
- $end = CarbonImmutable::parse($start, self::TIMEZONE)->addMonth()->toDateString();
- return [$start, $dailyFrom === null ? $end : max($start, min($end, $dailyFrom))];
- }
- public static function waitsForSettlement(string $period, ?string $dailyFrom): bool
- {
- return strlen($period) === 10 || ($dailyFrom !== null && $period === substr($dailyFrom, 0, 7));
- }
- }
|