Created
May 14, 2016 13:34
-
-
Save arman-hpp/3b1a944c76a488e6a967196bb04d12f6 to your computer and use it in GitHub Desktop.
Miladi and Shamsi to Ghamari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class GhamariDate | |
{ | |
private const decimal GREGORIAN_EPOCH = 1721425.5M; | |
private const decimal ISLAMIC_EPOCH = 1948439.5M; | |
public static string MiladiToGhamari(DateTime date) | |
{ | |
var result = j_gh(date.Year, date.Month, date.Day); | |
return string.Format("{0}/{1}/{2}", result[0], result[1], result[2]); | |
} | |
public static string ShamsiToGhamari(string date) | |
{ | |
var pc = new System.Globalization.PersianCalendar(); | |
var result = date.Split('/'); | |
var dt1 = pc.ToDateTime(Convert.ToInt32(result[0]), Convert.ToInt32(result[1]), Convert.ToInt32(result[2]), 0, 0, 0, 0); | |
var zresult = j_gh(dt1.Year, dt1.Month, dt1.Day); | |
return string.Format("{0}/{1}/{2}", zresult[0], zresult[1], zresult[2]); | |
} | |
private static int[] j_gh(int t, int o, int n) | |
{ | |
var julian_update = gregorian_to_jd(t, o, n) + Math.Floor(0.5M) / 86400M; | |
return jd_to_islamic(julian_update); | |
} | |
private static decimal gregorian_to_jd(int t, int o, int n) | |
{ | |
return GREGORIAN_EPOCH - 1 + 365 * (t - 1) + Math.Floor((t - 1) / 4M) + | |
-Math.Floor((t - 1) / 100M) + Math.Floor((t - 1) / 400M) + Math.Floor( | |
(367M * o - 362M) / 12M + (2 >= o ? 0 : leap_gregorian(t) ? -1 : - | |
2) + n); | |
} | |
private static bool leap_gregorian(decimal t) | |
{ | |
return t % 4 == 0 && !(t % 100 == 0 && t % 400M != 0); | |
} | |
private static int[] jd_to_islamic(decimal t) | |
{ | |
t = Math.Floor(t) + 0.5M; | |
var o = (int)Math.Floor((30*(t - ISLAMIC_EPOCH) + 10646)/10631); | |
var n = (int)Math.Min(12, Math.Ceiling((t - (29M + islamic_to_jd(o, 1, 1)))/29.5M) + 1); | |
var e = (int)(t - islamic_to_jd(o, n, 1) + 1); | |
return new[] {o, n, e}; | |
} | |
private static decimal islamic_to_jd(decimal t, decimal o, decimal n) | |
{ | |
return n + Math.Ceiling(29.5M * (o - 1)) + 354 * (t - 1) + Math.Floor((3 + 11 * t) / 30) + ISLAMIC_EPOCH - 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment