Domanda

Vorrei convertire data Gregoriana a Hijri data (islamica). Dopo può cercare sul web, ho trovato un codice sorgente per convertirlo. Ho convertito il codice Java e PHP alla base C.

Il implementare alcuni tempi di lavoro senza alcun problema. Ma alcuni giorni ha problema.

Ho bisogno del vostro aiuto sia risolvere l'attrezzo o di un codice disposizione che funziona senza alcun problema! BTW ho trovato un altro codice sorgente ( http://emr.cs.iit.edu/~ Reingold / calendar.C) che è C ++ base. Come io non so C ++ se qualcuno può convertire in C Base o Objective C sarebbe prefetto (ancora non è sicuro di questo codice funziona correttamente o meno).

P.S. È possibile controllare la data corretta in: islamicfinder.org/Hcal/index.php

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;
}
È stato utile?

Soluzione

Supponendo questo è per un Mac (cacao) o iOS (Cocoa Touch) app, in quanto è lì che si vede Objective C il più delle volte, allora si può solo fare qualcosa di simile a questo:

// 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];

Se invece si è la data corrente, allora si può saltare l'impostazione della data gregoriano del tutto e solo fare questo:

// 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];

Altri suggerimenti

Date un'occhiata a questo argomento: come convertire data hijari in data Gregoriana in java script?

La domanda menziona JavaScript ma la risposta in alto sembra avere collegamenti ad implementazioni in una varietà di lingue.

Si dovrebbe essere in grado di fare questo in Objective-C (se questo è davvero un'opzione) utilizzando NSCalendar.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top