persian_date_js 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf4bdb03368312bfe54d4bc47ef632d314bb7a71
4
+ data.tar.gz: 5b64ef71650cec6d990ec4df8cbb3edd747f3cce
5
+ SHA512:
6
+ metadata.gz: 1553f9acd0677c2e20f45f59ebe06bd8d6f8246b19c3f1e446664d6bf85e0ae7b10f9219e43baaf35886ac7917818706cd753cd5061e57d2445cfd736fec5fb0
7
+ data.tar.gz: 715b040919b916637f63bc8bce7d3a2ffaa8b2402283793c8c07f9222a5d8aa624f794767003bdb2f99f5f16323ebbdd267fdcd84b53e6548f34b47e939d7fb3
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in persian_date_js.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Pouya Gharib Pour
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # PersianDateJs
2
+
3
+ A gem for adding PersianDate(v0.1.8) JS library to your Rails project.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'persian_date_js'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Then add the following line to your application.js file:
18
+
19
+ ```javascript
20
+ //= require persian-date
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install persian_date_js
26
+
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/psparabara/persian_date_js/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,1097 @@
1
+ /* persian-date - v0.1.8 */ ( function () {String.prototype.toPersianDigit = function (a) {
2
+ return this.replace(/\d+/g, function (digit) {
3
+ var enDigitArr = [], peDigitArr = [], i, j;
4
+ for (i = 0; i < digit.length; i += 1) {
5
+ enDigitArr.push(digit.charCodeAt(i));
6
+ }
7
+ for (j = 0; j < enDigitArr.length; j += 1) {
8
+ peDigitArr.push(String.fromCharCode(enDigitArr[j] + ((!!a && a === true) ? 1584 : 1728)));
9
+ }
10
+ return peDigitArr.join('');
11
+ });
12
+ };
13
+
14
+ var toPersianDigit = function (digit) {
15
+ return digit.toString().toPersianDigit();
16
+ }, isArray = function (input) {
17
+ return Object.prototype.toString.call(input) === '[object Array]';
18
+ }, isString = function (input) {
19
+ return typeof input === "string" ? true : false;
20
+ }, isNumber = function (input) {
21
+ return typeof input === "number" ? true : false;
22
+ }, isDate = function (input) {
23
+ return input instanceof Date;
24
+ }, isUndefined = function (input) {
25
+ if (typeof input === "undefined")
26
+ return true;
27
+ else
28
+ return false;
29
+ }, leftZeroFill = function (number, targetLength) {
30
+ var output = number + '';
31
+ while (output.length < targetLength) {
32
+ output = '0' + output;
33
+ }
34
+ return output;
35
+ }, absRound = function (number) {
36
+ if (number < 0) {
37
+ return Math.ceil(number);
38
+ } else {
39
+ return Math.floor(number);
40
+ }
41
+ }, mod = function (a, b) {
42
+ return a - (b * Math.floor(a / b));
43
+ };
44
+ var monthRange = {
45
+ 1: {
46
+ name: {
47
+ fa: "فروردین"
48
+ },
49
+ abbr: {
50
+ fa: "فرو"
51
+ }
52
+ },
53
+ 2: {
54
+ name: {
55
+ fa: "اردیبهشت"
56
+ },
57
+ abbr: {
58
+ fa: "ارد"
59
+ }
60
+ },
61
+ 3: {
62
+ name: {
63
+ fa: "خرداد"
64
+ },
65
+ abbr: {
66
+ fa: "خرد"
67
+ }
68
+ },
69
+ 4: {
70
+ name: {
71
+ fa: "تیر"
72
+ },
73
+ abbr: {
74
+ fa: "تیر"
75
+ }
76
+ },
77
+ 5: {
78
+ name: {
79
+ fa: "مرداد"
80
+ },
81
+ abbr: {
82
+ fa: "مرد"
83
+ }
84
+ },
85
+ 6: {
86
+ name: {
87
+ fa: "شهریور"
88
+ },
89
+ abbr: {
90
+ fa: "شهر"
91
+ }
92
+ },
93
+ 7: {
94
+ name: {
95
+ fa: "مهر"
96
+ },
97
+ abbr: {
98
+ fa: "مهر"
99
+ }
100
+ },
101
+ 8: {
102
+ name: {
103
+ fa: "آبان"
104
+ },
105
+ abbr: {
106
+ fa: "آبا"
107
+ }
108
+
109
+ },
110
+ 9: {
111
+ name: {
112
+ fa: "آذر"
113
+ },
114
+ abbr: {
115
+ fa: "آذر"
116
+ }
117
+ },
118
+ 10: {
119
+ name: {
120
+ fa: "دی"
121
+ },
122
+ abbr: {
123
+ fa: "دی"
124
+ }
125
+ },
126
+ 11: {
127
+ name: {
128
+ fa: "بهمن"
129
+ },
130
+ abbr: {
131
+ fa: "بهم"
132
+ }
133
+ },
134
+ 12: {
135
+ name: {
136
+ fa: "اسفند"
137
+ },
138
+ abbr: {
139
+ fa: "اسف"
140
+ }
141
+ }
142
+ }, weekRange = {
143
+ 1: {
144
+ name: {
145
+ fa: "شنبه"
146
+ },
147
+ abbr: {
148
+ fa: "ش"
149
+ }
150
+ },
151
+ 2: {
152
+ name: {
153
+ fa: "یکشنبه"
154
+ },
155
+ abbr: {
156
+ fa: "ی"
157
+ }
158
+ },
159
+ 3: {
160
+ name: {
161
+ fa: "دوشنبه"
162
+ },
163
+ abbr: {
164
+ fa: "د"
165
+ }
166
+ },
167
+ 4: {
168
+ name: {
169
+ fa: "سه شنبه"
170
+ },
171
+ abbr: {
172
+ fa: "س"
173
+ }
174
+ },
175
+ 5: {
176
+ name: {
177
+ fa: "چهار شنبه"
178
+ },
179
+ abbr: {
180
+ fa: "چ"
181
+ }
182
+ },
183
+ 6: {
184
+ name: {
185
+ fa: "پنج شنبه"
186
+ },
187
+ abbr: {
188
+ fa: "پ"
189
+ }
190
+ },
191
+ 0: {
192
+ name: {
193
+ fa: "جمعه"
194
+ },
195
+ abbr: {
196
+ fa: "ج"
197
+ }
198
+ }
199
+ },
200
+ persianDaysName = ["اورمزد", "بهمن", "اوردیبهشت", "شهریور", "سپندارمذ", "خورداد", "امرداد", "دی به آذز", "آذز", "آبان", "خورشید", "ماه", "تیر", "گوش", "دی به مهر", "مهر", "سروش", "رشن", "فروردین", "بهرام", "رام", "باد", "دی به دین", "دین", "ارد", "اشتاد", "آسمان", "زامیاد", "مانتره سپند", "انارام", "زیادی"];
201
+ GREGORIAN_EPOCH = 1721425.5,
202
+ PERSIAN_EPOCH = 1948320.5,
203
+ jwday = function (j) {
204
+ return mod(Math.floor((j + 1.5)), 7);
205
+ },
206
+ // LEAP_GREGORIAN -- Is a given year in the Gregorian calendar a leap year ?
207
+ isLeapGregorian = function (year) {
208
+ return ((year % 4) == 0) && (!(((year % 100) === 0) && ((year % 400) != 0)));
209
+ },
210
+ isLeapPersian = function (year) {
211
+ return ((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682;
212
+ },
213
+ // GREGORIAN_TO_JD -- Determine Julian day number from Gregorian calendar date
214
+ gregorianToJd = function (year, month, day) {
215
+ return (GREGORIAN_EPOCH - 1) + (365 * (year - 1)) + Math.floor((year - 1) / 4) + (-Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400) + Math.floor((((367 * month) - 362) / 12) + ((month <= 2) ? 0 : (isLeapGregorian(year) ? -1 : -2)
216
+ ) + day);
217
+ },
218
+ // JD_TO_GREGORIAN -- Calculate Gregorian calendar date from Julian day
219
+ jdToGregorian = function (jd) {
220
+ var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, dyindex, year, yearday, leapadj;
221
+ wjd = Math.floor(jd - 0.5) + 0.5;
222
+ depoch = wjd - GREGORIAN_EPOCH;
223
+ quadricent = Math.floor(depoch / 146097);
224
+ dqc = mod(depoch, 146097);
225
+ cent = Math.floor(dqc / 36524);
226
+ dcent = mod(dqc, 36524);
227
+ quad = Math.floor(dcent / 1461);
228
+ dquad = mod(dcent, 1461);
229
+ yindex = Math.floor(dquad / 365);
230
+ year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
231
+ if (!((cent == 4) || (yindex == 4))) {
232
+ year++;
233
+ }
234
+ yearday = wjd - gregorianToJd(year, 1, 1);
235
+ leapadj = ((wjd < gregorianToJd(year, 3, 1)) ? 0 : (isLeapGregorian(year) ? 1 : 2)
236
+ );
237
+ month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
238
+ day = (wjd - gregorianToJd(year, month, 1)) + 1;
239
+ return new Array(year, month, day);
240
+ },
241
+ // PERSIAN_TO_JD -- Determine Julian day from Persian date
242
+ persianToJd = function (year, month, day) {
243
+ mod = function (a, b) {
244
+ return a - (b * Math.floor(a / b));
245
+ };
246
+
247
+ var epbase, epyear;
248
+ epbase = year - ((year >= 0) ? 474 : 473);
249
+ epyear = 474 + mod(epbase, 2820);
250
+ return day + ((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6)
251
+ ) + Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 + Math.floor(epbase / 2820) * 1029983 + (PERSIAN_EPOCH - 1);
252
+ },
253
+ // JD_TO_PERSIAN -- Calculate Persian date from Julian day
254
+ jdToPersian = function (jd) {
255
+ var year, month, day, depoch, cycle, cyear, ycycle, aux1, aux2, yday;
256
+ jd = Math.floor(jd) + 0.5;
257
+ depoch = jd - persianToJd(475, 1, 1);
258
+ cycle = Math.floor(depoch / 1029983);
259
+ cyear = mod(depoch, 1029983);
260
+ if (cyear === 1029982) {
261
+ ycycle = 2820;
262
+ } else {
263
+ aux1 = Math.floor(cyear / 366);
264
+ aux2 = mod(cyear, 366);
265
+ ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1;
266
+ }
267
+ year = ycycle + (2820 * cycle) + 474;
268
+ if (year <= 0) {
269
+ year -= 1;
270
+ }
271
+ yday = (jd - persianToJd(year, 1, 1)) + 1;
272
+ month = (yday <= 186) ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30);
273
+ day = (jd - persianToJd(year, month, 1)) + 1;
274
+ return new Array(year, month, day);
275
+ },
276
+ calcPersian = function (year, month, day) {
277
+ var date, j;
278
+ var j = persianToJd(year, month, day);
279
+ var date = jdToGregorian(j);
280
+ return new Array(date[0], date[1] - 1, date[2]);
281
+ },
282
+ // calcGregorian -- Perform calculation starting with a Gregorian date
283
+ calcGregorian = function (year, month, day) {
284
+ // Update Julian day
285
+ var j = gregorianToJd(year, month + 1, day) + (Math.floor(0 + 60 * (0 + 60 * 0) + 0.5) / 86400.0),
286
+ // Update Persian Calendar
287
+ perscal = jdToPersian(j), weekday = jwday(j);
288
+ return new Array(perscal[0], perscal[1], perscal[2], weekday);
289
+ },
290
+ //Converts a gregorian date to Jalali date for different formats
291
+ toPersianDate = function (gd) {
292
+ var pa = calcGregorian(gd.getFullYear(), gd.getMonth(), gd.getDate());
293
+ var output = {};
294
+ output.monthDayNumber = pa[2] - 1;
295
+ if (pa[3] == 6) {
296
+ output.weekDayNumber = 1;
297
+ } else if (pa[3] == 5) {
298
+ output.weekDayNumber = 0;
299
+ } else if (pa[3] == 4) {
300
+ output.weekDayNumber = 6;
301
+ } else if (pa[3] == 3) {
302
+ output.weekDayNumber = 5;
303
+ } else if (pa[3] == 2) {
304
+ output.weekDayNumber = 4;
305
+ } else if (pa[3] == 1) {
306
+ output.weekDayNumber = 3;
307
+ } else if (pa[3] == 0) {
308
+ output.weekDayNumber = 2;
309
+ }
310
+ output.year = pa[0];
311
+ output.month = pa[1];
312
+ output.day = output.weekDayNumber;
313
+ output.date = pa[2];
314
+ output.hours = gd.getHours();
315
+ output.minutes = ((gd.getMinutes() < 10) ? ('0' + gd.getMinutes()) : (gd.getMinutes()));
316
+ output.seconds = gd.getSeconds();
317
+ output.milliseconds = gd.getMilliseconds();
318
+ output.timeZoneOffset = gd.getTimezoneOffset();
319
+ return output;
320
+ },
321
+ persianArrayToGregorianDate = function (parray) {
322
+ // Howha : javascript Cant Parse this array truly 2011,2,20
323
+ var pd = calcPersian(parray[0] ? parray[0] : 0, parray[1] ? parray[1] : 1, parray[2] ? parray[2] : 1);
324
+ var gDate = new Date(pd[0], pd[1], pd[2]);
325
+ gDate.setYear(pd[0]);
326
+ gDate.setMonth(pd[1]);
327
+ gDate.setDate(pd[2]);
328
+ // TODO:
329
+ gDate.setHours(parray[3] ? parray[3] : 0);
330
+ gDate.setMinutes(parray[4] ? parray[4] : 0);
331
+ gDate.setSeconds(parray[5] ? parray[5] : 0);
332
+ return gDate;
333
+ },
334
+ getPersianArrayFromPDate = function (pDate) {
335
+ return [pDate.year, pDate.month, pDate.date, pDate.hours, pDate.minutes, pDate.seconds, pDate.milliseconds];
336
+ };
337
+ Duration = function (duration) {
338
+ var absRound = function (number) {
339
+ if (number < 0) {
340
+ return Math.ceil(number);
341
+ } else {
342
+ return Math.floor(number);
343
+ }
344
+ }, data = this._data = {}, years = duration.years || duration.year || duration.y || 0, months = duration.months || duration.month || duration.M || 0, weeks = duration.weeks || duration.w || duration.week || 0, days = duration.days || duration.d || duration.day || 0, hours = duration.hours || duration.hour || duration.h || 0, minutes = duration.minutes || duration.minute || duration.m || 0, seconds = duration.seconds || duration.second || duration.s || 0, milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0;
345
+ // representation for dateAddRemove
346
+ this._milliseconds = milliseconds + seconds * (1e3) + minutes * (6e4) + hours * (36e5);
347
+ // Because of dateAddRemove treats 24 hours as different from a
348
+ // day when working around DST, we need to store them separately
349
+ this._days = days + weeks * 7;
350
+ // It is impossible translate months into days without knowing
351
+ // which months you are are talking about, so we have to store
352
+ // it separately.
353
+ this._months = months + years * 12;
354
+ // The following code bubbles up values, see the tests for
355
+ // examples of what that means.
356
+ data.milliseconds = milliseconds % 1000;
357
+ seconds += absRound(milliseconds / 1000);
358
+ data.seconds = seconds % 60;
359
+ minutes += absRound(seconds / 60);
360
+ data.minutes = minutes % 60;
361
+ hours += absRound(minutes / 60);
362
+ data.hours = hours % 24;
363
+ days += absRound(hours / 24);
364
+ days += weeks * 7;
365
+ data.days = days % 30;
366
+ months += absRound(days / 30);
367
+ data.months = months % 12;
368
+ years += absRound(months / 12);
369
+ data.years = years;
370
+ return this;
371
+ };
372
+
373
+ Duration.prototype = {
374
+ weeks: function () {
375
+ return "Must Implement";
376
+ },
377
+ valueOf: function () {
378
+ return this._milliseconds + this._days * (864e5) + this._months * (2592e6);
379
+ },
380
+ humanize: function (withSuffix) {
381
+ return "Must Implement";
382
+ }
383
+ };
384
+ var PersianDate = function (input) {
385
+ if (!(this instanceof PersianDate))
386
+ return new PersianDate(input)
387
+ // Convert Any thing to Gregorian Date
388
+ if (isUndefined(input)) {
389
+ this.gDate = new Date();
390
+ } else if (isDate(input)) {
391
+ this.gDate = input;
392
+ } else if (isArray(input)) {
393
+ // Encapsulate Input Array
394
+ var arrayInput = input.slice();
395
+ this.gDate = persianArrayToGregorianDate(arrayInput);
396
+ } else if (isNumber(input)) {
397
+ this.gDate = new Date(input);
398
+ } else if (input instanceof PersianDate) {
399
+ this.gDate = input.gDate;
400
+ }
401
+ // ASP.NET JSON Date
402
+ else if (input.substring(0, 6) === "/Date(") {
403
+ this.gDate = new Date(parseInt(input.substr(6)));
404
+ }
405
+ this.pDate = toPersianDate(this.gDate);
406
+ return this;
407
+ };
408
+
409
+ PersianDate.prototype = {
410
+ duration: function (input, key) {
411
+ var isDuration = this.isDuration(input), isNumber = ( typeof input === 'number'), duration = ( isDuration ? input._data : ( isNumber ? {} : input)), ret;
412
+ if (isNumber) {
413
+ if (key) {
414
+ duration[key] = input;
415
+ } else {
416
+ duration.milliseconds = input;
417
+ }
418
+ }
419
+ return new Duration(duration);
420
+ },
421
+ isDuration: function (obj) {
422
+ return obj instanceof Duration;
423
+ },
424
+ humanize: function () {
425
+ return "Must Implement";
426
+ },
427
+ add: function (key, input) {
428
+ var d = this.duration(input, key).valueOf(), newUnixDate = this.gDate.valueOf() + d;
429
+ return new PersianDate(newUnixDate);
430
+ },
431
+ subtract: function (key, input) {
432
+ var d = this.duration(input, key).valueOf(), newUnixDate = this.gDate.valueOf() - d;
433
+ return new PersianDate(newUnixDate);
434
+ },
435
+ formatPersian: "_default",
436
+ formatNumber: function () {
437
+ var output;
438
+ // if default conf dosent set follow golbal config
439
+ if (this.formatPersian === "_default") {
440
+ if (window.formatPersian === false) {
441
+ output = false;
442
+ } else {
443
+ // Default Conf
444
+ output = true;
445
+ }
446
+ } else {
447
+ if (this.formatPersian === true) {
448
+ output = true;
449
+ } else if (this.formatPersian === false) {
450
+ output = false;
451
+ } else {
452
+ $.error("Invalid Config 'formatPersian' !!")
453
+ }
454
+ }
455
+ return output;
456
+ },
457
+ format: function (inputString) {
458
+ var self = this, formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DD?D?D?|ddddd|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|X|LT|ll?l?l?|LL?L?L?)/g, info = {
459
+ year: self.year(),
460
+ month: self.month(),
461
+ hour: self.hours(),
462
+ minute: self.minutes(),
463
+ second: self.seconds(),
464
+ date: self.date(),
465
+ timezone: self.zone(),
466
+ unix: self.unix()
467
+ };
468
+
469
+ function replaceFunction(input) {
470
+ formatToPersian = self.formatNumber();
471
+ switch (input) {
472
+ // AM/PM
473
+ case("a"):
474
+ {
475
+ if (formatToPersian)
476
+ return ((info.hour >= 12) ? 'ب ظ' : 'ق ظ');
477
+ else
478
+ return ((info.hour >= 12) ? 'PM' : 'AM');
479
+ }
480
+ // Hours (Int)
481
+ case("H"):
482
+ {
483
+ if (formatToPersian)
484
+ return toPersianDigit(info.hour);
485
+ else
486
+ return info.hour;
487
+ }
488
+ case("HH"):
489
+ {
490
+ if (formatToPersian)
491
+ return toPersianDigit(leftZeroFill(info.hour, 2));
492
+ else
493
+ return leftZeroFill(info.hour, 2);
494
+ }
495
+ case("h"):
496
+ {
497
+ var h = info.hour % 12;
498
+ if (formatToPersian)
499
+ return toPersianDigit(h);
500
+ else
501
+ return h;
502
+ }
503
+ case("hh"):
504
+ {
505
+ var h = info.hour % 12;
506
+ if (formatToPersian)
507
+ return toPersianDigit(leftZeroFill(h, 2));
508
+ else
509
+ return leftZeroFill(h, 2);
510
+ }
511
+ // Minutes
512
+ case("m"):
513
+ {
514
+ if (formatToPersian)
515
+ return toPersianDigit(info.minute);
516
+ else
517
+ return info.minute;
518
+ }
519
+ // Two Digit Minutes
520
+ case("mm"):
521
+ {
522
+ if (formatToPersian)
523
+ return toPersianDigit(leftZeroFill(info.minute, 2));
524
+ else
525
+ return leftZeroFill(info.minute, 2);
526
+ }
527
+ // Second
528
+ case("s"):
529
+ {
530
+ if (formatToPersian)
531
+ return toPersianDigit(info.second);
532
+ else
533
+ return info.second;
534
+ }
535
+ case("ss"):
536
+ {
537
+ if (formatToPersian)
538
+ return toPersianDigit(leftZeroFill(info.second, 2));
539
+ else
540
+ return leftZeroFill(info.second, 2);
541
+ }
542
+ // Day (Int)
543
+ case("D"):
544
+ {
545
+ if (formatToPersian)
546
+ return toPersianDigit(leftZeroFill(info.date));
547
+ else
548
+ return leftZeroFill(info.date);
549
+ }
550
+ // Return Two Digit
551
+ case("DD"):
552
+ {
553
+ if (formatToPersian)
554
+ return toPersianDigit(leftZeroFill(info.date, 2));
555
+ else
556
+ return leftZeroFill(info.date, 2);
557
+ }
558
+ // Return day Of Year
559
+ case("DDD"):
560
+ {
561
+ var t = self.startOf("year")
562
+ if (formatToPersian)
563
+ return toPersianDigit(self.diff(t, "days"));
564
+ else
565
+ return self.diff(t, "days");
566
+ }
567
+ // Return Week Day Full Name
568
+ case("DDDD"):
569
+ {
570
+ var t = self.startOf("year")
571
+ if (formatToPersian)
572
+ return leftZeroFill(self.diff(t, "days"), 3);
573
+ else
574
+ return toPersianDigit(leftZeroFill(self.diff(t, "days"), 3));
575
+ }
576
+ // Return day Of week
577
+ case("d"):
578
+ {
579
+ if (formatToPersian)
580
+ return toPersianDigit(self.pDate.weekDayNumber);
581
+ else
582
+ return self.pDate.weekDayNumber;
583
+ }
584
+ // Return week day name abbr
585
+ case("ddd"):
586
+ {
587
+ return weekRange[self.pDate.weekDayNumber].abbr.fa;
588
+ }
589
+ case("dddd"):
590
+ {
591
+ return weekRange[self.pDate.weekDayNumber].name.fa;
592
+ }
593
+ // Return Persian Day Name
594
+ case("ddddd"):
595
+ {
596
+ return persianDaysName[self.pDate.monthDayNumber]
597
+ }
598
+ // Return Persian Day Name
599
+ case("w"):
600
+ {
601
+ var t = self.startOf("year")
602
+ return parseInt(self.diff(t, "days") / 7) + 1;
603
+ }
604
+ // Return Persian Day Name
605
+ case("ww"):
606
+ {
607
+ var t = self.startOf("year")
608
+ return leftZeroFill(parseInt(self.diff(t, "days") / 7) + 1, 2);
609
+ }
610
+ // Month (Int)
611
+ case("M"):
612
+ {
613
+ if (formatToPersian)
614
+ return toPersianDigit(info.month);
615
+ else
616
+ return info.month;
617
+ }
618
+ // Two Digit Month (Str)
619
+ case("MM"):
620
+ {
621
+ if (formatToPersian)
622
+ return toPersianDigit(leftZeroFill(info.month, 2));
623
+ else
624
+ return leftZeroFill(info.month, 2);
625
+ }
626
+ // Abbr String of Month (Str)
627
+ case("MMM"):
628
+ {
629
+ return monthRange[info.month].abbr.fa;
630
+ }
631
+ // Full String name of Month (Str)
632
+ case("MMMM"):
633
+ {
634
+ return monthRange[info.month].name.fa;
635
+ }
636
+ // Year
637
+ // Two Digit Year (Str)
638
+ case("YY"):
639
+ {
640
+ var yearDigitArray = info.year.toString().split("");
641
+ if (formatToPersian)
642
+ return toPersianDigit(yearDigitArray[2] + yearDigitArray[3]);
643
+ else
644
+ return yearDigitArray[2] + yearDigitArray[3];
645
+ }
646
+ // Full Year (Int)
647
+ case("YYYY"):
648
+ {
649
+ if (formatToPersian)
650
+ return toPersianDigit(info.year);
651
+ else
652
+ return info.year;
653
+ }
654
+ case("Z"):
655
+ {
656
+ var flag = "+";
657
+ var hours = Math.round(info.timezone / 60);
658
+ var minutes = info.timezone % 60;
659
+ if (minutes < 0) {
660
+ minutes *= -1;
661
+ }
662
+ if (hours < 0) {
663
+ flag = "-";
664
+ hours *= -1;
665
+ }
666
+
667
+ var z = flag + leftZeroFill(hours, 2) + ":" + leftZeroFill(minutes, 2);
668
+ if (formatToPersian)
669
+ return toPersianDigit(z)
670
+ else
671
+ return z;
672
+ }
673
+ case("ZZ"):
674
+ {
675
+ var flag = "+";
676
+ var hours = Math.round(info.timezone / 60);
677
+ var minutes = info.timezone % 60;
678
+ if (minutes < 0) {
679
+ minutes *= -1;
680
+ }
681
+ if (hours < 0) {
682
+ flag = "-";
683
+ hours *= -1;
684
+ }
685
+
686
+ var z = flag + leftZeroFill(hours, 2) + "" + leftZeroFill(minutes, 2);
687
+ if (formatToPersian)
688
+ return toPersianDigit(z)
689
+ else
690
+ return z;
691
+ }
692
+ case("X"):
693
+ {
694
+ return self.unix();
695
+ }
696
+ // 8:30 PM
697
+ case("LT"):
698
+ {
699
+ return self.format("h:m a");
700
+ }
701
+ // 09/04/1986
702
+ case("L"):
703
+ {
704
+ return self.format("YYYY/MM/DD");
705
+ }
706
+ // 9/4/1986
707
+ case("l"):
708
+ {
709
+ return self.format("YYYY/M/D");
710
+ }
711
+ // September 4th 1986
712
+ case("LL"):
713
+ {
714
+ return self.format("MMMM DD YYYY");
715
+ }
716
+ // Sep 4 1986
717
+ case("ll"):
718
+ {
719
+ return self.format("MMM DD YYYY");
720
+ }
721
+ //September 4th 1986 8:30 PM
722
+ case("LLL"):
723
+ {
724
+ return self.format("MMMM YYYY DD h:m a");
725
+ }
726
+ // Sep 4 1986 8:30 PM
727
+ case("lll"):
728
+ {
729
+ return self.format("MMM YYYY DD h:m a");
730
+ }
731
+ //Thursday, September 4th 1986 8:30 PM
732
+ case("LLLL"):
733
+ {
734
+ return self.format("dddd D MMMM YYYY h:m a");
735
+ }
736
+ // Thu, Sep 4 1986 8:30 PM
737
+ case("llll"):
738
+ {
739
+ return self.format("ddd D MMM YYYY h:m a");
740
+ }
741
+
742
+ default:
743
+ return info._d;
744
+ }
745
+ }
746
+
747
+ if (inputString) {
748
+ return inputString.replace(formattingTokens, replaceFunction);
749
+ } else {
750
+ var inputString = "YYYY-MM-DD HH:mm:ss a"
751
+ return inputString.replace(formattingTokens, replaceFunction);
752
+ }
753
+ },
754
+ // Humanize
755
+ from: function () {
756
+ return "Must Implement";
757
+ },
758
+ fromNow: function () {
759
+ return "Must Implement";
760
+ },
761
+ humanizeDuration: function () {
762
+ return "Must Implement";
763
+ },
764
+ _d: function () {
765
+ return this.gDate._d;
766
+ },
767
+ diff: function (input, val, asFloat) {
768
+ var self = new PersianDate(this), inputMoment = input,
769
+ //this._isUTC ? moment(input).utc() : moment(input).local();
770
+ zoneDiff = 0,
771
+ //(this.zone() - inputMoment.zone()) * 6e4;
772
+ diff = self.gDate - inputMoment.gDate - zoneDiff, year = self.year() - inputMoment.year(), month = self.month() - inputMoment.month(), date = (self.date() - inputMoment.date()) * -1, output;
773
+ if (val === 'months' || val === 'month') {
774
+ output = year * 12 + month + date / 30;
775
+ } else if (val === 'years' || val === 'year') {
776
+ output = year + (month + date / 30) / 12;
777
+ } else {
778
+ output = val === 'seconds' || val === 'second' ? diff / 1e3 : // 1000
779
+ val === 'minutes' || val === 'minute' ? diff / 6e4 : // 1000 * 60
780
+ val === 'hours' || val === 'hour' ? diff / 36e5 : // 1000 * 60 * 60
781
+ val === 'days' || val === 'day' ? diff / 864e5 : // 1000 * 60 * 60 * 24
782
+ val === 'weeks' || val === 'week' ? diff / 6048e5 : // 1000 * 60 * 60 * 24 * 7
783
+ diff;
784
+ }
785
+ if (output < 0)
786
+ output * -1;
787
+ return asFloat ? output : Math.round(output);
788
+ },
789
+ startOf: function (key) {
790
+ // Simplify this
791
+ switch (key) {
792
+ case "years":
793
+ case "year" :
794
+ return new PersianDate([this.year(), 1, 1]);
795
+ case "months":
796
+ case "month":
797
+ return new PersianDate([this.year(), this.month(), 1]);
798
+ case "days" :
799
+ case "day" :
800
+ return new PersianDate([this.year(), this.month(), this.date(), 0, 0, 0]);
801
+ case "hours" :
802
+ case "hour" :
803
+ return new PersianDate([this.year(), this.month(), this.date(), this.hours(), 0, 0]);
804
+ case "minutes":
805
+ case "minute":
806
+ return new PersianDate([this.year(), this.month(), this.date(), this.hours(), this.minutes(), 0]);
807
+ case "seconds":
808
+ case "second":
809
+ return new PersianDate([this.year(), this.month(), this.date(), this.hours(), this.minutes(), this.seconds()]);
810
+ case "weeks":
811
+ case "week":
812
+ var weekDayNumber = this.pDate.weekDayNumber;
813
+ if (weekDayNumber === 0) {
814
+ return new PersianDate([this.year(), this.month(), this.date()]);
815
+ } else {
816
+ return new PersianDate([this.year(), this.month(), this.date()]).subtract("days", weekDayNumber);
817
+ }
818
+ default:
819
+ return this;
820
+ }
821
+ },
822
+ endOf: function (key) {
823
+ // Simplify this
824
+ switch (key) {
825
+ case "years":
826
+ case "year":
827
+ var days = this.isLeapYear() ? 30 : 29;
828
+ return new PersianDate([this.year(), 12, days, 23, 59, 59]);
829
+ case "months":
830
+ case "month":
831
+ var monthDays = this.daysInMonth(this.year(), this.month());
832
+ return new PersianDate([this.year(), this.month(), monthDays, 23, 59, 59]);
833
+ case "days" :
834
+ case "day" :
835
+ return new PersianDate([this.year(), this.month(), this.date(), 23, 59, 59]);
836
+ case "hours" :
837
+ case "hour" :
838
+ return new PersianDate([this.year(), this.month(), this.date(), this.hours(), 59, 59]);
839
+ case "minutes":
840
+ case "minute":
841
+ return new PersianDate([this.year(), this.month(), this.date(), this.hours(), this.minutes(), 59]);
842
+ case "seconds":
843
+ case "second":
844
+ return new PersianDate([this.year(), this.month(), this.date(), this.hours(), this.minutes(), this.seconds()]);
845
+ case "weeks":
846
+ case "week":
847
+ var weekDayNumber = this.pDate.weekDayNumber;
848
+ if (weekDayNumber === 6) {
849
+ weekDayNumber = 7;
850
+ } else {
851
+ weekDayNumber = 6 - weekDayNumber;
852
+ }
853
+ return new PersianDate([this.year(), this.month(), this.date()]).add("days", weekDayNumber);
854
+ default:
855
+ return this;
856
+ }
857
+ },
858
+ sod: function () {
859
+ return this.startOf("day");
860
+ },
861
+ eod: function () {
862
+ return this.endOf("day");
863
+ },
864
+ // Get the timezone offset in minutes.
865
+ zone: function () {
866
+ return this.pDate.timeZoneOffset;
867
+ },
868
+ _utcMode: false,
869
+ local: function () {
870
+ if (!this._utcMode) {
871
+ return this;
872
+ } else {
873
+ var offsetMils = this.pDate.timeZoneOffset * 60 * 1000;
874
+ if (this.pDate.timeZoneOffset < 0) {
875
+ var utcStamp = this.valueOf() - offsetMils;
876
+ } else {
877
+ var utcStamp = this.valueOf() + offsetMils;
878
+ }
879
+ this.gDate = new Date(utcStamp);
880
+ this._updatePDate();
881
+ this._utcMode = false;
882
+ return this;
883
+ }
884
+ },
885
+ // current date/time in UTC mode
886
+ utc: function (input) {
887
+ if (input) {
888
+ return new persianDate(input).utc();
889
+ }
890
+ if (this._utcMode) {
891
+ return this;
892
+ } else {
893
+ var offsetMils = this.pDate.timeZoneOffset * 60 * 1000;
894
+ if (this.pDate.timeZoneOffset < 0) {
895
+ var utcStamp = this.valueOf() + offsetMils;
896
+ } else {
897
+ var utcStamp = this.valueOf() - offsetMils;
898
+ }
899
+ this.gDate = new Date(utcStamp);
900
+ this._updatePDate();
901
+ this._utcMode = true;
902
+ return this;
903
+ }
904
+ },
905
+ isUtc: function () {
906
+ return this._utcMode;
907
+ },
908
+ // version 0.0.1
909
+ isDST: function () {
910
+ // Just Iran Day light saving time
911
+ var output = false;
912
+ if (this.month() >= 1 && this.month() <= 6) {
913
+ output = true;
914
+ switch (this.month()) {
915
+ case(1):
916
+ if (this.date() < 2) {
917
+ output = false
918
+ }
919
+ ;
920
+ break;
921
+ case(6):
922
+ if (this.date() > 30) {
923
+ output = false
924
+ }
925
+ ;
926
+ break;
927
+ }
928
+ }
929
+ ;
930
+ return output;
931
+ },
932
+ isLeapYear: function () {
933
+ return isLeapPersian(this.year());
934
+ },
935
+ daysInMonth: function (yearInput, monthInput) {
936
+ var year = yearInput ? yearInput : this.year();
937
+ var month = monthInput ? monthInput : this.month();
938
+ if (month < 1 || month > 12)
939
+ return 0;
940
+ if (month < 7)
941
+ return 31;
942
+ if (month < 12)
943
+ return 30;
944
+ if (isLeapPersian(year))
945
+ return 30;
946
+ return 29;
947
+ },
948
+ // return Native Javascript Date
949
+ toDate: function () {
950
+ return this.gDate;
951
+ },
952
+ // return Array Of Persian Date
953
+ toArray: function () {
954
+ return [this.year(), this.month(), this.day(), this.hour(), this.minute(), this.second(), this.millisecond()];
955
+ },
956
+ // Return Milliseconds since the Unix Epoch (1318874398806)
957
+ _valueOf: function () {
958
+ return this.gDate.valueOf();
959
+ },
960
+ // Return Unix Timestamp (1318874398)
961
+ unix: function (timestamp) {
962
+ if (timestamp) {
963
+ return new persianDate(timestamp * 1000);
964
+ } else {
965
+ var str = this.gDate.valueOf().toString();
966
+ output = str.substring(0, str.length - 3);
967
+ }
968
+ return parseInt(output);
969
+ },
970
+ isPersianDate: function (obj) {
971
+ return obj instanceof PersianDate;
972
+ },
973
+ // -------------------------------------- Getter Setter
974
+ millisecond: function (input) {
975
+ return this.milliseconds(input)
976
+ },
977
+ milliseconds: function (input) {
978
+ if (input) {
979
+ this.gDate.setMilliseconds(input);
980
+ this._updatePDate();
981
+ return this;
982
+ } else {
983
+ return this.pDate.milliseconds;
984
+ }
985
+ },
986
+ second: function (input) {
987
+ return this.seconds(input);
988
+
989
+ },
990
+ seconds: function (input) {
991
+ if (input | input === 0) {
992
+ this.gDate.setSeconds(input);
993
+ this._updatePDate();
994
+ return this;
995
+ } else {
996
+ return this.pDate.seconds;
997
+ }
998
+ },
999
+ minute: function (input) {
1000
+ return this.minutes(input);
1001
+ },
1002
+ minutes: function (input) {
1003
+ if (input || input === 0) {
1004
+ this.gDate.setMinutes(input);
1005
+ this._updatePDate();
1006
+ return this;
1007
+ } else {
1008
+ return this.pDate.minutes;
1009
+ }
1010
+ },
1011
+ hour: function (input) {
1012
+ return this.hours(input)
1013
+ },
1014
+ hours: function (input) {
1015
+ if (input | input === 0) {
1016
+ this.gDate.setHours(input);
1017
+ this._updatePDate();
1018
+ return this;
1019
+ } else {
1020
+ return this.pDate.hours;
1021
+ }
1022
+ },
1023
+ // Day of Months
1024
+ dates: function (input) {
1025
+ return this.date(input)
1026
+ },
1027
+ date: function (input) {
1028
+ if (input | input == 0) {
1029
+ var pDateArray = getPersianArrayFromPDate(this.pDate);
1030
+ pDateArray[2] = input;
1031
+ this.gDate = persianArrayToGregorianDate(pDateArray);
1032
+ this._updatePDate();
1033
+ return this;
1034
+ } else {
1035
+ return this.pDate.date;
1036
+ }
1037
+ },
1038
+ // DAy of week
1039
+ days: function () {
1040
+ return this.day();
1041
+ },
1042
+ day: function () {
1043
+ return this.pDate.day;
1044
+ },
1045
+ month: function (input) {
1046
+ if (input | input === 0) {
1047
+ var pDateArray = getPersianArrayFromPDate(this.pDate);
1048
+ pDateArray[1] = input;
1049
+ this.gDate = persianArrayToGregorianDate(pDateArray);
1050
+ this._updatePDate();
1051
+ return this;
1052
+ } else {
1053
+ return this.pDate.month;
1054
+ }
1055
+ },
1056
+ years: function (input) {
1057
+ return this.year(input);
1058
+ },
1059
+ year: function (input) {
1060
+ if (input | input === 0) {
1061
+ var pDateArray = getPersianArrayFromPDate(this.pDate);
1062
+ pDateArray[0] = input;
1063
+ this.gDate = persianArrayToGregorianDate(pDateArray);
1064
+ this._updatePDate();
1065
+ return this;
1066
+ } else {
1067
+ return this.pDate.year;
1068
+ }
1069
+ },
1070
+ getFirstWeekDayOfMonth: function (year, month) {
1071
+ var dateArray = calcPersian(year, month, 1), pdate = calcGregorian(dateArray[0], dateArray[1], dateArray[2]);
1072
+ if (pdate[3] + 2 === 8) {
1073
+ return 1;
1074
+ } else if (pdate[3] + 2 === 7) {
1075
+ return 7;
1076
+ } else {
1077
+ return pdate[3] + 2;
1078
+ }
1079
+ },
1080
+ clone: function () {
1081
+ var self = this;
1082
+ return new PersianDate(self.gDate);
1083
+ },
1084
+ version: "0.1.7",
1085
+ _updatePDate: function () {
1086
+ this.pDate = toPersianDate(this.gDate);
1087
+ },
1088
+ valueOf: function () {
1089
+ return this._valueOf();
1090
+ }
1091
+ };
1092
+
1093
+
1094
+ persianDate = PersianDate;
1095
+ pDate = PersianDate;
1096
+ persianDate.unix = persianDate.prototype.unix;
1097
+ persianDate.utc = persianDate.prototype.utc;}());
@@ -0,0 +1,6 @@
1
+ require "persian_date_js/version"
2
+ require "persian_date_js/engine"
3
+
4
+ module PersianDateJs
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,4 @@
1
+ module PersianDateJs
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module PersianDateJs
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'persian_date_js/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "persian_date_js"
8
+ spec.version = PersianDateJs::VERSION
9
+ spec.authors = ["Pouya Gharib Pour"]
10
+ spec.email = ["p.gharibpour@gmail.com"]
11
+ spec.summary = %q{JavaScript library for persian/jalali date.}
12
+ spec.description = %q{A gem for adding PersianDate JS library to your Rails project.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: persian_date_js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pouya Gharib Pour
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A gem for adding PersianDate JS library to your Rails project.
42
+ email:
43
+ - p.gharibpour@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - app/assets/javascripts/persian-date.js
54
+ - lib/persian_date_js.rb
55
+ - lib/persian_date_js/engine.rb
56
+ - lib/persian_date_js/version.rb
57
+ - persian_date_js.gemspec
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.5.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: JavaScript library for persian/jalali date.
82
+ test_files: []