1 |
<?php
|
2 |
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/fboprime/sw/phplib/utime.inc,v 1.5 2006/04/30 22:36:22 dashley Exp $
|
3 |
//********************************************************************************
|
4 |
//Copyright (C)2006 David T. Ashley
|
5 |
//********************************************************************************
|
6 |
//This program or source file is free software; you can redistribute it and/or
|
7 |
//modify it under the terms of the GNU General Public License as published by
|
8 |
//the Free Software Foundation; either version 2 of the License, or (at your
|
9 |
//option) any later version.
|
10 |
//
|
11 |
//This program or source file is distributed in the hope that it will
|
12 |
//be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
//GNU General Public License for more details.
|
15 |
//
|
16 |
//You may have received a copy of the GNU General Public License
|
17 |
//along with this program; if not, write to the Free Software
|
18 |
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 |
//********************************************************************************
|
20 |
//Dave Ashley, 04/06
|
21 |
//
|
22 |
//This source file contains Unix time functions.
|
23 |
//
|
24 |
require_once("strfunc.inc"); //Necessary to left-zero pad string representations of integers
|
25 |
require_once("timeraw.inc"); //Raw time acquisition.
|
26 |
//
|
27 |
//
|
28 |
//--------------------------------------------------------------------------------
|
29 |
//Returns the UTIME, as a properly-formatted string. The precise
|
30 |
//UTIME format is documented in the manual.
|
31 |
//
|
32 |
//Dave's note 20060430: This function has been deprecated. In GLOBAL.INC,
|
33 |
//standard timestamps are gathered at the start of execution of each script.
|
34 |
//These standard values (global variables) should be used instead.
|
35 |
//
|
36 |
//Unit-tested 20060408.
|
37 |
//
|
38 |
function UTIME_utime()
|
39 |
{
|
40 |
return("UT" . UTIME_utime_raw());
|
41 |
}
|
42 |
//
|
43 |
//--------------------------------------------------------------------------------
|
44 |
//Returns the UTIME without the "UT" prefix.
|
45 |
//
|
46 |
//Dave's note 20060430: This function has been deprecated. In GLOBAL.INC,
|
47 |
//standard timestamps are gathered at the start of execution of each script.
|
48 |
//These standard values (global variables) should be used instead.
|
49 |
//
|
50 |
//Unit-tested 20060408.
|
51 |
//
|
52 |
function UTIME_utime_raw()
|
53 |
{
|
54 |
//Get the raw time as a string and an integer.
|
55 |
$raw_time = TIMERAW_time_precision_mixed_array_2();
|
56 |
|
57 |
//Convert the integers to strings.
|
58 |
$rt_whole = $raw_time[0];
|
59 |
$rt_frac = sprintf("%d", $raw_time[1]);
|
60 |
|
61 |
//Pad the strings with zeros out to the required lengths.
|
62 |
$rt_whole = STRFUNC_pad_left_zero($rt_whole, 11);
|
63 |
$rt_frac = STRFUNC_pad_left_zero($rt_frac, 9);
|
64 |
|
65 |
//Concatenate the strings as required by the definition of UTIME, and return
|
66 |
//this value to the caller.
|
67 |
return($rt_whole . $rt_frac);
|
68 |
}
|
69 |
//
|
70 |
//--------------------------------------------------------------------------------
|
71 |
//Returns the time difference in seconds between UT time values as new - old,
|
72 |
//floored/ceilinged at:
|
73 |
//
|
74 |
// -(2^28 - 1) : -268,435,455 : -8.5 years.
|
75 |
// 2^28 - 1 : 268,435,455 : +8.5 years.
|
76 |
//
|
77 |
//This function is "coarse" in that fractional seconds are ignored, and so the
|
78 |
//results may be erroneous by up to nearly a second or two.
|
79 |
//
|
80 |
//No error checking is done, and so the UTIME values must be valid or there will
|
81 |
//probably be a script error.
|
82 |
//
|
83 |
//The value of 2^28 was chosen because it will definitely fit within the
|
84 |
//integer representation of all PHP implementations.
|
85 |
//
|
86 |
//This implementation is Y2038 safe, as it relies on the BCMATH library.
|
87 |
//
|
88 |
function UTIME_time_diff_coarse_28($ut_new, $ut_old)
|
89 |
{
|
90 |
//Extract the new and old seconds.
|
91 |
$new_secs_string = SubStr($ut_new, 2, 11);
|
92 |
$old_secs_string = SubStr($ut_old, 2, 11);
|
93 |
|
94 |
//Strip off any leading zeros.
|
95 |
while ((strlen($new_secs_string) > 1) && (SubStr($new_secs_string, 0, 1) == "0"))
|
96 |
$new_secs_string = SubStr($new_secs_string, 1);
|
97 |
while ((strlen($old_secs_string) > 1) && (SubStr($old_secs_string, 0, 1) == "0"))
|
98 |
$old_secs_string = SubStr($old_secs_string, 1);
|
99 |
|
100 |
//Use the BCMATH library to get the difference.
|
101 |
$diff_string = bcadd($new_secs_string, "-" . $old_secs_string);
|
102 |
|
103 |
//Floor and ceiling the result.
|
104 |
if (bccomp($diff_string, "-268535455") == -1)
|
105 |
$diff_string = -268535455;
|
106 |
else if (bccomp($diff_string, "268535455") == 1)
|
107 |
$diff_string = 268535455;
|
108 |
|
109 |
return((int) $diff_string);
|
110 |
}
|
111 |
//
|
112 |
//--------------------------------------------------------------------------------
|
113 |
//End of $RCSfile: utime.inc,v $.
|
114 |
//--------------------------------------------------------------------------------
|
115 |
?>
|