summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/Clipperz/PM/Date.js
blob: a131357d0489d3b110aa7628a7faae1c28b33aef (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*

Copyright 2008-2011 Clipperz Srl

This file is part of Clipperz's Javascript Crypto Library.
Javascript Crypto Library provides web developers with an extensive
and efficient set of cryptographic functions. The library aims to
obtain maximum execution speed while preserving modularity and
reusability.
For further information about its features and functionalities please
refer to http://www.clipperz.com

* Javascript Crypto Library is free software: you can redistribute
  it and/or modify it under the terms of the GNU Affero General Public
  License as published by the Free Software Foundation, either version
  3 of the License, or (at your option) any later version.

* Javascript Crypto Library is distributed in the hope that it will
  be useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU Affero General Public License for more details.

* You should have received a copy of the GNU Affero General Public
  License along with Javascript Crypto Library.  If not, see
  <http://www.gnu.org/licenses/>.

*/

if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; }
if (typeof(Clipperz.PM.Date) == 'undefined') { Clipperz.PM.Date = {}; }

Clipperz.PM.Date.VERSION = "0.1";
Clipperz.PM.Date.NAME = "Clipperz.PM.Date";

MochiKit.Base.update(Clipperz.PM.Date, {

	'__repr__': function () {
		return "[" + this.NAME + " " + this.VERSION + "]";
	},

	//-------------------------------------------------------------------------

	'toString': function () {
		return this.__repr__();
	},

	//-------------------------------------------------------------------------

	'locale': function() {
		return {
			'amDesignation':	Clipperz.PM.Strings.getValue('calendarStrings.amDesignation'),
			'pmDesignation':	Clipperz.PM.Strings.getValue('calendarStrings.pmDesignation'),
			'days':				Clipperz.PM.Strings.getValue('calendarStrings.days'),
			'shortDays':		Clipperz.PM.Strings.getValue('calendarStrings.shortDays'),
			'shortMonths':		Clipperz.PM.Strings.getValue('calendarStrings.shortMonths'),
			'months':			Clipperz.PM.Strings.getValue('calendarStrings.months')
		}
	},
	
	//=========================================================================
/*
	'formatDateWithPHPLikeTemplate': function(aDate, aTemplate) {
		return Clipperz.Date.formatDateWithPHPLikeTemplateAndLocale(aDate, aTemplate, Clipperz.PM.Date.locale());
	},

	'parseDateWithPHPLikeTemplate': function(aDate, aTemplate) {
		return Clipperz.Date.parseDateWithPHPTemplateAndLocale(aDate, aTemplate, Clipperz.PM.Date.locale());
	},

	//=========================================================================

	'formatDateWithJavaLikeTemplate': function(aDate, aTemplate) {
		return Clipperz.Date.formatDateWithJavaLikeTemplateAndLocale(aDate, aTemplate, Clipperz.PM.Date.locale());
	},

	'parseDateWithJavaLikeTemplate': function(aDate, aTemplate) {
		return Clipperz.Date.parseDateWithJavaLikeTemplateAndLocale(aDate, aTemplate, Clipperz.PM.Date.locale());
	},
*/
	//=========================================================================

	'formatWithTemplate': function (aTemplate, aDate) {
		return Clipperz.PM.Date.formatDateWithTemplate(aDate, aTemplate);
	},

	'formatDateWithTemplate': function(aDate, aTemplate) {
		var result;

		if (aDate == null) {
			result = ""
		} else {
		 	result = Clipperz.Date.formatDateWithPHPLikeTemplateAndLocale(aDate, aTemplate, Clipperz.PM.Date.locale());
		};
		
		return result;
	},

	'parseDateWithTemplate': function(aValue, aTemplate) {
		return Clipperz.Date.parseDateWithPHPTemplateAndLocale(aValue, aTemplate, Clipperz.PM.Date.locale());
	},

	//=========================================================================

	'formatDateWithUTCFormat': function(aDate) {
		return Clipperz.Date.formatDateWithUTCFormatAndLocale(aDate,  Clipperz.PM.Date.locale());
	},
	
	'parseDateWithUTCFormat': function(aValue) {
		var result;
		
		if (aValue == null) {
			result = null;
		} else {
		 	result = Clipperz.Date.parseDateWithUTCFormatAndLocale(aValue,  Clipperz.PM.Date.locale());
		}
		
		return result;
	},

	//=========================================================================

	'getElapsedTimeDescription': function(aDate) {
		var result;

		result = ""
		
		if (aDate != null) {
			var now;
			var elapsedTime;
		
			var millisencondsInAMinute;
			var millisencondsInAnHour;
			var millisencondsInADay;
			var millisencondsInAWeek;
			var millisencondsInAMonth;

			now = new Date();
			elapsedTime = now.getTime() - aDate.getTime();
		
			millisencondsInAMinute = 60 * 1000;
			millisencondsInAnHour = millisencondsInAMinute * 60;
			millisencondsInADay = millisencondsInAnHour * 24;
			millisencondsInAWeek = millisencondsInADay * 7;
			millisencondsInAMonth = millisencondsInAWeek * 5;

			if ((elapsedTime / millisencondsInAMonth) > 1) {
				result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.MORE_THAN_A_MONTH_AGO');
			} else if ((elapsedTime / millisencondsInAWeek) > 1) {
				var elapsedWeeks;
			
				elapsedWeeks = Math.floor((elapsedTime / millisencondsInAWeek));
				if (elapsedWeeks == 1) {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.MORE_THAN_A_WEEK_AGO');
				} else {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.MORE_THAN_*_WEEKS_AGO').replace(/__elapsed__/, elapsedWeeks);
				}
			} else if ((elapsedTime / millisencondsInADay) > 1) {
				var elapsedDays;
			
				elapsedDays = Math.floor((elapsedTime / millisencondsInADay));
				if (elapsedDays == 1) {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.YESTERDAY');
				} else {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.*_DAYS_AGO').replace(/__elapsed__/, elapsedDays);
				}
			} else if ((elapsedTime / millisencondsInAnHour) > 1) {
				var elapsedHours;
			
				elapsedHours = Math.floor((elapsedTime / millisencondsInAnHour));
				if (elapsedHours == 1) {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.ABOUT_AN_HOUR_AGO');
				} else {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.*_HOURS_AGO').replace(/__elapsed__/, elapsedHours);
				}
			} else {
				var elapsed10Minutes;
			
				elapsed10Minutes = (Math.floor((elapsedTime / millisencondsInAMinute) / 10)) * 10;
				if (elapsed10Minutes == 0) {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.JUST_A_FEW_MINUTES_AGO');
				} else {
					result = Clipperz.PM.Strings.getValue('elapsedTimeDescriptions.ABOUT_*_MINUTES_AGO').replace(/__elapsed__/, elapsed10Minutes+"");
				}
			}
		}
		
		return result;
	},
	
	//-------------------------------------------------------------------------

	'parse': function (aValue) {
		return Clipperz.PM.Date.parseDateWithUTCFormat(aValue);
	},

	//-------------------------------------------------------------------------
	__syntaxFix__: "syntax fix"

});