74 lines
2.6 KiB
PHP
74 lines
2.6 KiB
PHP
<?php
|
|
|
|
/*
|
|
This is free and unencumbered software released into the public domain.
|
|
|
|
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
distribute this software, either in source code form or as a compiled
|
|
binary, for any purpose, commercial or non-commercial, and by any
|
|
means.
|
|
|
|
In jurisdictions that recognize copyright laws, the author or authors
|
|
of this software dedicate any and all copyright interest in the
|
|
software to the public domain. We make this dedication for the benefit
|
|
of the public at large and to the detriment of our heirs and
|
|
successors. We intend this dedication to be an overt act of
|
|
relinquishment in perpetuity of all present and future rights to this
|
|
software under copyright law.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
function unix_to_dit($time) {
|
|
$dit = ($time - 12*3600) % 86400 / 86400;
|
|
$deca = $dit * 10 % 10;
|
|
$decim = str_pad($dit * 1000 % 100, 2, '0', STR_PAD_LEFT);
|
|
$desec = str_pad($dit * 100000 % 100, 2, '0', STR_PAD_LEFT);
|
|
return "$deca.$decim.$desec";
|
|
}
|
|
|
|
function unix_to_stardit($time) {
|
|
$year = date('Y', $time - 12*3600) + 10000;
|
|
$day = date('z', $time - 12*3600) + 1;
|
|
return "$year:$day:" . unix_to_dit($time);
|
|
}
|
|
|
|
function is_dit($dit) {
|
|
return preg_match('/^[0-9]{1}(.[0-9]{1,2})?(.[0-9]{1,2})?$/', $dit);
|
|
}
|
|
|
|
function dit_to_unix($dit) {
|
|
if (!is_dit($dit))
|
|
return false;
|
|
$dit = explode('.', $dit);
|
|
$result = 0;
|
|
for ($i = 0 ; isset($dit[$i]) AND $i < 3 ; $i++)
|
|
$result += $dit[$i] * 8640 / 10**(2*$i);
|
|
return $result + 12*3600;
|
|
}
|
|
|
|
function is_stardit($stardit) {
|
|
return preg_match('/^((-?[0-9]{1,5}:)?[0-9]{1,3}:)?[0-9]{1}(\.[0-9]{1,2})?(\.[0-9]{1,2})?$/', $stardit);
|
|
}
|
|
|
|
function stardit_to_unix($stardit) {
|
|
if (!is_stardit($stardit))
|
|
return false;
|
|
$stardit = array_reverse(explode(':', $stardit));
|
|
$tz = date_default_timezone_get();
|
|
date_default_timezone_set('UTC');
|
|
$yearSec = 0;
|
|
$daySec = 0;
|
|
if (count($stardit) === 3)
|
|
$yearSec = mktime(year: $stardit[2]-10000, month: 1, day: 1, hour: 0, minute: 0, second: 0);
|
|
if (count($stardit) >= 2)
|
|
$daySec = date_format(date_create_from_format('!z', $stardit[1] - 1), 'U');
|
|
date_default_timezone_set($tz);
|
|
return $yearSec + $daySec + dit_to_unix($stardit[0]);
|
|
}
|