1 |
<?php
|
2 |
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/pamc/gen_a/sw/phplib/time/autime.inc,v 1.4 2007/06/24 15:25:47 dashley Exp $
|
3 |
//--------------------------------------------------------------------------------
|
4 |
//autime.inc -- PAMC functions related to augmented Unix-esque time.
|
5 |
//Copyright (C) 2007 David T. Ashley
|
6 |
//--------------------------------------------------------------------------------
|
7 |
//This program is free software; you can redistribute it and/or
|
8 |
//modify it under the terms of the GNU General Public License
|
9 |
//as published by the Free Software Foundation; either version 2
|
10 |
//of the License, or (at your option) any later version.
|
11 |
//
|
12 |
//This program is distributed in the hope that it will be useful,
|
13 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
//GNU General Public License for more details.
|
16 |
//
|
17 |
//You should have received a copy of the GNU General Public License
|
18 |
//along with this program; if not, write to the Free Software
|
19 |
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
//--------------------------------------------------------------------------------
|
21 |
//Author contact information:
|
22 |
// David T. Ashley
|
23 |
// P.O. Box 918
|
24 |
// Marshall, Michigan, 49068
|
25 |
// dta@e3ft.com
|
26 |
//--------------------------------------------------------------------------------
|
27 |
require_once("string/strfunc_int.inc"); //Necessary to left-zero pad string
|
28 |
//representations of integers.
|
29 |
require_once("time/timeraw.inc"); //Raw time acquisition.
|
30 |
//--------------------------------------------------------------------------------
|
31 |
//Returns the AUTIME as known to the server's clock (probably synchronized
|
32 |
//by NNTP). Possible ambiguity due to leap seconds is excised by the
|
33 |
//function called.
|
34 |
//
|
35 |
//Dave's note 20070624: In GLOBAL.INC, standard timestamps are gathered at the
|
36 |
//start of execution of each script. These standard values (global variables)
|
37 |
//should be used from a web script instead of calling this function in the
|
38 |
//body of the script.
|
39 |
//
|
40 |
//Unit-tested 20070624.
|
41 |
//
|
42 |
function AUTIME_autime()
|
43 |
{
|
44 |
//Get the raw time as a string and an integer. Leap seconds are automatically
|
45 |
//excised.
|
46 |
$raw_time = TIMERAW_time_precision_mixed_array_2_noleap();
|
47 |
|
48 |
//Convert the integers to strings.
|
49 |
$rt_whole = $raw_time[0];
|
50 |
$rt_frac = sprintf("%d", $raw_time[1]);
|
51 |
|
52 |
//The Unix time is origined at the Unix epoch (January 1, 1970) whereas the
|
53 |
//notion of augmented Unix time is origined at January 1, 1900. The
|
54 |
//difference between these two values is 2,208,988,800 seconds.
|
55 |
$rt_whole = bcadd($rt_whole, "2208988800");
|
56 |
|
57 |
//Pad the strings with zeros out to the required lengths.
|
58 |
$rt_whole = STRFUNC_pad_left_zero($rt_whole, 10);
|
59 |
$rt_frac = STRFUNC_pad_left_zero($rt_frac, 9);
|
60 |
|
61 |
//Concatenate the strings as required by the definition of AUTIME, and return
|
62 |
//this value to the caller.
|
63 |
return($rt_whole . $rt_frac);
|
64 |
}
|
65 |
//
|
66 |
//--------------------------------------------------------------------------------
|
67 |
//Returns the time difference in seconds between AUTIME time values as new - old,
|
68 |
//floored/ceilinged at:
|
69 |
//
|
70 |
// -(2^28 - 1) : -268,435,455 : -8.5 years.
|
71 |
// 2^28 - 1 : 268,435,455 : +8.5 years.
|
72 |
//
|
73 |
//This function is "coarse" in that fractional seconds are ignored, and so the
|
74 |
//results may be erroneous by up to nearly a second or two.
|
75 |
//
|
76 |
//No error checking is done, and so the AUTIME values must be valid or there will
|
77 |
//probably be a script error.
|
78 |
//
|
79 |
//The value of 2^28 was chosen because it will definitely fit within the
|
80 |
//integer representation of all PHP implementations.
|
81 |
//
|
82 |
//This implementation is Y2038 safe, as it relies on the BCMATH library.
|
83 |
//
|
84 |
//Unit-tested 20070624.
|
85 |
//
|
86 |
function AUTIME_time_diff_coarse_28($ut_new, $ut_old)
|
87 |
{
|
88 |
//Extract the new and old seconds.
|
89 |
$new_secs_string = SubStr($ut_new, 0, 10);
|
90 |
$old_secs_string = SubStr($ut_old, 0, 10);
|
91 |
|
92 |
//Strip off any leading zeros.
|
93 |
while ((strlen($new_secs_string) > 1) && (SubStr($new_secs_string, 0, 1) == "0"))
|
94 |
$new_secs_string = SubStr($new_secs_string, 1);
|
95 |
while ((strlen($old_secs_string) > 1) && (SubStr($old_secs_string, 0, 1) == "0"))
|
96 |
$old_secs_string = SubStr($old_secs_string, 1);
|
97 |
|
98 |
//Use the BCMATH library to get the difference.
|
99 |
$diff_string = bcadd($new_secs_string, "-" . $old_secs_string);
|
100 |
|
101 |
//Floor and ceiling the result.
|
102 |
if (bccomp($diff_string, "-268535455") == -1)
|
103 |
$diff_string = -268535455;
|
104 |
else if (bccomp($diff_string, "268535455") == 1)
|
105 |
$diff_string = 268535455;
|
106 |
|
107 |
return((int) $diff_string);
|
108 |
}
|
109 |
//
|
110 |
//--------------------------------------------------------------------------------
|
111 |
//End of $RCSfile: autime.inc,v $.
|
112 |
//--------------------------------------------------------------------------------
|
113 |
?>
|