Welcome, guest | Sign In | My Account | Store | Cart

Hey guys, Below code generate shamsi code for iran datetime and is useful for iranian developer. I hope suitable for all.

Python, 127 lines
  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
# Generate by Hassan Sajedi
import datetime

class IranDateTime:
    def todayShamsi_DT(self):
        dt = datetime.datetime.now().strftime("%m/%d/%Y %H:%M:%S")
        s = self.Shamsi(int(dt[6:10]), int(dt[0:2]), int(dt[3:5]))
        return (s + '  ' + dt[11:19].__str__())

    def todayShamsi(self):
        dt = datetime.datetime.now().strftime("%m/%d/%Y")
        return self.Shamsi(int(dt[6:10]), int(dt[0:2]), int(dt[3:5]))

    def Shamsi(self, Y , M , D):
        if (Y == 0):
            Y = 2000;
        if (Y < 100):
            Y = Y + 1900;
        if (Y == 2000):
            if (M > 2):
                curentDateandTime = datetime.datetime.now().strftime("%m/%d/%Y")
                year = curentDateandTime.substring(0, 4)
                month = curentDateandTime.substring(4, 6)
                day = curentDateandTime.substring(6, 8)
                Y = year
                M = month
                D = day
        if (M < 3 or (M == 3 and D < 21)):
            Y = Y - 622
        else:
            Y = (int(Y) - 621).__str__()
        if(M == 1):
            if (D < 21):
                M = 10
                D = D + 10
            else:
                M = 11
                D = D - 20
        elif(M == 2):
            if (D < 20):
                M = 11
                D = D + 11
            else:
                M = 12
                D = D - 19
        elif(M == 3):
            if (D < 21):
                M = 12;
                D = D + 9;
            else:
                M = 1;
                D = D - 20;
        elif (M == 4):
            if (D < 21):
                M = 1;
                D = D + 11;
            else:
                M = 2;
                D = D - 20;
        elif (M == 5):
            if (D < 22):
                M = M - 3;
                D = D + 10;
            else:
                M = M - 2;
                D = D - 21;
        elif (M == 6):
            if (D < 22):
                M = M - 3;
                D = D + 10;
            else:
                M = M - 2;
                D = D - 21;
        elif (M == 7):
            if (D < 23):
                M = M - 3;
                D = D + 9;
            else:
                M = M - 2;
                D = D - 22;
        elif (M == 8):
            if (D < 23):
                M = M - 3;
                D = D + 9;
            else:
                M = M - 2;
                D = D - 22;
        elif (M == 9):
            if (D < 23):
                M = M - 3;
                D = D + 9;
            else:
                M = M - 2;
                D = D - 22;
        elif (M == 10):
            if (D < 23):
                M = 7;
                D = D + 8;
            else:
                M = 8;
                D = D - 22;
        elif (M == 11):
            if (D < 22):
                M = M - 3;
                D = D + 9;
            else:
                M = M - 2;
                D = D - 21;
        elif (M == 12):
            if (D < 22):
                M = M - 3;
                D = D + 9;
            else:
                M = M - 2;
                D = D - 21;
        month = "00";
        day = "00";
        D = int(D);
        if (M < 10):
            month = "0" + str(M);
        else:
            month = str(M);
        if (D < 10):
            day = "0" + str(D);
        else:
            day = str(D);
        return str(Y) + "/" + month + "/" + day;
Created by Hassan Sajedi on Thu, 19 Jun 2014 (MIT)
Python recipes (4591)
Hassan Sajedi's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks