Login

Complete Persian DateTime Widget

Author:
serj1975
Posted:
November 25, 2011
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

These are the whole scripts that worked for me as a django (1.3) persian datetime widget.

  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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# ------------------------ widgets.py
from django import forms
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
from settings import STATIC_URL
import datetime,time
from utils import date_convert

calbtn = u'''<img src="{STATIC_URL}jscalendar/images/cal.png" alt="calendar" id="%s_btn" 
style="cursor: pointer; width: 18px; height:18px; vertical-align:middle; float: none;" title="Select date" />
<script type="text/javascript">
    function onJalaliDateSelected(calendar, date) {
        var e = document.getElementById("%s");
        var str = calendar.date.getFullYear() + "-" + (calendar.date.getMonth() + 1) + "-" + calendar.date.getDate();
        e.value = str;
    }
    Calendar.setup({
        inputField     :    "%s_display",   
        button         :    "%s_btn",
        ifFormat       :    "%s",
        dateType       :    "jalali",
        weekNumbers    :     false,
        onUpdate       :     onJalaliDateSelected
    });
</script>'''
calbtn = calbtn.replace("{STATIC_URL}", STATIC_URL)

class DateTimeWidget(forms.widgets.TextInput):
    class Media:
        css = {
            'all': (STATIC_URL + 'jscalendar/styles/skins/calendar-system.css',)
        }
        js = (
              STATIC_URL + 'jscalendar/js/jalali.js',
              STATIC_URL + 'jscalendar/js/calendar.js',
              STATIC_URL + 'jscalendar/js/calendar-setup.js',
              STATIC_URL + 'jscalendar/js/lang/calendar-fa.js',
        )

    dformat = '%Y-%m-%d'
    def render(self, name, value, attrs=None):
        if value is None: value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            try:
                final_attrs['value'] = \
                                   force_unicode(value.strftime(self.dformat))
            except:
                final_attrs['value'] = \
                                   force_unicode(value)
        if not final_attrs.has_key('id'):
            final_attrs['id'] = u'%s_id' % (name)
        id = final_attrs['id']

        jsdformat = self.dformat #.replace('%', '%%')
        cal = calbtn % (id, id, id, id, jsdformat)
        parsed_atts = forms.util.flatatt(final_attrs)
        dat_f = date_convert.gregorian_to_jalali(value,'-')
        a = u'<span><input type="text" id="%s_display" value="%s"/><input type="hidden" %s/> %s%s</span>' % (id,dat_f, parsed_atts, self.media, cal)        
        return mark_safe(a)

    def value_from_datadict(self, data, files, name):
        dtf = forms.fields.DEFAULT_DATETIME_INPUT_FORMATS
        empty_values = forms.fields.EMPTY_VALUES

        value = data.get(name, None)
        if value in empty_values:
            return None
        if isinstance(value, datetime.datetime):
            return value
        if isinstance(value, datetime.date):
            return datetime.datetime(value.year, value.month, value.day)
        for format in dtf:
            try:
                # print "%s   %s    %s" %(value,format,time.strptime(value, format)[:6])
                dst = time.strptime(value, format)[:6]  # date string tuple
                return datetime.datetime(dst[0],dst[1],dst[2])
            except ValueError:
                continue
            # except Exception,e:
                # return e.message 
        return None

    def _has_changed(self, initial, data):
        '''
        Return True if data differs from initial.
        Copy of parent's method, but modify value with strftime function before final comparsion
        '''
        if data is None:
            data_value = u''
        else:
            data_value = data

        if initial is None:
            initial_value = u''
        else:
            initial_value = initial

        try:
            if force_unicode(initial_value.strftime(self.dformat)) != force_unicode(data_value.strftime(self.dformat)):
                return True
        except:
            if force_unicode(initial_value) != force_unicode(data_value):
                return True
        return False

