Access keysTop, Summary, Constructors,
Class properties,
Instance properties,
Instance methodsTime
| Kind of class: |
class |
| Inherits from: |
BasicClass
|
| Version: |
1.0 |
| Author: |
Martin Heidegger |
| Classpath: |
org.as2lib.data.type.Time |
| File last modified: |
Thursday, 15 September 2005, 18:43:44 |
/*
* Copyright the original author or authors.
*
* Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.mozilla.org/MPL/MPL-1.1.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.as2lib.core.BasicClass;
import org.as2lib.util.MathUtil;
/**
* {@code Time} is a holder for a time difference.
*
* <p>{@code Time} splits a time difference (distance between two dates) into
* days, hours, minutes, seconds and milliseconds to offers methods to access
* the time difference value.
*
* <p>There are two ways to access the {@code Time} instance:
*
* <p>The first way is by {@code in*()}({@code inHours()}, {@code inMinutes()},...).
* Those methods are coversions to the different time units. You can recieve the
* complete value in a different unit.
*
* Example:
* <code>
* var time:Time = new Time(1.5, "d");
* trace(time.inDays()); // 1.5
* trace(time.inHours()); // 36
* trace(time.inMinutes()); // 2160
* </code>
*
* <p>The second way is by {@code get*()}({@code getHours()}, {@code getMinutes()},...).
* Those methods contain only the part of each unit that is contained within the
* value.
*
* Example:
* <code>
* var time:Time = new Time(1.5, "d");
* trace(time.getDays()); // 1.5
* trace(time.getHours()); // 12
* trace(time.getMinutes(); // 0
* </code>
*
* <p>Its possible to pass-in a number in to round the value to the next lower case:
*
* Example:
* <code>
* var time:Time = new Time(1.5, "d");
* trace(time.getDays(0)); // 1
* </code>
*
* @author Martin Heidegger
* @version 1.0
*/
class org.as2lib.data.type.Time extends BasicClass {
/** Factor from ms to second. */
private static var SECOND:Number = 1000;
/** Factor from ms to minute. */
private static var MINUTE:Number = SECOND*60;
/** Factor from ms to hour. */
private static var HOUR:Number = MINUTE*60;
/** Factor from ms to day. */
private static var DAY:Number = HOUR*24;
/** Time difference in ms. */
private var ms:Number;
/** Amount of days. */
private var days:Number;
/** Amount of hours. */
private var hours:Number;
/** Amount of minutes. */
private var minutes:Number;
/** Amount of seconds. */
private var seconds:Number;
/** Amount of milliseconds. */
private var milliSeconds:Number;
/** Flag if the instance need to be evaluated by {@link #evaluate}. */
private var doEval:Boolean = true;
/**
* Constructs a new {@code Time} instance.
*
* <p>Uses "ms" if no format or a wrong format was passed-in.
*
* <p>Uses {@code Number.MAX_VALUE} if {@code Infinity} was passed-in.
*
*
* @param time size of the time difference for the passed-in {@code format}
* @param format (optional) "d"/"h"/"m"/"s"/"ms" for the unit of the amout,
* default case is "ms"
*/
public function Time(timeDifference:Number, format:String) {
setValue(timeDifference, format);
}
/**
* Sets the time of the instance.
*
* <p>Uses "ms" if no format or a wrong format was passed-in.
*
* <p>Uses {@code Number.MAX_VALUE} if {@code Infinity} was passed-in.
*
* @param time size of the time difference for the passed-in {@code format}
* @param format (optional) "d"/"h"/"m"/"s"/"ms" for the unit of the amout.
* Default value is ms.
*/
public function setValue(timeDifference:Number, format:String):Time {
if (timeDifference == Infinity) {
timeDifference = Number.MAX_VALUE;
}
switch (format) {
case "d":
ms = timeDifference*DAY;
break;
case "h":
ms = timeDifference*HOUR;
break;
case "m":
ms = timeDifference*MINUTE;
break;
case "s":
ms = timeDifference*SECOND;
break;
default:
ms = timeDifference;
}
doEval = true;
return this;
}
/**
* Adds the passed-in {@code timedistance} to the current time.
*
* @param timeDifference time difference to be added to the current time
* @return new instance with the resulting amount of time
*/
public function plus(timeDifference:Time):Time {
return new Time(ms+timeDifference.valueOf());
}
/**
* Adds the passed-in {@code timeDifference} from the current time.
*
* @param timeDifference time difference to be removed from the current time
* @return new instance with the resulting amount of time
*/
public function minus(timeDifference:Time):Time {
return new Time(ms-timeDifference.valueOf());
}
/**
* Getter for the amount of milliseconds are contained within the time.
*
* <p>It will not round the result if you pass-in nothing.
*
* @param round (optional) the number of decimal spaces
* @return time difference in milliseconds
*/
public function getMilliSeconds(round:Number):Number {
if (doEval) evaluate();
if (round == null) {
return milliSeconds;
} else {
return MathUtil.floor(milliSeconds, round);
}
}
/**
* Getter for the time distance in milliseconds.
*
* @return time difference in milliseconds
*/
public function inMilliSeconds(Void):Number {
return ms;
}
/**
* Getter for the amount of seconds are contained within the time.
*
* <p>It will not round the result if you pass-in nothing.
*
* @param round (optional) the number of decimal spaces
* @return time difference in seconds
*/
public function getSeconds(round:Number):Number {
if (doEval) evaluate();
if (round == null) {
return seconds;
} else {
return MathUtil.floor(seconds, round);
}
}
/**
* Getter for the time distance in seconds.
*
* @return time difference in seconds
*/
public function inSeconds(Void):Number {
return ms/SECOND;
}
/**
* Getter for the amount of minutes are contained within the time.
*
* <p>It will not round the result if you pass-in nothing.
*
* @param round (optional) the number of decimal spaces
* @return time difference in minutes
*/
public function getMinutes(round:Number):Number {
if (doEval) evaluate();
if (round == null) {
return minutes;
} else {
return MathUtil.floor(minutes, round);
}
}
/**
* Getter for the time distance in minutes.
*
* @return time difference in minutes
*/
public function inMinutes(Void):Number {
return ms/MINUTE;
}
/**
* Getter for the amount of hours are contained within the time.
*
* <p>It will not round the result if you pass-in nothing.
*
* @param round (optional) the number of decimal spaces
* @return time difference in hours
*/
public function getHours(round:Number):Number {
if (doEval) evaluate();
if (round == null) {
return hours;
} else {
return MathUtil.floor(hours, round);
}
}
/**
* Getter for the time distance in hours.
*
* @return time difference in hours
*/
public function inHours(Void):Number {
return ms/HOUR;
}
/**
* Getter for the amount of days are contained within the time.
*
* <p>It will not round the result if you pass-in nothing.
*
* @param round (optional) the number of decimal spaces
* @return time difference in days
*/
public function getDays(round:Number):Number {
if (doEval) evaluate();
if (round == null) {
return days;
} else {
return MathUtil.floor(days, round);
}
}
/**
* Getter for the time distance in days.
*
* @return time difference in days
*/
public function inDays(Void):Number {
return ms/DAY;
}
/**
* Generates String representation of the time.
*
* @return time difference as string
*/
public function toString():String {
return getDays(0)+"d "+getHours(0)+":"+getMinutes(0)+":"+getSeconds(0)+"."+getMilliSeconds(0);
}
/**
* Splits the time distance from ms (source value) into the different units.
*/
private function evaluate(Void):Void {
var negative = (ms >= 0) ? 1 : -1;
var rest:Number = ms;
days = rest/DAY;
rest -= negative*Math.floor(days)*DAY;
hours = rest/HOUR;
rest -= negative*Math.floor(hours)*HOUR;
minutes = rest/MINUTE;
rest -= negative*Math.floor(minutes)*MINUTE;
seconds = rest/SECOND;
rest -= negative*Math.floor(seconds)*SECOND;
milliSeconds = rest;
doEval = false;
}
/**
* Returns the value of the time distance (in ms).
*
* @return value in ms
*/
public function valueOf():Number {
return ms;
}
}
Time is a holder for a time difference.
Time splits a time difference (distance between two dates) into
days, hours, minutes, seconds and milliseconds to offers methods to access
the time difference value.
There are two ways to access the Time instance:
The first way is by in*()(inHours(), inMinutes(),...).
Those methods are coversions to the different time units. You can recieve the
complete value in a different unit.
Example:
var time:Time = new Time(1.5, "d");
trace(time.inDays());
trace(time.inHours());
trace(time.inMinutes());
The second way is by get*()(getHours(), getMinutes(),...).
Those methods contain only the part of each unit that is contained within the
value.
Example:
var time:Time = new Time(1.5, "d");
trace(time.getDays());
trace(time.getHours());
trace(time.getMinutes();
Its possible to pass-in a number in to round the value to the next lower case:
Example:
var time:Time = new Time(1.5, "d");
trace(time.getDays(0));
Constructor
Time
function Time (
timeDifference:Number,
format:String)
Constructs a new
Time instance.
Uses "ms" if no format or a wrong format was passed-in.
Uses Number.MAX_VALUE if Infinity was passed-in.
Parameters:
time :
size of the time difference for the passed-in format
format:
(optional) "d"/"h"/"m"/"s"/"ms" for the unit of the amout,
default case is "ms"
Class properties
DAY
static private DAY:Number = HOUR*24
(read,write)
Factor from ms to day.
HOUR
static private HOUR:Number = MINUTE*60
(read,write)
Factor from ms to hour.
MINUTE
static private MINUTE:Number = SECOND*60
(read,write)
Factor from ms to minute.
SECOND
static private SECOND:Number = 1000
(read,write)
Factor from ms to second.
Instance properties
days
private days:Number
(read,write)
Amount of days.
doEval
private doEval:Boolean = true
(read,write)
Flag if the instance need to be evaluated by
evaluate.
hours
private hours:Number
(read,write)
Amount of hours.
milliSeconds
private milliSeconds:Number
(read,write)
Amount of milliseconds.
minutes
private minutes:Number
(read,write)
Amount of minutes.
ms
private ms:Number
(read,write)
Time difference in ms.
seconds
private seconds:Number
(read,write)
Amount of seconds.
Instance methods
evaluate
private function evaluate (
Void) : Void
Splits the time distance from ms (source value) into the different units.
getDays
function getDays (
round:Number) : Number
Getter for the amount of days are contained within the time.
It will not round the result if you pass-in nothing.
Parameters:
round:
(optional) the number of decimal spaces
Returns:time difference in days
getHours
function getHours (
round:Number) : Number
Getter for the amount of hours are contained within the time.
It will not round the result if you pass-in nothing.
Parameters:
round:
(optional) the number of decimal spaces
Returns:time difference in hours
getMilliSeconds
function getMilliSeconds (
round:Number) : Number
Getter for the amount of milliseconds are contained within the time.
It will not round the result if you pass-in nothing.
Parameters:
round:
(optional) the number of decimal spaces
Returns:time difference in milliseconds
getMinutes
function getMinutes (
round:Number) : Number
Getter for the amount of minutes are contained within the time.
It will not round the result if you pass-in nothing.
Parameters:
round:
(optional) the number of decimal spaces
Returns:time difference in minutes
getSeconds
function getSeconds (
round:Number) : Number
Getter for the amount of seconds are contained within the time.
It will not round the result if you pass-in nothing.
Parameters:
round:
(optional) the number of decimal spaces
Returns:time difference in seconds
inDays
function inDays (
Void) : Number
Getter for the time distance in days.
Returns:time difference in days
inHours
function inHours (
Void) : Number
Getter for the time distance in hours.
Returns:time difference in hours
inMilliSeconds
function inMilliSeconds (
Void) : Number
Getter for the time distance in milliseconds.
Returns:time difference in milliseconds
inMinutes
function inMinutes (
Void) : Number
Getter for the time distance in minutes.
Returns:time difference in minutes
inSeconds
function inSeconds (
Void) : Number
Getter for the time distance in seconds.
Returns:time difference in seconds
minus
Adds the passed-in timeDifference from the current time.
Parameters:
timeDifference:
time difference to be removed from the current time
Returns:new instance with the resulting amount of time
plus
Adds the passed-in timedistance to the current time.
Parameters:
timeDifference:
time difference to be added to the current time
Returns:new instance with the resulting amount of time
setValue
function setValue (
timeDifference:Number,
format:String) : Time
Sets the time of the instance.
Uses "ms" if no format or a wrong format was passed-in.
Uses Number.MAX_VALUE if Infinity was passed-in.
Parameters:
time :
size of the time difference for the passed-in format
format:
(optional) "d"/"h"/"m"/"s"/"ms" for the unit of the amout.
Default value is ms.
toString
function toString (
) : String
Generates String representation of the time.
Returns:time difference as string
valueOf
function valueOf (
) : Number
Returns the value of the time distance (in ms).