global statement removal: hphp/test [7/x]
[hiphop-php.git] / hphp / test / quick / builtin_extension_DateTime.php
blobda656c92fb5927d2a87bffe635dca372bfdd8ddb
1 <?hh
3 date_default_timezone_set('America/Los_Angeles');
5 class A_DateTime extends DateTime {
6 public $___x;
7 public function __clone() {
8 $this->___x++;
12 function test($cls, $args = array()) {
13 echo $cls . "\n";
14 $a = (new ReflectionClass($cls))->newInstanceArgs($args);
15 var_dump($a);
16 // serialize and unserialize
17 $b = serialize($a);
18 var_dump($b);
19 $c = unserialize($b);
20 var_dump($c);
21 if (($a != $c) && (get_class($c) !== null)) {
22 echo "bad serialization/deserialization\n";
23 exit(1);
25 // get class methods
26 var_dump(get_class_methods($a));
28 echo "================\n";
30 $cls = 'A_' . $cls;
31 echo $cls . "\n";
32 $a = (new ReflectionClass($cls))->newInstanceArgs($args);
33 var_dump($a);
34 // serialize and unserialize
35 $b = serialize($a);
36 var_dump($b);
37 $c = unserialize($b);
38 var_dump($c);
39 if (($a != $c) && (get_class($c) !== null)) {
40 echo "bad serialization/deserialization\n";
41 exit(1);
43 // get class methods
44 var_dump(get_class_methods($a));
47 test("DateTime", array("2012-06-23T11:00:00"));
49 function main() {
50 echo "================\n";
52 $y = new A_DateTime("2012-06-23T11:00:00");
53 $y->___y = 73;
54 $y2 = clone $y;
55 $y2->___y++;
56 $y2->modify("+3 days");
58 var_dump($y);
59 var_dump($y->format('Y-m-d'));
60 var_dump($y2);
61 var_dump($y2->format('Y-m-d'));
63 main();
65 function DataTimeFromString() {
66 echo "================\n";
67 $num_tests = 10;
68 $count_fill_error = 0;
69 $count_nofill_error = 0;
70 for ($i = 0; $i < $num_tests; $i++) {
71 date_default_timezone_set('UTC');
72 $curr_str = date('H:i:s');
73 $format = 'Y-m-d';
74 $rand_date = sprintf('%d-%02d-%02d',
75 rand(1900,2100),
76 rand(1,12),
77 rand(1,28));
78 $date = DateTime::createFromFormat($format, $rand_date)
79 ->format('H:i:s');
80 if ($date != $curr_str) {
81 // passing the above condition may be the result of change in
82 // the system time. assuming running on a reasonable fast CPU
83 // time cannot change on the second sample
84 $curr_str = date('H:i:s');
85 if ($date != $curr_str) {
86 $count_fill_error++;
89 if ('00:00:00' == $curr_str) {
90 // just in case make sure the current time is not exact midnight!
91 sleep(1);
92 $curr_str = date('H:i:s');
94 // adding ! to the time format force to fill missing parts by
95 // parts of Unix epoch is 1970-01-01 00:00:00 UTC
96 $format = '!'.$format;
97 $date = DateTime::createFromFormat($format, $rand_date)
98 ->format('H:i:s');
99 if ('00:00:00' != $date) {
100 $count_no_fill_error++;
103 if ($num_tests == $count_fill_error) {
104 echo 'Error in createFromFormat when filling option is ON\n';
105 echo $count_fill_error;
108 if ($num_tests == $count_nofill_error){
109 echo 'Error in createFromFormat when filling option is OFF\n';
113 DataTimeFromString();