1. مهمان گرامی، جهت ارسال پست، دانلود و سایر امکانات ویژه کاربران عضو، ثبت نام کنید.
    بستن اطلاعیه

تبدیل تاریخ از میلادی به شمسی و بر عکس

شروع موضوع توسط hector2141 ‏11/9/12 در انجمن Delphi

  1. کاربر ارشد

    تاریخ عضویت:
    ‏6/9/12
    ارسال ها:
    14,326
    تشکر شده:
    2,653
    امتیاز دستاورد:
    0
    حرفه:
    daneshjo
    [TABLE]
    [TR]
    [TD="class: gutter"]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
    [/TD]
    [TD="class: code"]unit UDate;

    interface

    type

    t_date = record
    y, m, d : word;
    end;

    procedure ShToM(var sh, m : t_date);
    procedure MToSh(var m, sh : t_date);

    implementation

    uses sysUtils;

    type
    tt_date = record
    y, m, d : longint;
    end;

    Const
    MKMONTH = 2;
    SHKMONTH = 12;

    SHRYEAR = 1358;
    SHRMONTH = 10;
    SHRDAY = 11;

    MRYEAR = 1980;
    MRMONTH = 1;
    MRDAY = 1;

    _SHRYEAR = 1358;
    _SHRMONTH = 1;
    _SHRDAY = 1;

    _MRYEAR = 1979;
    _MRMONTH = 3;
    _MRDAY = 21;

    sh_month : array [1..12] of integer =
    (31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
    sh_k_month : array [1..12] of integer =
    (31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30);
    m_month : array [1..12] of integer =
    (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    m_k_month : array [1..12] of integer =
    (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    MaxKYears = 63;
    sh_k_years : array [1.. MaxKYears] of integer =
    (1197, 1201, 1205, 1210, 1214, 1218, 1222, 1226,
    1230, 1234, 1238, 1243, 1247, 1251, 1255, 1259,
    1263, 1267, 1271, 1276, 1280, 1284, 1288, 1292,
    1296, 1300, 1304, 1309, 1313, 1317, 1321, 1325,
    1329, 1333, 1337, 1342, 1346, 1350, 1354, 1358,
    1362, 1366, 1370, 1375, 1379, 1383, 1387, 1391,
    1395, 1399, 1403, 1408, 1412, 1416, 1420, 1424,
    1428, 1432, 1436, 1441, 1445, 1449, 1453);

    function sh_is_k(y : integer) : boolean;
    var
    i : integer;
    begin
    if y < 100 then y := y + 1300;
    sh_is_k := false;
    for i := 1 to MaxKYears do
    if sh_k_years = y then begin
    sh_is_k := true;
    exit;
    end;
    end;

    function m_elapsed(m : t_date) : longint;
    var
    mt, rt : TDateTime;
    el : real;
    begin
    rt := EncodeDate(MRYEAR, MRMONTH, MRDAY);
    mt := EncodeDate(m.y, m.m, m.d);
    el := mt - rt;
    m_elapsed := trunc(el);
    end;

    function sh_elapsed(sh : t_date) : longint;
    var
    el, i : longint;
    sh_k : boolean;
    begin
    el := 0;
    sh_k := sh_is_k(sh.y);

    // sh_elapsed := 0;

    if sh.y < 100 then sh.y := sh.y + 1300;
    if sh.y < _SHRYEAR then begin
    el := (longint(sh.y+1) - _SHRYEAR) * 365;
    for i := _SHRYEAR downto sh.y+1 do
    if sh_is_k(i) then dec(el);
    for i := 12 downto sh.m+1 do
    if sh_k then
    el := el - sh_k_month
    else
    el := el - sh_month;
    if sh_k then el := el - (sh_k_month[sh.m] - sh.d)
    else el := el - (sh_month[sh.m] - sh.d);
    end else begin
    if sh.y > _SHRYEAR then
    el := el + (sh.y - _SHRYEAR) * 365;
    for i := _SHRYEAR to sh.y-1 do
    if sh_is_k(i) then inc(el);
    for i := 1 to sh.m-1 do
    if sh_k then
    el := el + sh_k_month
    else
    el := el + sh_month;
    el := el + sh.d - _SHRDAY;
    end;
    sh_elapsed := el;
    end;

    procedure sh_add(var sht : t_date; el : longint);
    var
    mt, yt : longint;
    sh : tt_date;
    begin
    if el = 0 then exit;
    sh.y := sht.y;
    sh.m := sht.m;
    sh.d := sht.d;
    if el < 0 then begin
    while abs(el) >= 366 do begin
    if (sh_is_k(sh.y) and (sh.m >= SHKMONTH)) or
    (sh_is_k(sh.y-1) and (sh.m < SHKMONTH))
    then
    el := el + 366
    else
    el := el + 365;
    dec(sh.y);
    end;
    yt := sh.y;
    mt := sh.m - 1;
    if mt < 1 then begin
    mt := 12;
    dec(yt);
    end;
    while (sh_is_k(yt) and (abs(el) > sh_k_month[mt])) or
    (not sh_is_k(yt) and (abs(el) > sh_month[mt])) do
    begin
    if(sh_is_k(yt)) then
    el := el + sh_k_month[mt]
    else
    el := el + sh_month[mt];
    dec(sh.m);
    while sh.m < 1 do begin
    sh.m := sh.m + 12;
    dec(sh.y);
    end;
    yt := sh.y;
    mt := sh.m - 1;
    if mt < 1 then begin
    mt := 12;
    dec(yt);
    end;
    end;
    sh.d := sh.d + el;
    while (sh.d <= 0) do
    begin
    yt := sh.y;
    mt := sh.m - 1;
    if mt < 1 then begin
    mt := 12;
    dec(yt);
    end;
    if(sh_is_k(yt)) then
    sh.d := sh.d + sh_k_month[mt]
    else
    sh.d := sh.d + sh_month[mt];
    dec(sh.m);
    while sh.m < 1 do begin
    sh.m := sh.m + 12;
    dec(sh.y);
    end;
    end;
    end else begin
    while el >= 366 do begin
    if (sh_is_k(sh.y) and (sh.m <= SHKMONTH)) or
    (sh_is_k(sh.y+1) and (sh.m > SHKMONTH))
    then
    el := el - 366
    else
    el := el - 365;
    inc(sh.y);
    end;
    while (sh_is_k(sh.y) and (el > sh_k_month[sh.m])) or
    (not sh_is_k(sh.y) and (el > sh_month[sh.m])) do
    begin
    if(sh_is_k(sh.y)) then
    el := el - sh_k_month[sh.m]
    else
    el := el - sh_month[sh.m];
    inc(sh.m);
    while sh.m > 12 do begin
    sh.m := sh.m - 12;
    inc(sh.y);
    end;
    end;
    sh.d := sh.d + el;
    while (sh_is_k(sh.y) and (sh.d > sh_k_month[sh.m])) or
    (not sh_is_k(sh.y) and (sh.d > sh_month[sh.m])) do
    begin
    if(sh_is_k(sh.y)) then
    sh.d := sh.d - sh_k_month[sh.m]
    else
    sh.d := sh.d - sh_month[sh.m];
    inc(sh.m);
    while sh.m > 12 do begin
    sh.m := sh.m - 12;
    inc(sh.y);
    end;
    end;
    end;
    sht.y := sh.y;
    sht.m := sh.m;
    sht.d := sh.d;
    end;

    procedure m_add(var m : t_date; el : longint);
    var
    mt : TDateTime;
    begin
    mt := EncodeDate(m.y, m.m, m.d);
    mt := mt + el;
    DecodeDate(mt, m.y, m.m, m.d);
    end;

    procedure MToSh(var m, sh : t_date);
    var
    el : longint;
    begin
    { 1358/10/11 = 1980/1/1 }
    el := m_elapsed(m);
    sh.y := SHRYEAR;
    sh.m := SHRMONTH;
    sh.d := SHRDAY;
    sh_add(sh, el);
    end;

    procedure ShToM(var sh, m : t_date);
    var
    el : longint;
    begin
    { 1358/1/1 = 1979/3/21 }
    el := sh_elapsed(sh);
    m.y := _MRYEAR;
    m.m := _MRMONTH;
    m.d := _MRDAY;
    m_add(m, el);
    end;
    end.

    [/TD]
    [/TR]
    [/TABLE]