#------------------------------------------------------------------------
#--------------- date_convert.py
from datetime import datetime, date, time
from calverter import calverter
def jalali_to_gregorian(dat_str):
    '''
    Gets date in (char(8)) (or / delimited) (or char(10) / delimited)
    returns Date
    returns None on error
    '''
    cal = calverter()	
    try:
        year = None
        month = None
        day = None        
        if len(dat_str) == 8:
            year = dat_str[0:4]            
            month = dat_str[4:6]            
            day = dat_str[6:8]
        elif len(dat_str) == 10:
            year = dat_str[0:4]
            month = dat_str[5:7]
            day = dat_str[8:10]
        else:
            splited = dat_str.split('/')
            year = splited[0]
            month = splited[1]
            day = splited[2]            
        if not year.isdigit(): return None
        if not month.isdigit(): return None
        if not day.isdigit(): return None
        jd = cal.jalali_to_jd(int(year),int(month),int(day))
        dat_tuple = cal.jd_to_gregorian(jd)
        return date(dat_tuple[0],dat_tuple[1],dat_tuple[2])
    except Exception,msg:
        # print msg
        return None
        
def gregorian_to_jalali(date,sep='/'):        
    '''
    Gets georgian date 
    returns persian date in char(10) (/ separated) 
    '''
    if date == '' or date == None: return ''
    cal = calverter()
    date_str = str(date)
    year = date_str[0:4]
    month = date_str[5:7]
    day = date_str[8:10]
    jd = cal.gregorian_to_jd(int(year),int(month),int(day))
    dat_tuple = cal.jd_to_jalali(jd)
    format = "%s"+sep+"%s"+sep+"%s"
    return format %(str(dat_tuple[0]).rjust(4,'0'),str(dat_tuple[1]).rjust(2,'0'),str(dat_tuple[2]).rjust(2,'0'))

    
def jalali_today():
    date = datetime.now().date()
    return gregorgian_to_jalali(date)

# ----------------------------------------------------------------------
#!/usr/bin/env python
##   calverter.py  (2008/08/16)
##
##   Copyright (C) 2008 Mehdi Bayazee ([email protected])
##
##   Iranian (Jalali) calendar: 
##           http://en.wikipedia.org/wiki/Iranian_calendar
##   Islamic (Hijri) calendar:
##           http://en.wikipedia.org/wiki/Islamic_calendar
##   Gregorian calendar:
##           http://en.wikipedia.org/wiki/Gregorian_calendar
##
##   This program is free software; you can redistribute it and/or modify
##   it under the terms of the GNU General Public License as published by
##   the Free Software Foundation; either version 2, or (at your option)
##   any later version.
##
##   This program 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 General Public License for more details.


__author__ = "Mehdi Bayazee"
__copyright__ = "Copyright (C) 2008 Mehdi Bayazee"

__revision__ = "$Id$"
__version__ = "0.1.5"

import math


