summaryrefslogtreecommitdiff
path: root/tests/UnitTests/framework/Data/utDateTimeSimpleFormatter.php
blob: b186e3ff6892efa5ec404ebb2f5c33d42a7600d2 (plain)
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
<?php

// NOTE: This file must be saved with charset GB2312
require_once(dirname(__FILE__).'/../common.php');

Prado::using('System.Data.TDateTimeSimpleFormatter');

class utDateTimeSimpleFormatter extends UnitTestCase
{
	function testFormatting()
	{
		$time = mktime(0,0,0,12,30,2005);
		$pattern = "dd-MM-yyyy";
		$expect = '30-12-2005';
		
		$formatter = new TDateTimeSimpleFormatter($pattern);
		$this->assertEqual($expect, $formatter->format($time));

		$time = mktime(0,0,0,5,6,2005);
		$pattern = "d-M-yy";
		$expect = "6-5-05";

		$formatter->setPattern($pattern);
		$this->assertEqual($expect, $formatter->format($time));

		$pattern = "dd-MM-yy";
		$expect = "06-05-05";

		$formatter->setPattern($pattern);
		$this->assertEqual($expect, $formatter->format($time));

		$pattern = "yyyy年MM月dd日";
		$expect = "2005年05月06日";
		
		$formatter = new TDateTimeSimpleFormatter($pattern, 'GB2312');
		$this->assertEqual($expect, $formatter->format($time));

		$pattern = "MM/dd/yyyy";
		$expect = "05/06/2005";
		
		$formatter = new TDateTimeSimpleFormatter($pattern, 'UTF-8');
		$this->assertEqual($expect, $formatter->format($time));

	}

	function testParsing()
	{
		$pattern = "yyyy年MM月dd日";
		$value = "2005年05月06日";
		$expect = mktime(0,0,0,5,6,2005);

		$formatter = new TDateTimeSimpleFormatter($pattern, 'GB2312');
		$this->assertEqual($expect, $formatter->parse($value));

		$pattern = "dd-MM-yy";
		$value= "06-05-05";
		
		$formatter = new TDateTimeSimpleFormatter($pattern);
		$this->assertEqual($expect, $formatter->parse($value));

		$pattern = "d-M-yy";
		$value = "6-5-05";
		$formatter = new TDateTimeSimpleFormatter($pattern);
		$this->assertEqual($expect, $formatter->parse($value));

		$pattern = "MM/dd/yyyy";
		$value = "05/06/2005";
		$formatter = new TDateTimeSimpleFormatter($pattern);
		$this->assertEqual($expect, $formatter->parse($value));


		$pattern = "dd-MM-yyyy";
		$value = '30-12-2005';
		$expect = mktime(0,0,0,12,30,2005);

		$formatter = new TDateTimeSimpleFormatter($pattern);
		$this->assertEqual($expect, $formatter->parse($value));
	}
}

?>