func CreateHijriDate(date time.Time, leapPattern LeapYearsPattern) (HijriDate, error) {
	// Convert date to UTC and strip times from the date
	date = date.UTC()
	date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)

	// Calculate Julian Days
	julianDays, err := juliandays.FromTime(date)
	if err != nil {
		return HijriDate{}, err
	}

	// Get days since 1 Muharram 1
	islamicDays := int64(julianDays - 1948438.5)
	if islamicDays < 0 {
		return HijriDate{}, errors.New("date is before hijri calendar started")
	}

	// Check how many 30 years cycles to reach this day
	nCycles := islamicDays / 10631

	// Calculate leftover years outside 30 years cycle
	leftoverDays := islamicDays % 10631
	leftoverYears := leftoverDays / 354

	// Calculate the leftover days after years subtracted
	leftoverDays = leftoverDays % 354

	// Adjust leftover days based on leap years that happened within leftover years
	for year := int64(1); year <= leftoverYears; year++ {
		if isLeapYear(year, leapPattern) {
			leftoverDays--
		}
	}

	// Calculate final hijri year
	hijriYear := nCycles*30 + leftoverYears
	if leftoverDays > 0 {
		hijriYear++
	} else {
		leftoverDays += 354
		if isLeapYear(hijriYear, leapPattern) {
			leftoverDays++
		}
	}

	// Calculate final hijri month and day
	var hijriDay, hijriMonth int64
	inLeapYear := isLeapYear(hijriYear, leapPattern)

	for month := int64(1); month <= 12; month++ {
		hijriMonth = month
		daysInMonth := int64(29 + month%2)
		if inLeapYear && month == 12 {
			daysInMonth = 30
		}

		leftoverDays -= daysInMonth
		if leftoverDays <= 0 {
			hijriDay = leftoverDays + daysInMonth
			break
		}
	}

	return HijriDate{
		Day:     hijriDay,
		Month:   hijriMonth,
		Year:    hijriYear,
		Pattern: leapPattern,
	}, nil
}
// CreateHijriDate converts normal Gregorian date to Hijri date. Since Hijri calendar is not proleptic
// any date before 16 July 622 CE (1 Muharram 1 H) will make this method throws error.
CreateHijriDate converts normal Gregorian date to Hijri date. Since Hijri calendar is not proleptic
any date before 16 July 622 CE (1 Muharram 1 H) will make this method throws error.
// CreateHijriDate converts normal Gregorian date to Hijri date . Since Hijri calendar is not proleptic // any date before 16 July 622 CE ( 1 Muharram 1 H ) will make this method throws error .
func CreateHijriDate(date time.Time, leapPattern LeapYearsPattern) (HijriDate, error) {
	date = date.UTC()
	date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)
	julianDays, err := juliandays.FromTime(date)
	if err != nil {
		return HijriDate{}, err
	}
	islamicDays := int64(julianDays - 1948438.5)
	if islamicDays < 0 {
		return HijriDate{}, errors.New("date is before hijri calendar started")
	}
	nCycles := islamicDays / 10631
	leftoverDays := islamicDays % 10631
	leftoverYears := leftoverDays / 354
	leftoverDays = leftoverDays % 354
	for year := int64(1); year <= leftoverYears; year++ {
		if isLeapYear(year, leapPattern) {
			leftoverDays--
		}
	}
	hijriYear := nCycles*30 + leftoverYears
	if leftoverDays > 0 {
		hijriYear++
	} else {
		leftoverDays += 354
		if isLeapYear(hijriYear, leapPattern) {
			leftoverDays++
		}
	}
	var hijriDay, hijriMonth int64
	inLeapYear := isLeapYear(hijriYear, leapPattern)
	for month := int64(1); month <= 12; month++ {
		hijriMonth = month
		daysInMonth := int64(29 + month%2)
		if inLeapYear && month == 12 {
			daysInMonth = 30
		}
		leftoverDays -= daysInMonth
		if leftoverDays <= 0 {
			hijriDay = leftoverDays + daysInMonth
			break
		}
	}
	return HijriDate{
		Day:     hijriDay,
		Month:   hijriMonth,
		Year:    hijriYear,
		Pattern: leapPattern,
	}, nil
}
func CreateHijriDate ( date time . Time , leapPattern LeapYearsPattern ) ( HijriDate , error ) { date = date . UTC ( ) date = time . Date ( date . Year ( ) , date . Month ( ) , date . Day ( ) , 0 , 0 , 0 , 0 , time . UTC ) julianDays , err := juliandays . FromTime ( date ) if err != nil { return HijriDate { } , err } islamicDays := int64 ( julianDays - 1948438.5 ) if islamicDays < 0 { return HijriDate { } , errors . New ( " " ) } nCycles := islamicDays / 10631 leftoverDays := islamicDays % 10631 leftoverYears := leftoverDays / 354 leftoverDays = leftoverDays % 354 for year := int64 ( 1 ) ; year <= leftoverYears ; year ++ { if isLeapYear ( year , leapPattern ) { leftoverDays -- } } hijriYear := nCycles * 30 + leftoverYears if leftoverDays > 0 { hijriYear ++ } else { leftoverDays += 354 if isLeapYear ( hijriYear , leapPattern ) { leftoverDays ++ } } var hijriDay , hijriMonth int64 inLeapYear := isLeapYear ( hijriYear , leapPattern ) for month := int64 ( 1 ) ; month <= 12 ; month ++ { hijriMonth = month daysInMonth := int64 ( 29 + month % 2 ) if inLeapYear && month == 12 { daysInMonth = 30 } leftoverDays -= daysInMonth if leftoverDays <= 0 { hijriDay = leftoverDays + daysInMonth break } } return HijriDate { Day : hijriDay , Month : hijriMonth , Year : hijriYear , Pattern : leapPattern , } , nil }
CreateHijriDate converts normal Gregorian date to Hijri date.
CreateHijriDate converts normal Gregorian date to Hijri date .
// Convert date to UTC and strip times from the date // Calculate Julian Days // Get days since 1 Muharram 1 // Check how many 30 years cycles to reach this day // Calculate leftover years outside 30 years cycle // Calculate the leftover days after years subtracted // Adjust leftover days based on leap years that happened within leftover years // Calculate final hijri year // Calculate final hijri month and day