Converting to and from Hindu calendar
Asked Answered
A

5

50

How can I convert unix time to Hindu calendar­Wikipedia time and the other way round in php, Perl or Python or Java? I know I can convert to Hebrew and Jewish. But Hindu is not an option.

To be more specific, I'm talking about the Hindu lunar calendar. The following website is working and does exactly what I want: http://web.meson.org/calendars/. For example, it 'translates' 28-1-2012 (Gregorian) to 5-11-2068 (Hind. Lun.). How can I accomplish the same task? And if there are absolutely no scrips out there, how can I write it myself?

Anaplastic answered 27/12, 2011 at 14:53 Comment(5)
Please give example values how you would express a date (timestamp) when you write it Hindu calendar -style.Millerite
From day,month,year to day,month,year is enough for our application.Anaplastic
That's not what I wanted to know. Just give multiple examples, hindu date on the left, the according gregorian date on the right. I just want to know what that hindu calendar is about when the date values are expressed on a website.Millerite
Whoa, Kevin. Good question, and good luck. Datetimes in UTC are hard enough, and those are standardized!Pellet
Just wondering... even for this calendar there are people ready to die or to kill for which of the mod-7 days is the sacred day of rest like for Christian, Hebrew and Muslim calendars?Chromyl
B
8

Paper: Indian Calendrical Calculations
Provides Common Lisp code in the appendix.

While a Python (or other language) solution could be written according to the paper, the authors enumerate the Indian calendar rules pretty well, so it's a pretty solid paper if you're willing to consider taking the provided Common Lisp code.

Bloomfield answered 2/3, 2012 at 10:13 Comment(2)
Great! I think with the help of this paper it should be possible to implement the calendar conversion.Entanglement
Just to note that almost all implementations - including the one in Python that I posted and the one you initially mentioned in your question use code derived from the Lisp snippets from that paper.Errolerroll
S
18

Did you check DateTime-Indic-0.1 family of modules? At least DateTime::Indic::Chandramana seems to have a method to convert traditional date into UTC values (utc_rd_values).

UPDATE:

I suppose Calendar::Saka may be useful as well for many users (as I have known, it's the Indian national calendar), in particular, to_gregorian() and from_gregorian() methods.

Shakedown answered 25/2, 2012 at 10:48 Comment(8)
Thanks, how can I install this? I installed CPAN and now tried to do perl Makefile.PL, but it gives errors like Warning: prerequisite DateTime::Event::Lunar 0.06 not found.Anaplastic
And what does cpan install DateTime::Event::Lunar tell you, sorry?Shakedown
That some other dependencies are missing. I am trying to install them now.Anaplastic
Ok, tried now perl ./Build.PL, but that said to me that DateTime was missing. So I did cpan DateTime and now it is saying: Result: FAIL Failed 43/95 test programs. 3/3 subtests failed.Anaplastic
which version/flavor of Perl do you use?Shakedown
Perl5 according to my CentOS system.Anaplastic
let us continue this discussion in chatShakedown
@Kevin: "Installed CPAN?" CPAN should already be installed. And dependencies should be installed automatically. Though I recommend cpanm.Avila
E
12

For Python, use calendar2 (note: this is not the built-in calendar module).

Sample use:

>>> from calendar2 import *
>>> old_hindu_solar_from_absolute(absolute_from_gregorian(3,1,2012))
(11, 16, 5112)
>>> old_hindu_lunar_from_absolute(absolute_from_gregorian(3,1,2012))
(12, 0, 8, 5112)
Errolerroll answered 1/3, 2012 at 7:0 Comment(0)
B
8

Paper: Indian Calendrical Calculations
Provides Common Lisp code in the appendix.

While a Python (or other language) solution could be written according to the paper, the authors enumerate the Indian calendar rules pretty well, so it's a pretty solid paper if you're willing to consider taking the provided Common Lisp code.

Bloomfield answered 2/3, 2012 at 10:13 Comment(2)
Great! I think with the help of this paper it should be possible to implement the calendar conversion.Entanglement
Just to note that almost all implementations - including the one in Python that I posted and the one you initially mentioned in your question use code derived from the Lisp snippets from that paper.Errolerroll
E
5

Seems to be a difficult task. According to this discussion at bytes.com there is no clear way to accomplish a 100% correct conversion. But it seems that they are wrong when they assume that the Hindu calendar has only 364 days instead of 365 (or 366 in leap years).

Here you can find a good conversion table including the handling of leap years: http://hinduism.about.com/od/basics/a/monthsdayseras.htm

If it is as easy as written there you can try something like this (php code):

<?php

function convertDateToHinduDate($date) {
    $beginningDayOfMonth = array(
        1 => 21,
        2 => 20,
        3 => 22 + (dateIsLeapYear($date) ? -1 : 0), /* 21 in leap years */
        4 => 21,
        5 => 22,
        6 => 22,
        7 => 23,
        8 => 23,
        9 => 23,
        10 => 23,
        11 => 22,
        12 => 22,
    );

    $daysOfHinduMonth = array(
        1 => 30 + (dateIsLeapYear($date) ? 1 : 0), /* 31 in leap years */
        2 => 31,
        3 => 31,
        4 => 31,
        5 => 31,
        6 => 31,
        7 => 30,
        8 => 30,
        9 => 30,
        10 => 30,
        11 => 30,
        12 => 30,
    );

    $day = (int) date('d', strtotime($date));
    $month = (int) date('m', strtotime($date));
    $year = (int) date('Y', strtotime($date));

    $monthBefore = $day < $beginningDayOfMonth[$month];
    $yearBefore = $month < 3 || ($month == 3 && $day < $beginningDayOfMonth[3]);

    $newYear = $year + 57 + ($yearBefore ? -1 : 0);
    $newMonth = $month - 2 + ($monthBefore ? -1 : 0);
    if($newMonth < 1) $newMonth = 12 + $newMonth;
    $newDay = $day - $beginningDayOfMonth[$month];
    if($newDay < 1) $newDay = $daysOfHinduMonth[$newMonth] + $newDay;

    return date("d-m-Y",  mktime(11, 59, 0, $newMonth, $newDay, $newYear));
}

function dateIsLeapYear($date) {
    return date('L', strtotime($date));
}

$date = date("d-m-Y", strtotime('2012-01-28'));

echo 'Date: ', $date, ' (is leap year: ', dateIsLeapYear($date) ? 'yes' : 'no', ')<br />';
echo 'Converted Hindu date: ', convertDateToHinduDate($date);
?>

Output of this code:

Date: 28-01-2012 (is leap year: yes)
Converted Hindu date: 07-11-2068

But according to the calculator of this Java applet, it should be 05-11-2068 instead of 07-11-2068. So, there are still some conversion rules missing. Maybe you can give me some more information so that i can correct the code above.

Entanglement answered 25/2, 2012 at 12:6 Comment(0)
P
1

I dont know whether it is correct approach or not but Please go to http://calendarhome.com/converter/ site and download two javascript files astro.js and calendar.js and follow onchange events of Gregorian Date and fetch Indian Civil Date parameters.

Parhe answered 3/3, 2012 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.