/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

interface READERTimeInterface {
 public function __construct(READER $reader = NULL, $opts = false);
 public function ImportTime(DateTime $dt);
 public function ExportTime($db_time);
 public function GetTimeSlicingFunction($db_time_var, $width, $offset = 0);
}


abstract class NULLReaderTime implements READERTimeInterface {
 var $gmt_timezone;

 public function __construct(READER $reader = NULL, $opts = false) {
    if ($reader) $this->gmt_timezone = $reader->gmt_timezone;
    else $this->gmt_timezone = new DateTimeZone("GMT");
 }

 public function ImportTime(DateTime $dt) {
    throw new ADEIException(translate("CLASS %s: Methods (%s) is not implemented", get_class($this),  __METHOD__));
 }

 public function ExportTime($db_time) {
    throw new ADEIException(translate("CLASS %s: Methods (%s) is not implemented", get_class($this),  __METHOD__));
 }

 public function GetTimeSlicingFunction($db_time_var, $width, $offset = 0) {
    throw new ADEIException(translate("CLASS %s: Methods (%s) is not implemented", get_class($this),  __METHOD__));
 }

 function ImportUnixTime($unix_time) {
    $itime = floor($unix_time);

    if ($itime==$unix_time) {
            // Time zone should be specified, esle "e" output is invalid
	return $this->ImportTime(new DateTime("@" . sprintf("%f", $itime), $this->gmt_timezone));
    } else {
	if (is_float($unix_time)) $subsec = strchr(sprintf("%F", $unix_time), '.');
	else $subsec = strchr($unix_time, '.');
	
	    // DS: Due to the bug in PHP (precision is limited to 10ns)
	if (strlen($subsec) > 9) $subsec = substr($subsec, 0, 9);
	return $this->ImportTime(new DateTime(strftime("%Y-%m-%dT%H:%M:%S", $itime) . $subsec, $this->gmt_timezone));
//	return $this->ImportTime(new DateTime(strftime("%Y/%m/%d %H:%M:%S", $itime) . $subsec, $this->gmt_timezone));
    }
 }
 
 function ExportUnixTime($db_time) {
    $dt = $this->ExportTime($db_time);
    return $dt->format("U.u");
 }
}

class READERTime extends NULLReaderTime implements READERTimeInterface {
 var $time_zone;
 var $time_format;
 
 public function __construct(READER $reader = NULL, $opts = false) {
    parent::__construct($reader, $opts);

    $this->time_format = $opts['format'];
    $this->time_zone = $opts['timezone'];
 }
 
 public function ImportTime(DateTime $dt) {
    $dt->setTimezone($this->time_zone);
    return $dt->format($this->time_format);
 }
 
 public function ExportTime($db_time) {
    if ($this->time_zone) {
        $ctz = date_default_timezone_get();
	date_default_timezone_set($this->time_zone->getName());
	switch ($this->time_format) {
	    case "U":
		$dt = new DateTime("@" . $db_time);
	    break;
	    default:
		$dt = new DateTime($db_time);
	}
	date_default_timezone_set($ctz);

//	$dt->setTimezone($this->gmt_timezone);
	return $dt;
    }

    switch ($this->time_format) {
	case "U":
	    return new DateTime("@" . $db_time);
	default:
	    return new DateTime($db_time);
    }
 }

 function GetTimeSlicingFunction($db_time_var, $width, $offset = 0) {
    if ($offset) {
	if (is_object($offset)) 
	    $off = $this->ImportTime($offset);
	else
	    $off = $this->ImportUnixTime($offset);

	return "($db_time_var - $off) / $width";
    } 
    
    return "$db_time_var / $width";
 }
}



?>