core/source/helpers/pit.php

256 lines
4.2 KiB
PHP
Raw Normal View History

2025-09-09 12:07:53 +02:00
<?php
2025-09-22 23:17:08 +02:00
/*
davina Calendar data CalDAV conditioner
Copyright (C) 2025 Fenris <fenris@folksprak.org>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see
<https://www.gnu.org/licenses/>.
*/
2025-09-09 12:07:53 +02:00
2025-09-16 12:05:35 +02:00
namespace davina\helpers\pit;
2025-09-09 12:07:53 +02:00
2025-09-16 12:48:45 +02:00
require_once(DIR_LOGIC . '/helpers/call.php');
require_once(DIR_LOGIC . '/helpers/string.php');
2025-09-09 12:07:53 +02:00
/**
*/
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
{
2025-09-16 12:05:35 +02:00
return \davina\helpers\string_\coin(
2025-09-09 12:07:53 +02:00
'{{year}}{{month}}{{day}}',
[
2025-09-16 12:05:35 +02:00
'year' => \davina\helpers\call\convey(
2025-09-09 12:07:53 +02:00
$date->year,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 4, '0', \STR_PAD_LEFT),
]
),
2025-09-16 12:05:35 +02:00
'month' => \davina\helpers\call\convey(
2025-09-09 12:07:53 +02:00
$date->month,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
2025-09-16 12:05:35 +02:00
'day' => \davina\helpers\call\convey(
2025-09-09 12:07:53 +02:00
$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
{
2025-09-16 12:05:35 +02:00
return \davina\helpers\string_\coin(
2025-09-09 12:07:53 +02:00
'{{hour}}{{minute}}{{second}}',
[
2025-09-16 12:05:35 +02:00
'hour' => \davina\helpers\call\convey(
2025-09-09 12:07:53 +02:00
$time->hour,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
2025-09-16 12:05:35 +02:00
'minute' => \davina\helpers\call\convey(
2025-09-09 12:07:53 +02:00
$time->minute,
[
fn($x) => \sprintf('%u', $x),
fn($x) => \str_pad($x, 2, '0', \STR_PAD_LEFT),
]
),
2025-09-16 12:05:35 +02:00
'second' => \davina\helpers\call\convey(
2025-09-09 12:07:53 +02:00
$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
{
2025-09-16 12:05:35 +02:00
return \davina\helpers\string_\coin(
2025-09-09 12:07:53 +02:00
'{{date}}{{macro_time}}',
[
'date' => date_to_string($datetime->date),
'macro_time' => (
($datetime->time === null)
?
''
:
2025-09-16 12:05:35 +02:00
\davina\helpers\string_\coin(
2025-09-09 12:07:53 +02:00
'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)),
)
));
}
?>