initial commit
[CPE_learningsite] / CPEFlex / EngagementPod / src / models / TimeSpan.as
1 package models
2 {
3                 /**
4                 * Represents an interval of time 
5                 */ 
6                 public class TimeSpan
7                 {
8                         private var _totalMilliseconds : Number;
9                         
10                         public function TimeSpan(milliseconds : Number)
11                         {
12                                 _totalMilliseconds = Math.floor(milliseconds);
13                         }
14                         
15                         /**
16                          * Gets the number of whole days
17                          * 
18                          * @example In a TimeSpan created from TimeSpan.fromHours(25), 
19                          *          totalHours will be 1.04, but hours will be 1 
20                          * @return A number representing the number of whole days in the TimeSpan
21                          */
22                         public function get days() : int
23                         {
24                                 return int(_totalMilliseconds / MILLISECONDS_IN_DAY);
25                         }
26                         
27                         /**
28                          * Gets the number of whole hours (excluding entire days)
29                          * 
30                          * @example In a TimeSpan created from TimeSpan.fromMinutes(1500), 
31                          *          totalHours will be 25, but hours will be 1 
32                          * @return A number representing the number of whole hours in the TimeSpan
33                          */
34                         public function get hours() : int
35                         {
36                                 return int(_totalMilliseconds / MILLISECONDS_IN_HOUR) % 24;
37                         }
38                         
39                         /**
40                          * Gets the number of whole minutes (excluding entire hours)
41                          * 
42                          * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
43                          *          totalSeconds will be 65.5, but seconds will be 5 
44                          * @return A number representing the number of whole minutes in the TimeSpan
45                          */
46                         public function get minutes() : int
47                         {
48                                 return int(_totalMilliseconds / MILLISECONDS_IN_MINUTE) % 60; 
49                         }
50                         
51                         /**
52                          * Gets the number of whole seconds (excluding entire minutes)
53                          * 
54                          * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
55                          *          totalSeconds will be 65.5, but seconds will be 5 
56                          * @return A number representing the number of whole seconds in the TimeSpan
57                          */
58                         public function get seconds() : int
59                         {
60                                 return int(_totalMilliseconds / MILLISECONDS_IN_SECOND) % 60;
61                         }
62                         
63                         /**
64                          * Gets the number of whole milliseconds (excluding entire seconds)
65                          * 
66                          * @example In a TimeSpan created from TimeSpan.fromMilliseconds(2123), 
67                          *          totalMilliseconds will be 2001, but milliseconds will be 123 
68                          * @return A number representing the number of whole milliseconds in the TimeSpan
69                          */
70                         public function get milliseconds() : int
71                         {
72                                 return int(_totalMilliseconds) % 1000;
73                         }
74                         
75                         /**
76                          * Gets the total number of days.
77                          * 
78                          * @example In a TimeSpan created from TimeSpan.fromHours(25), 
79                          *          totalHours will be 1.04, but hours will be 1 
80                          * @return A number representing the total number of days in the TimeSpan
81                          */
82                         public function get totalDays() : Number
83                         {
84                                 return _totalMilliseconds / MILLISECONDS_IN_DAY;
85                         }
86                         
87                         /**
88                          * Gets the total number of hours.
89                          * 
90                          * @example In a TimeSpan created from TimeSpan.fromMinutes(1500), 
91                          *          totalHours will be 25, but hours will be 1 
92                          * @return A number representing the total number of hours in the TimeSpan
93                          */
94                         public function get totalHours() : Number
95                         {
96                                 return _totalMilliseconds / MILLISECONDS_IN_HOUR;
97                         }
98                         
99                         /**
100                          * Gets the total number of minutes.
101                          * 
102                          * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
103                          *          totalSeconds will be 65.5, but seconds will be 5 
104                          * @return A number representing the total number of minutes in the TimeSpan
105                          */
106                         public function get totalMinutes() : Number
107                         {
108                                 return _totalMilliseconds / MILLISECONDS_IN_MINUTE;
109                         }
110                         
111                         /**
112                          * Gets the total number of seconds.
113                          * 
114                          * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500), 
115                          *          totalSeconds will be 65.5, but seconds will be 5 
116                          * @return A number representing the total number of seconds in the TimeSpan
117                          */
118                         public function get totalSeconds() : Number
119                         {
120                                 return _totalMilliseconds / MILLISECONDS_IN_SECOND;
121                         }
122                         
123                         /**
124                          * Gets the total number of milliseconds.
125                          * 
126                          * @example In a TimeSpan created from TimeSpan.fromMilliseconds(2123), 
127                          *          totalMilliseconds will be 2001, but milliseconds will be 123 
128                          * @return A number representing the total number of milliseconds in the TimeSpan
129                          */
130                         public function get totalMilliseconds() : Number
131                         {
132                                 return _totalMilliseconds;
133                         }
134                         
135                         /**
136                          * Adds the timespan represented by this instance to the date provided and returns a new date object.
137                          * @param date The date to add the timespan to
138                          * @return A new Date with the offseted time
139                          */     
140                         public function add(date : Date) : Date
141                         {
142                                 var ret : Date = new Date(date.time);
143                                 ret.milliseconds += totalMilliseconds;
144                                 
145                                 return ret;
146                         }
147                         
148                         /**
149                          * Creates a TimeSpan from the different between two dates
150                          * 
151                          * Note that start can be after end, but it will result in negative values. 
152                          *  
153                          * @param start The start date of the timespan
154                          * @param end The end date of the timespan
155                          * @return A TimeSpan that represents the difference between the dates
156                          * 
157                          */     
158                         public static function fromDates(start : Date, end : Date) : TimeSpan
159                         {
160                                 return new TimeSpan(end.time - start.time);
161                         }
162                         
163                         /**
164                          * Creates a TimeSpan from the specified number of milliseconds
165                          * @param milliseconds The number of milliseconds in the timespan
166                          * @return A TimeSpan that represents the specified value
167                          */     
168                         public static function fromMilliseconds(milliseconds : Number) : TimeSpan
169                         {
170                                 return new TimeSpan(milliseconds);
171                         }
172                         
173                         /**
174                          * Creates a TimeSpan from the specified number of seconds
175                          * @param seconds The number of seconds in the timespan
176                          * @return A TimeSpan that represents the specified value
177                          */ 
178                         public static function fromSeconds(seconds : Number) : TimeSpan
179                         {
180                                 return new TimeSpan(seconds * MILLISECONDS_IN_SECOND);
181                         }
182                         
183                         /**
184                          * Creates a TimeSpan from the specified number of minutes
185                          * @param minutes The number of minutes in the timespan
186                          * @return A TimeSpan that represents the specified value
187                          */ 
188                         public static function fromMinutes(minutes : Number) : TimeSpan
189                         {
190                                 return new TimeSpan(minutes * MILLISECONDS_IN_MINUTE);
191                         }
192                         
193                         /**
194                          * Creates a TimeSpan from the specified number of hours
195                          * @param hours The number of hours in the timespan
196                          * @return A TimeSpan that represents the specified value
197                          */ 
198                         public static function fromHours(hours : Number) : TimeSpan
199                         {
200                                 return new TimeSpan(hours * MILLISECONDS_IN_HOUR);
201                         }
202                         
203                         /**
204                          * Creates a TimeSpan from the specified number of days
205                          * @param days The number of days in the timespan
206                          * @return A TimeSpan that represents the specified value
207                          */ 
208                         public static function fromDays(days : Number) : TimeSpan
209                         {
210                                 return new TimeSpan(days * MILLISECONDS_IN_DAY);
211                         }
212                         
213                         /**
214                          * The number of milliseconds in one day
215                          */ 
216                         public static const MILLISECONDS_IN_DAY : Number = 86400000;
217                         
218                         /**
219                          * The number of milliseconds in one hour
220                          */ 
221                         public static const MILLISECONDS_IN_HOUR : Number = 3600000;
222                         
223                         /**
224                          * The number of milliseconds in one minute
225                          */ 
226                         public static const MILLISECONDS_IN_MINUTE : Number = 60000;
227                         
228                         /**
229                          * The number of milliseconds in one second
230                          */ 
231                         public static const MILLISECONDS_IN_SECOND : Number = 1000;
232                 }
233 }