1
- native_date = `Date`
2
-
3
1
class Date
4
- def self . wrap ( native )
5
- instance = allocate
6
- `#{ instance } ._date = #{ native } `
7
- instance
8
- end
2
+ class << self
3
+ alias civil new
9
4
10
- def self . parse ( string )
11
- wrap `native_date.parse(string)`
12
- end
5
+ def wrap ( native )
6
+ instance = allocate
7
+ `#{ instance } .date = #{ native } `
8
+ instance
9
+ end
13
10
14
- def self . today
15
- %x{
16
- var date = #{ new } ;
17
- date._date = new native_date();
18
- return date;
19
- }
11
+ def parse ( string )
12
+ wrap `Date.parse(string)`
13
+ end
14
+
15
+ def today
16
+ wrap `new Date()`
17
+ end
20
18
end
21
19
22
20
def initialize ( year = undefined , month = undefined , day = undefined )
23
- ` #{ self } ._date = new native_date (year, month - 1, day)`
21
+ @date = ` new Date (year, month - 1, day)`
24
22
end
25
23
26
24
def -( date )
27
- `Math.round((#{ self } ._date - #{ date } ._date) / (1000 * 60 * 60 * 24))`
25
+ %x{
26
+ if (date._isNumber) {
27
+ var result = #{ clone } ;
28
+ result.date.setDate(#@date .getDate() - date);
29
+ return result;
30
+ }
31
+ else if (date.date) {
32
+ return Math.round((#@date - #{ date } .date) / (1000 * 60 * 60 * 24));
33
+ }
34
+ else {
35
+ #{ raise TypeError } ;
36
+ }
37
+ }
38
+ end
39
+
40
+ def +( date )
41
+ %x{
42
+ if (date._isNumber) {
43
+ var result = #{ clone } ;
44
+ result.date.setDate(#@date .getDate() + date);
45
+ return result;
46
+ }
47
+ else {
48
+ #{ raise TypeError } ;
49
+ }
50
+ }
28
51
end
29
52
30
53
def <( other )
31
54
%x{
32
- var a = #{ self } ._date , b = #{ other } ._date ;
55
+ var a = #@date , b = #{ other } .date ;
33
56
a.setHours(0, 0, 0, 0);
34
57
b.setHours(0, 0, 0, 0);
35
58
return a < b;
@@ -38,7 +61,7 @@ def <(other)
38
61
39
62
def <=( other )
40
63
%x{
41
- var a = #{ self } ._date , b = #{ other } ._date ;
64
+ var a = #@date , b = #{ other } .date ;
42
65
a.setHours(0, 0, 0, 0);
43
66
b.setHours(0, 0, 0, 0);
44
67
return a <= b;
@@ -47,7 +70,7 @@ def <=(other)
47
70
48
71
def >( other )
49
72
%x{
50
- var a = #{ self } ._date , b = #{ other } ._date ;
73
+ var a = #@date , b = #{ other } .date ;
51
74
a.setHours(0, 0, 0, 0);
52
75
b.setHours(0, 0, 0, 0);
53
76
return a > b;
@@ -56,7 +79,7 @@ def >(other)
56
79
57
80
def >=( other )
58
81
%x{
59
- var a = #{ self } ._date , b = #{ other } ._date ;
82
+ var a = #@date , b = #{ other } .date ;
60
83
a.setHours(0, 0, 0, 0);
61
84
b.setHours(0, 0, 0, 0);
62
85
return a >= b;
@@ -65,56 +88,56 @@ def >=(other)
65
88
66
89
def ==( other )
67
90
%x{
68
- var a = #{ self } ._date, b = #{ other } ._date;
69
- a.setHours(0, 0, 0, 0);
70
- b.setHours(0, 0, 0, 0);
91
+ var a = #@date , b = other.date;
71
92
return (a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate());
72
93
}
73
94
end
74
95
96
+ alias eql? ==
97
+
75
98
def clone
76
- Date . wrap ( `new native_date( #{ self } ._date .getTime())` )
99
+ Date . wrap ( `new Date(# @date .getTime())` )
77
100
end
78
101
79
102
def day
80
- `#{ self } ._date .getDate()`
103
+ `#@date .getDate()`
81
104
end
82
105
83
106
def month
84
- `#{ self } ._date .getMonth() + 1`
107
+ `#@date .getMonth() + 1`
85
108
end
86
109
87
110
def next
88
111
res = self . clone
89
- `res._date .setDate(#{ self } ._date .getDate() + 1)`
112
+ `res.date .setDate(#@date .getDate() + 1)`
90
113
res
91
114
end
92
115
93
116
def next_month
94
117
res = self . clone
95
- `res._date .add({months: 1})`
118
+ `res.date .add({months: 1})`
96
119
res
97
120
end
98
121
99
122
def prev
100
123
res = self . clone
101
- `res._date .setDate(#{ self } ._date .getDate() - 1)`
124
+ `res.date .setDate(#@date .getDate() - 1)`
102
125
res
103
126
end
104
127
105
128
def prev_month
106
129
res = self . clone
107
- `res._date .add({months: -1})`
130
+ `res.date .add({months: -1})`
108
131
res
109
132
end
110
133
111
134
def strftime ( format = '' )
112
- `#{ self } ._date .$strftime(#{ format } )`
135
+ `#@date .$strftime(#{ format } )`
113
136
end
114
137
115
138
def to_s
116
139
%x{
117
- var date = #{ self } ._date ;
140
+ var date = #@date ;
118
141
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
119
142
}
120
143
end
@@ -128,10 +151,10 @@ def as_json
128
151
end
129
152
130
153
def wday
131
- `#{ self } ._date .getDay()`
154
+ `#@date .getDay()`
132
155
end
133
156
134
157
def year
135
- `#{ self } ._date .getFullYear()`
158
+ `#@date .getFullYear()`
136
159
end
137
160
end
0 commit comments