PageRenderTime 34ms CodeModel.GetById 24ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/std/Date.hx

http://github.com/Yoomee/clippy
Haxe | 192 lines | 97 code | 18 blank | 77 comment | 6 complexity | ee4456d4f5ca84a70f2e534367902494 MD5 | raw file
  1/*
  2 * Copyright (c) 2005, The haXe Project Contributors
  3 * All rights reserved.
  4 * Redistribution and use in source and binary forms, with or without
  5 * modification, are permitted provided that the following conditions are met:
  6 *
  7 *   - Redistributions of source code must retain the above copyright
  8 *     notice, this list of conditions and the following disclaimer.
  9 *   - Redistributions in binary form must reproduce the above copyright
 10 *     notice, this list of conditions and the following disclaimer in the
 11 *     documentation and/or other materials provided with the distribution.
 12 *
 13 * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 16 * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
 17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 23 * DAMAGE.
 24 */
 25
 26/**
 27	The Date class is used for date manipulation. There is some extra functions
 28	available in the [DateTools] class.
 29**/
 30
 31#if (cpp && !xmldoc)
 32typedef Date = cpp.CppDate__;
 33#else
 34
 35extern class Date
 36{
 37	/**
 38		Creates a new date object.
 39	**/
 40	function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void;
 41
 42	/**
 43		Returns the timestamp of the date. It's the number of milliseconds
 44		elapsed since 1st January 1970. It might only have a per-second precision
 45		depending on the platforms.
 46	**/
 47	function getTime() : Float;
 48
 49	/**
 50		Returns the hours value of the date (0-23 range).
 51	**/
 52	function getHours() : Int;
 53
 54	/**
 55		Returns the minutes value of the date (0-59 range).
 56	**/
 57	function getMinutes() : Int;
 58
 59	/**
 60		Returns the seconds of the date (0-59 range).
 61	**/
 62	function getSeconds() : Int;
 63
 64	/**
 65		Returns the full year of the date.
 66	**/
 67	function getFullYear() : Int;
 68
 69	/**
 70		Returns the month of the date (0-11 range).
 71	**/
 72	function getMonth() : Int;
 73
 74	/**
 75		Returns the day of the date (1-31 range).
 76	**/
 77	function getDate() : Int;
 78
 79	/**
 80		Returns the week day of the date (0-6 range).
 81	**/
 82	function getDay() : Int;
 83
 84	/**
 85		Returns a string representation for the Date, by using the
 86		standard format [YYYY-MM-DD HH:MM:SS]. See [DateTools.format] for
 87		other formating rules.
 88	**/
 89	function toString():String;
 90
 91	/**
 92		Returns a Date representing the current local time.
 93	**/
 94	static function now() : Date;
 95
 96	/**
 97		Returns a Date from a timestamp [t] which is the number of
 98		milliseconds elapsed since 1st January 1970.
 99	**/
100	static function fromTime( t : Float ) : Date;
101
102	/**
103		Returns a Date from a formated string of one of the following formats :
104		[YYYY-MM-DD hh:mm:ss] or [YYYY-MM-DD] or [hh:mm:ss]. The first two formats
105		are expressed in local time, the third in UTC Epoch.
106	**/
107	static function fromString( s : String ) : Date;
108
109	#if flash_lite
110	/** flash lite only **/
111	function getLocaleLongDate():String;
112	/** flash lite only **/
113	function getLocaleShortDate():String;
114	/** flash lite only **/
115	function getLocaleTime():String;
116	#end
117
118#if !php
119	private static function __init__() : Void untyped {
120	#if neko
121		Date = neko.NekoDate__;
122		neko.Boot.__classes.Date = Date;
123	#else
124		Date.now = function() {
125			return __new__(Date);
126		};
127		Date.fromTime = function(t){
128			var d : Date = __new__(Date);
129			#if flash9
130			d.setTime(t);
131			#else
132			d["setTime"]( t );
133			#end
134			return d;
135		};
136		Date.fromString = function(s : String) {
137			switch( s.length ) {
138			case 8: // hh:mm:ss
139				var k = s.split(":");
140				var d : Date = __new__(Date);
141				#if flash9
142				d.setTime(0);
143				d.setUTCHours(k[0]);
144				d.setUTCMinutes(k[1]);
145				d.setUTCSeconds(k[2]);
146				#else
147				d["setTime"](0);
148				d["setUTCHours"](k[0]);
149				d["setUTCMinutes"](k[1]);
150				d["setUTCSeconds"](k[2]);
151				#end
152				return d;
153			case 10: // YYYY-MM-DD
154				var k = s.split("-");
155				return new Date(cast k[0],cast k[1] - 1,cast k[2],0,0,0);
156			case 19: // YYYY-MM-DD hh:mm:ss
157				var k = s.split(" ");
158				var y = k[0].split("-");
159				var t = k[1].split(":");
160				return new Date(cast y[0],cast y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
161			default:
162				throw "Invalid date format : " + s;
163			}
164		};
165		Date.prototype["toString"] = function() {
166			var date : Date = this;
167			var m = date.getMonth() + 1;
168			var d = date.getDate();
169			var h = date.getHours();
170			var mi = date.getMinutes();
171			var s = date.getSeconds();
172			return date.getFullYear()
173				+"-"+(if( m < 10 ) "0"+m else ""+m)
174				+"-"+(if( d < 10 ) "0"+d else ""+d)
175				+" "+(if( h < 10 ) "0"+h else ""+h)
176				+":"+(if( mi < 10 ) "0"+mi else ""+mi)
177				+":"+(if( s < 10 ) "0"+s else ""+s);
178		};
179		#if flash9
180		#elseif flash
181		Date.prototype[__unprotect__("__class__")] = Date;
182		Date[__unprotect__("__name__")] = ["Date"];
183		#else
184		Date.prototype.__class__ = Date;
185		Date.__name__ = ["Date"];
186		#end
187	#end
188	}
189#end
190}
191
192#end // !cpp