class calverter:
          
     def __init__(self):
          self.J0000 = 1721424.5                # Julian date of Gregorian epoch: 0000-01-01
          self.J1970 = 2440587.5                # Julian date at Unix epoch: 1970-01-01
          self.JMJD  = 2400000.5                # Epoch of Modified Julian Date system
          self.J1900 = 2415020.5                # Epoch (day 1) of Excel 1900 date system (PC)
          self.J1904 = 2416480.5                # Epoch (day 0) of Excel 1904 date system (Mac)
          
          self.NormLeap = ("Normal year", "Leap year")
          
          self.GREGORIAN_EPOCH = 1721425.5
          self.GREGORIAN_WEEKDAYS = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
          
          self.ISLAMIC_EPOCH = 1948439.5;
          self.ISLAMIC_WEEKDAYS = ("al-ahad", "al-'ithnayn", "ath-thalatha'", "al-arbia`aa'", "al-khamis", "al-jumu`a", "as-sabt")
          
          self.JALALI_EPOCH = 1948320.5;
          self.JALALI_WEEKDAYS = ("Yekshanbeh", "Doshanbeh", "Seshhanbeh", "Chaharshanbeh", "Panjshanbeh", "Jomeh", "Shanbeh")
     
     
     def jwday(self, j):
          "JWDAY: Calculate day of week from Julian day"
          
          return int(math.floor((j + 1.5))) % 7     
     
     def weekday_before(self, weekday, jd):
          """
          WEEKDAY_BEFORE: Return Julian date of given weekday (0 = Sunday)
          in the seven days ending on jd.
          """
          
          return jd - self.jwday(jd - weekday)
     
     
     def search_weekday(self, weekday, jd, direction, offset):
          """
          SEARCH_WEEKDAY: Determine the Julian date for: 
     
          weekday      Day of week desired, 0 = Sunday
          jd           Julian date to begin search
          direction    1 = next weekday, -1 = last weekday
          offset       Offset from jd to begin search
          """
                 
          return self.weekday_before(weekday, jd + (direction * offset))
     
     #  Utility weekday functions, just wrappers for search_weekday
     
     def nearest_weekday(self, weekday, jd):
          return self.search_weekday(weekday, jd, 1, 3)
     
     def next_weekday(self, weekday, jd):
          return self.search_weekday(weekday, jd, 1, 7)
     
     def next_or_current_weekday(self, weekday, jd):
          return self.search_weekday(weekday, jd, 1, 6)
     
     def previous_weekday(self, weekday, jd):
          return self.search_weekday(weekday, jd, -1, 1)
     
     def previous_or_current_weekday(self, weekday, jd):
          return self.search_weekday(weekday, jd, 1, 0)
         
     def leap_gregorian(self, year):
          "LEAP_GREGORIAN: Is a given year in the Gregorian calendar a leap year ?"
          
          return ((year % 4) == 0) and (not(((year % 100) == 0) and ((year % 400) != 0)))
     
     def gregorian_to_jd(self, year, month, day):
          "GREGORIAN_TO_JD: Determine Julian day number from Gregorian calendar date"
         
          # Python <= 2.5
          if month <= 2 :
               tm = 0
          elif self.leap_gregorian(year):
               tm = -1
          else:
               tm = -2
             
          # Python 2.5
          #tm = 0 if month <= 2 else (-1 if self.leap_gregorian(year) else -2)
             
          return (self.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) + tm + day)
     
     
     def jd_to_gregorian(self, jd) :
          "JD_TO_GREGORIAN: Calculate Gregorian calendar date from Julian day"
     
          wjd = math.floor(jd - 0.5) + 0.5
          depoch = wjd - self.GREGORIAN_EPOCH
          quadricent = math.floor(depoch / 146097)
          dqc = depoch % 146097
          cent = math.floor(dqc / 36524)
          dcent = dqc % 36524
          quad = math.floor(dcent / 1461)
          dquad = dcent % 1461
          yindex = math.floor(dquad / 365)
          year = int((quadricent * 400) + (cent * 100) + (quad * 4) + yindex)
          if not((cent == 4) or (yindex == 4)) :
               year += 1
      
          yearday = wjd - self.gregorian_to_jd(year, 1, 1)
          
          # Python <= 2.5
          if wjd < self.gregorian_to_jd(year, 3, 1):
               leapadj = 0
          elif self.leap_gregorian(year):
               leapadj = 1
          else:
               leapadj = 2
              
          # Python 2.5
          #leapadj = 0 if wjd < self.gregorian_to_jd(year, 3, 1) else (1 if self.leap_gregorian(year) else 2)
         
          month = int(math.floor((((yearday + leapadj) * 12) + 373) / 367))
          day = int(wjd - self.gregorian_to_jd(year, month, 1)) + 1
      
          return year, month, day
     
     def n_weeks(self, weekday, jd, nthweek):
         
          j = 7 * nthweek
          if nthweek > 0 :
               j += self.previous_weekday(weekday, jd)
          else :
               j += next_weekday(weekday, jd)
          return j
     
     
     def iso_to_julian(self, year, week, day):
          "ISO_TO_JULIAN: Return Julian day of given ISO year, week, and day"
          
          return day + self.n_weeks(0, self.gregorian_to_jd(year - 1, 12, 28), week)
     
     
     def jd_to_iso(self, jd):
          "JD_TO_ISO: Return array of ISO (year, week, day) for Julian day"
          
          year = self.jd_to_gregorian(jd - 3)[0]
          if jd >= self.iso_to_julian(year + 1, 1, 1) :
               year += 1
         
          week = int(math.floor((jd - self.iso_to_julian(year, 1, 1)) / 7) + 1)
          day = self.jwday(jd)
          if day == 0 :
               day = 7
         
          return year, week, day
     
     def iso_day_to_julian(self, year, day):
          "ISO_DAY_TO_JULIAN: Return Julian day of given ISO year, and day of year"
          
          return (day - 1) + self.gregorian_to_jd(year, 1, 1)
     
     def jd_to_iso_day(self, jd):
          "JD_TO_ISO_DAY: Return array of ISO (year, day_of_year) for Julian day"
          
          year = self.jd_to_gregorian(jd)[0]
          day = int(math.floor(jd - self.gregorian_to_jd(year, 1, 1))) + 1
          return year, day
     
     def pad(self, Str, howlong, padwith) :
          "PAD: Pad a string to a given length with a given fill character. "
          
          s = str(Str)
     
          while s.length < howlong :
               s = padwith + s
          return s
     
     
     def leap_islamic(self, year):
          "LEAP_ISLAMIC: Is a given year a leap year in the Islamic calendar ?"
          
          return (((year * 11) + 14) % 30) < 11
     
     
     def islamic_to_jd(self, year, month, day):
          "ISLAMIC_TO_JD: Determine Julian day from Islamic date"
          
          return (day + math.ceil(29.5 * (month - 1)) + \
                  (year - 1) * 354 + \
                  math.floor((3 + (11 * year)) / 30) + \
                  self.ISLAMIC_EPOCH) - 1
     
     def jd_to_islamic(self, jd):
          "JD_TO_ISLAMIC: Calculate Islamic date from Julian day"
         
          jd = math.floor(jd) + 0.5
          year = int(math.floor(((30 * (jd - self.ISLAMIC_EPOCH)) + 10646) / 10631))
          month = int(min(12, math.ceil((jd - (29 + self.islamic_to_jd(year, 1, 1))) / 29.5) + 1))
          day = int(jd - self.islamic_to_jd(year, month, 1)) + 1;
          return year, month, day
     
     def leap_jalali(self, year):
          "LEAP_jalali: Is a given year a leap year in the Jalali calendar ?"
     
          # Python <= 2.5
          if year > 0:
               rm = 474
          else:
               rm = 473
     
          # Python 2.5
          #return ((((((year - 474 if year > 0 else 473 ) % 2820) + 474) + 38) * 682) % 2816) < 682
     
          return ((((((year - rm) % 2820) + 474) + 38) * 682) % 2816) < 682
     
     def jalali_to_jd(self, year, month, day):
          "JALALI_TO_JD: Determine Julian day from Jalali date"
          
          # Python <= 2.5
          if year >=0 :
               rm = 474
          else:
               rm = 473
          epbase = year - (rm)
         
          # Python 2.5
          #epbase = year - 474 if year>=0 else 473
          epyear = 474 + (epbase % 2820)
         
            
          if month <= 7 :
               mm = (month - 1) * 31
          else:
               mm = ((month - 1) * 30) + 6
     
          return day + mm + \
                 math.floor(((epyear * 682) - 110) / 2816) + \
                 (epyear - 1) * 365 + \
                 math.floor(epbase / 2820) * 1029983 + \
                 (self.JALALI_EPOCH - 1)
     
     def jd_to_jalali(self, jd):
          "JD_TO_JALALI: Calculate Jalali date from Julian day"
     
          jd = math.floor(jd) + 0.5
          depoch = jd - self.jalali_to_jd(475, 1, 1)
          cycle = math.floor(depoch / 1029983)
          cyear = depoch % 1029983
          if cyear == 1029982 :
               ycycle = 2820
          else :
               aux1 = math.floor(cyear / 366)
               aux2 = cyear % 366
               ycycle = math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1
         
          year = int(ycycle + (2820 * cycle) + 474)
          if year <= 0 :
               year -= 1
         
          yday = (jd - self.jalali_to_jd(year, 1, 1)) + 1
          if yday <= 186:
               month = int(math.ceil(yday / 31))
          else:
               month = int(math.ceil((yday - 6) / 30))
             
          day = int(jd - self.jalali_to_jd(year, month, 1)) + 1
          return year, month, day
     
     
# ----------------------------------------------------------------
# ----------------------- settings.py

STATIC_URL = '/static/'

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 5 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.