首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hijri (伊斯兰)日历问题!

hijri (伊斯兰)日历问题!
EN

Stack Overflow用户
提问于 2010-12-04 23:45:47
回答 3查看 4.5K关注 0票数 2

我会将公历日期转换为回历(伊斯兰)日期。可以在网上搜索后,我找到了一个源代码来转换它。我将代码从Java和PHP转换为C base。

这个工具有时工作起来没有任何问题。但有时也会有问题。

我需要你的帮助,要么修复工具或一个可用代码,将没有任何问题的工作!顺便说一句,我找到了另一个源代码(基于C++的http://emr.cs.iit.edu/~reingold/calendar.C)。因为我不知道C++是否有人可以将它转换成C基础或Objective C将是完美的(仍然不确定这段代码是否能正常工作)。

附注:您可以在以下地址查看正确的日期:illamicfinder.org/Hcal/index.php

代码语言:javascript
复制
void gregorian_to_hijri(int* h_y, int* h_m, int* h_d, int  g_y, int  g_m, int  g_d)
{
    int year, month, day;

    int zyr;
    int zd;
    int zm;
    int zy;

    float zjd;
    int zl;
    int zn;
    int zj;

    year = g_y;
    month = g_m;
    day = g_d;


    zyr = year;
    zd = day;
    zm = month;
    zy = zyr;

    if((zy > 1582) || ((zy == 1582) && (zm > 10)) || ((zy == 1582) && (zm == 10) && (zd > 14)))
    {
        zjd = ((1461 * (zy + 4800 + ((zm - 14) / 12))) / 4)
            + ((367 * (zm - 2 - 12 * (((zm - 14) / 12)))) / 12)
            - ((3 * (((zy + 4900 + ((zm - 14) / 12)) / 100))) / 4) + zd - 32075;
    }
    else
    {
        zjd = 367 * zy - ((7 * (zy + 5001 + ((zm - 9) / 7))) / 4)
            + ((275 * zm) / 9) + zd + 1729777;
    }

    zl = zjd - 1948440 + 10632;
    zn = ((zl - 1) / 10631);
    zl = zl - 10631 * zn + 354;
    zj = (((10985 - zl) / 5316)) * ((int)((50 * zl) / 17719))
        + ((zl / 5670)) * ((int)((43 * zl) / 15238));

    zl = zl - (((30 - zj) / 15)) * (((17719 * zj) / 50))
        - ((zj / 16)) * (((15238 * zj) / 43)) + 29;

    zm = ((24 * zl) / 709);
    zd = zl - ((709 * zm) / 24);
    zy = 30 * zn + zj - 30;

    *h_y = zy;
    *h_m = zm;
    *h_d = zd;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-12-05 00:55:53

假设这是针对Mac (Cocoa)或iOS (Cocoa Touch)应用程序的,因为这是你最常看到的Objective C,那么你可以这样做:

代码语言:javascript
复制
// Create a Gregorian Calendar
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Set up components of a Gregorian date
NSDateComponents *gregorianComponents = [[NSDateComponents alloc] init];

gregorianComponents.day = 4;
gregorianComponents.month = 12;
gregorianComponents.year = 2010;

// Create the date
NSDate *date = [gregorianCalendar dateFromComponents:gregorianComponents];

[gregorianComponents release];
[gregorianCalendar release];


// Then create an Islamic calendar
NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCivilCalendar];

// And grab those date components for the same date
NSDateComponents *hijriComponents = [hijriCalendar components:(NSDayCalendarUnit | 
                                                               NSMonthCalendarUnit |
                                                               NSYearCalendarUnit)
                                                     fromDate:date];


NSLog(@"[In Hijri calendar ->] Day: %ld, Month: %ld, Year:%ld", 
          [hijriComponents day],
          [hijriComponents month],
          [hijriComponents year]);

[hijriCalendar release];

如果您想要的只是当前日期,那么您可以完全跳过设置公历日期,只需执行以下操作:

代码语言:javascript
复制
// Create an Islamic calendar
NSCalendar *hijriCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];

// And grab the date components for the current date
NSDateComponents *hijriComponents = [hijriCalendar components:(NSDayCalendarUnit | 
                                                               NSMonthCalendarUnit |
                                                               NSYearCalendarUnit)
                                                     fromDate:[NSDate date]];

[hijriCalendar release];
票数 10
EN

Stack Overflow用户

发布于 2010-12-04 23:47:20

看看这个主题:how to convert hijari date into gregorian date in java script?

这个问题提到了JavaScript,但最上面的答案似乎有各种语言的实现链接。

票数 1
EN

Stack Overflow用户

发布于 2010-12-05 00:00:44

你应该能够使用NSCalendar在Objective-C中做到这一点(如果这确实是一种选择)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4354337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档