-
-
Notifications
You must be signed in to change notification settings - Fork 655
/
Date.hx
174 lines (152 loc) · 4.82 KB
/
Date.hx
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (C)2005-2017 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
The Date class provides a basic structure for date and time related
information. Date instances can be created by
- `new Date()` for a specific date,
- `Date.now()` to obtain information about the current time,
- `Date.fromTime()` with a given timestamp or
- `Date.fromString()` by parsing from a String.
There are some extra functions available in the `DateTools` class.
In the context of Haxe dates, a timestamp is defined as the number of
milliseconds elapsed since 1st January 1970.
**/
extern class Date
{
/**
Creates a new date object from the given arguments.
The behaviour of a Date instance is only consistent across platforms if
the the arguments describe a valid date.
- month: 0 to 11
- day: 1 to 31
- hour: 0 to 23
- min: 0 to 59
- sec: 0 to 59
**/
function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void;
/**
Returns the timestamp (in milliseconds) of the date. It might
only have a per-second precision depending on the platforms.
**/
function getTime() : Float;
/**
Returns the hours of `this` Date (0-23 range).
**/
function getHours() : Int;
/**
Returns the minutes of `this` Date (0-59 range).
**/
function getMinutes() : Int;
/**
Returns the seconds of `this` Date (0-59 range).
**/
function getSeconds() : Int;
/**
Returns the full year of `this` Date (4-digits).
**/
function getFullYear() : Int;
/**
Returns the month of `this` Date (0-11 range).
**/
function getMonth() : Int;
/**
Returns the day of `this` Date (1-31 range).
**/
function getDate() : Int;
/**
Returns the day of the week of `this` Date (0-6 range) where `0` is Sunday.
**/
function getDay() : Int;
/**
Returns a string representation of `this` Date, by using the
standard format [YYYY-MM-DD HH:MM:SS]. See `DateTools.format` for
other formating rules.
**/
function toString():String;
/**
Returns a Date representing the current local time.
**/
static function now() : Date;
/**
Returns a Date from timestamp (in milliseconds) `t`.
**/
static function fromTime( t : Float ) : Date;
/**
Returns a Date from a formatted string `s`, with the following accepted
formats:
- `"YYYY-MM-DD hh:mm:ss"`
- `"YYYY-MM-DD"`
- `"hh:mm:ss"`
The first two formats are expressed in local time, the third in UTC
Epoch.
**/
static function fromString( s : String ) : Date;
#if flash
private static function __init__() : Void untyped {
var d : Dynamic = Date;
d.now = function() {
return __new__(Date);
};
d.fromTime = function(t){
var d : Date = __new__(Date);
d.setTime(t);
return d;
};
d.fromString = function(s : String) {
switch( s.length ) {
case 8: // hh:mm:ss
var k = s.split(":");
var d : Date = __new__(Date);
d.setTime(0);
d.setUTCHours(k[0]);
d.setUTCMinutes(k[1]);
d.setUTCSeconds(k[2]);
return d;
case 10: // YYYY-MM-DD
var k = s.split("-");
return new Date(cast k[0],cast k[1] - 1,cast k[2],0,0,0);
case 19: // YYYY-MM-DD hh:mm:ss
var k = s.split(" ");
var y = k[0].split("-");
var t = k[1].split(":");
return new Date(cast y[0],cast y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
default:
throw "Invalid date format : " + s;
}
};
d.prototype[#if (as3 || no_flash_override) "toStringHX" #else "toString" #end] = function() {
var date : Date = __this__;
var m = date.getMonth() + 1;
var d = date.getDate();
var h = date.getHours();
var mi = date.getMinutes();
var s = date.getSeconds();
return date.getFullYear()
+"-"+(if( m < 10 ) "0"+m else ""+m)
+"-"+(if( d < 10 ) "0"+d else ""+d)
+" "+(if( h < 10 ) "0"+h else ""+h)
+":"+(if( mi < 10 ) "0"+mi else ""+mi)
+":"+(if( s < 10 ) "0"+s else ""+s);
};
}
#end
}