core/source/helpers/pit.php

242 lines
3.5 KiB
PHP
Raw Normal View History

2025-09-09 12:07:53 +02:00
<?php
namespace davigil\helpers\pit;
require_once('helpers/call.php');
require_once('helpers/string.php');
/**
*/
class struct_pit
{
public int $stamp;
public function __construct(
int $stamp
)
{
$this->stamp = $stamp;
}
}
/**
*/
class struct_date
{
public int $year;
public int $month;
public int $day;
public function __construct(
int $year,
int $month,
int $day,
)
{
$this->year = $year;
$this->month = $month;
$this->day = $day;
}
}
/**
*/
class struct_time
{
public int $hour;
public int $minute;
public int $second;
public function __construct(
int $hour,
int $minute,
int $second
)
{
$this->hour = $hour;
$this->minute = $minute;
$this->second = $second;
}
}
/**
*/
class struct_datetime
{
public int $timezone_shift;
public struct_date $date;
public ?struct_time $time;
public function __construct(
int $timezone_shift,
struct_date $date,
?struct_time $time
)
{
$this->timezone_shift = $timezone_shift;
$this->date = $date;
$this->time = $time;
}
}
/**
*/
function date_to_string(
struct_date $date
) : string
{
return \davigil\helpers\string_\coin(
'{{year}}{{month}}{{day}}',
[
'year' => \davigil\helpers\call\convey(
$date->year,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 4, '0', \STR_PAD_LEFT),
]
),
'month' => \davigil\helpers\call\convey(
$date->month,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
'day' => \davigil\helpers\call\convey(
$date->day,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
]
);
}
/**
*/
function time_to_string(
struct_time $time
) : string
{
return \davigil\helpers\string_\coin(
'{{hour}}{{minute}}{{second}}',
[
'hour' => \davigil\helpers\call\convey(
$time->hour,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
'minute' => \davigil\helpers\call\convey(
$time->minute,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
'second' => \davigil\helpers\call\convey(
$time->second,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
]
);
}
/**
*/
function datetime_to_string(
struct_datetime $datetime
) : string
{
return \davigil\helpers\string_\coin(
'{{date}}{{macro_time}}',
[
'date' => date_to_string($datetime->date),
'macro_time' => (
($datetime->time === null)
?
''
:
\davigil\helpers\string_\coin(
'T{{time}}{{utc}}',
[
'time' => time_to_string($datetime->time),
'utc' => (($datetime->timezone_shift === 0) ? 'Z' : ''),
]
)
),
]
);
}
/**
*/
function pit_to_unix_timestamp(
struct_pit $pit
) : int
{
return $pit->stamp;
}
/**
*/
function pit_from_unix_timestamp(
int $unix_timestamp
) : struct_pit
{
return (new struct_pit(
$unix_timestamp
));
}
/**
*/
function pit_from_datetime(
struct_datetime $datetime
) : struct_pit
{
return (new struct_pit(
\strtotime(datetime_to_string($datetime))
));
}
/**
*/
function pit_to_datetime(
struct_pit $pit
) : struct_datetime
{
$str = \date('c', $pit->stamp);
return (new struct_datetime(
0,
new struct_date(
\intval(\substr($str, 0, 4)),
\intval(\substr($str, 5, 2)),
\intval(\substr($str, 8, 2)),
),
new struct_time(
\intval(\substr($str, 11, 2)),
\intval(\substr($str, 14, 2)),
\intval(\substr($str, 17, 2)),
)
));
}
?>