~rosco2/ubuntu/wily/gramps/bug-1492304

« back to all changes in this revision

Viewing changes to src/calendars/Persian.py

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2004-06-16 16:53:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040616165336-kjzczqef4gnxrn2b
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Gramps - a GTK+/GNOME based genealogy program
 
3
#
 
4
# Copyright (C) 2001  Donald N. Allingham
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
"""
 
21
Gregorian calendar module for GRAMPS. 
 
22
 
 
23
The original algorithms for this module came from Scott E. Lee's
 
24
C implementation. The original C source can be found at Scott's
 
25
web site at http://www.scottlee.com
 
26
"""
 
27
 
 
28
__author__ = "Donald N. Allingham"
 
29
__version__ = "$Revision: 1.3 $"
 
30
 
 
31
#-------------------------------------------------------------------------
 
32
#
 
33
# python modules
 
34
#
 
35
#-------------------------------------------------------------------------
 
36
import math
 
37
 
 
38
#-------------------------------------------------------------------------
 
39
#
 
40
# Gramps Modules
 
41
#
 
42
#-------------------------------------------------------------------------
 
43
import Calendar
 
44
from gettext import gettext as _
 
45
 
 
46
#-------------------------------------------------------------------------
 
47
#
 
48
# Persian
 
49
#
 
50
#-------------------------------------------------------------------------
 
51
class Persian(Calendar.Calendar):
 
52
    """Persian Calendar"""
 
53
 
 
54
    EPOCH = 1948320.5
 
55
    SDN_475_1_1 = 2121446
 
56
 
 
57
    MONTHS = [ "Farvardin", "Ordibehesht", "Khordad", "Tir", "Mordad",
 
58
               "Shahrivar", "Mehr", "Aban", "Azar", "Dey", "Bahman", "Esfand" ]
 
59
 
 
60
    M2NUM = {
 
61
        "farvardin" : 1,   "ordibehesht" : 2,     "khordad" : 3,
 
62
        "tir" : 4,         "mordad" : 5,          "shahrivar" : 6,
 
63
        "mehr" : 7,        "aban" : 8,            "azar" : 9,
 
64
        "dey" : 10,        "bahman" : 11,         "esfand" : 12
 
65
        }
 
66
 
 
67
    NAME = "Persian"
 
68
    TNAME = _("Persian")
 
69
 
 
70
    def quote_display(self,year,month,day,mode):
 
71
        return "%s (%s)" % (self.display(year,month,day,mode),Persian.NAME)
 
72
 
 
73
    def set_month_string(self,text):
 
74
        try:
 
75
            return Persian.M2NUM[unicode(text.lower())]
 
76
        except KeyError:
 
77
            return Calendar.UNDEF
 
78
 
 
79
    def month(self,val):
 
80
        try:
 
81
            return Persian.MONTHS[val-1]
 
82
        except:
 
83
            return "Illegal Month"
 
84
 
 
85
    def get_sdn(self,year, month, day):
 
86
        if year >= 0:
 
87
            epbase = year - 474
 
88
        else:
 
89
            epbase = year - 473
 
90
        
 
91
        epyear = 474 + epbase % 2820
 
92
 
 
93
        if month <= 7:
 
94
            v1 = (month - 1) * 31
 
95
        else:
 
96
            v1 = ((month - 1) * 30) + 6
 
97
        v2 = math.floor(((epyear * 682) - 110) / 2816)
 
98
        v3 = (epyear - 1) * 365 + day
 
99
        v4 = math.floor(epbase / 2820) * 1029983
 
100
        
 
101
        return int(math.ceil(v1 + v2 + v3 + v4 + Persian.EPOCH - 1))
 
102
 
 
103
    def get_ymd(self,sdn):
 
104
        sdn = math.floor(sdn) + 0.5
 
105
        
 
106
        depoch = sdn - self.get_sdn(475,1,1)
 
107
        cycle = math.floor(depoch / 1029983)
 
108
        cyear = depoch % 1029983
 
109
        if cyear == 1029982:
 
110
            ycycle = 2820
 
111
        else:
 
112
            aux1 = math.floor(cyear / 366)
 
113
            aux2 = cyear % 366
 
114
            ycycle = math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1;
 
115
            
 
116
        year = ycycle + (2820 * cycle) + 474
 
117
        if year <= 0:
 
118
            year = year - 1;
 
119
 
 
120
        yday = sdn - self.get_sdn(year, 1, 1) + 1
 
121
        if yday < 186:
 
122
            month = math.ceil(yday / 31)
 
123
        else:
 
124
            month = math.ceil((yday - 6) / 30)
 
125
        day = (sdn - self.get_sdn(year, month, 1)) + 1
 
126
        return (int(year), int(month), int(day))
 
127
 
 
128
Calendar.register(Persian)