Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tshrinivasan/d0f34a0d0921f22193aa53d43285f561 to your computer and use it in GitHub Desktop.
Save tshrinivasan/d0f34a0d0921f22193aa53d43285f561 to your computer and use it in GitHub Desktop.
Code to get tamil months/date from english months
# -*- coding: utf-8 -*-
"""Tamil Month Calculator - Simpler.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1vYnNRVuCUZa-n1V6P18FUcONYflxJdcd
Created by Ravi Annasamy - ravi.annaswamy@gmail.com
Detailed discussuion is here - https://groups.google.com/g/mintamil/c/DSXP2KHvgRw
"""
import datetime as dtm
import math
month_names = 'Chiththirai,Vaigaasi,Aani,Aadi,Aavani,Purattaasi,Aippasi,Kaarthigai,Margazhi,Thai,Maasi,Panguni'.split(',')
assert len(month_names)==12
def _fract(x):
return x - int(x)
def cd_to_jd(gdate):
"""Convert Greenwich date to Julian date."""
result = 1721424.5 + gdate.toordinal()
try:
return result + (gdate.hour * 3600 + gdate.minute * 60 +
gdate.second + gdate.microsecond * 1e-6) / 86400.0
except AttributeError:
# date parameter is of type 'date' instead of 'datetime'
return result
def jd_to_cd(jd):
"""Convert Julian date to calendar date."""
date = jd - 1721424.5
result = dtm.datetime.fromordinal(int(date))
result += dtm.timedelta(seconds=86400.0 * _fract(date))
return result
def start_of_day(gdatetime):
"""Set the time part of a datetime value to zero."""
return gdatetime.replace(hour=0, minute=0, second=0, microsecond=0)
def solar_hours(t):
"""Convert siderial hours into solar (normal) hours."""
return t * 0.9972695663
def eccentric_anomaly(am, ec):
"""Return eccentric anomaly for given mean anomaly and eccentricity.
am: mean anomaly
ec: eccentricity,
"""
m = am % (2 * math.pi)
ae = m
while 1:
d = ae - (ec * math.sin(ae)) - m
if abs(d) < 0.000001:
break
ae -= d / (1.0 - (ec * math.cos(ae)))
return ae
def true_anomaly(am, ec):
"""Return true anomaly for given mean anomaly and eccentricity.
am: mean anomaly
ec: eccentricity,
"""
ae = eccentric_anomaly(am, ec)
return 2.0 * math.atan(math.sqrt((1.0 + ec) / (1.0 - ec)) * math.tan(ae * 0.5))
def get_ayan(year, month, day):
a = 16.90709 * year/1000 - 0.757371 * year/1000 * year/1000 - 6.92416100010001000
b = (month-1 + day/30) * 1.1574074/1000
return -(a+b)
def sun_long(gdate, zone):
"""Return sun longitude in degrees for given Greenwich datetime (UTC)."""
t = (cd_to_jd(gdate) - 2415020.0 + zone/24.0) / 36525.0
t2 = t * t
l = 279.69668 + 0.0003025 * t2 + 360.0 * _fract(100.0021359 * t)
m1 = 358.47583 - (0.00015 + 0.0000033 * t) * t2 + 360.0 * _fract(99.99736042 * t)
ec = 0.01675104 - 0.0000418 * t - 0.000000126 * t2
# sun anomaly
at = true_anomaly(math.radians(m1), ec)
a1 = math.radians(153.23 + 360.0 * _fract(62.55209472 * t))
b1 = math.radians(216.57 + 360.0 * _fract(125.1041894 * t))
c1 = math.radians(312.69 + 360.0 * _fract(91.56766028 * t))
d1 = math.radians(350.74 - 0.00144 * t2 + 360.0 * _fract(1236.853095 * t))
e1 = math.radians(231.19 + 20.2 * t)
h1 = math.radians(353.4 + 360.0 * _fract(183.1353208 * t))
d2 = (0.00134 * math.cos(a1) + 0.00154 * math.cos(b1) + 0.002 * math.cos(c1) +
0.00179 * math.sin(d1) + 0.00178 * math.sin(e1))
sr = (at + math.radians(l - m1 + d2)) % (2 * math.pi)
return math.degrees(sr)
def sun_long_precessed(gdate, zone):
t = (cd_to_jd(gdate) - 2415020.0 + zone/24.0) / 36525.0
sr = sun_long(gdate, zone)
ay = get_ayan(gdate.year, gdate.month, gdate.day+gdate.hour/24.0)
return sr+ay
dt = dtm.datetime(2001,3,14,18,0)
zone=-5.5
assert sun_long_precessed(dt,zone)==330.12115511267547
dt = dtm.datetime(2019, 5, 16, 18, 0)
zone=-5.5
print(dt, sun_long_precessed(dt,zone))
def print_month_firstdays(year_requested):
zone = -5.5
year = year_requested
mon,day,hour,minute = 3, 31, 18, 0
date = dtm.datetime(year, mon, day, hour, minute)
print(date)
last=12
daycount=0
search=True
for _ in range(400):
sunlong = sun_long_precessed(date, zone)
house = int(sunlong)%360.*60.
if house<0:
house+=21600
house=int(house/1800.)+1
deg=(sunlong)%30
if deg<1 and house!=last:
if search:
search=False
else:
if house==1:
print(12, daycount)
else:
print(house-1, daycount)
last=house
print(str(date.year)+'/'+str(date.month)+'/'+str(date.day),':',month_names[house-1], '1')
daycount=0
date=date+dtm.timedelta(days=1)
daycount+=1
print_month_firstdays(2019)
def print_daily_calendar(year_requested):
zone = -5.5
year = year_requested
mon,day,hour,minute = 3, 31, 18, 0
date = dtm.datetime(year, mon, day, hour, minute)
print('Search started on: ', date)
last=12
daycount=0
search=True
for _ in range(400):
sunlong = sun_long_precessed(date, zone)
house = int(sunlong)%360.*60.
if house<0:
house+=21600
house=int(house/1800.)+1
deg=(sunlong)%30
if deg<1 and house!=last:
if search:
search=False
else:
if house==1:
print(12, daycount)
else:
print(house-1, daycount)
last=house
print()
#print(str(date.year)+'/'+str(date.month)+'/'+str(date.day),':',month_names[house-1], '1')
daycount=0
if not search:
print(str(date.year)+'/'+str(date.month)+'/'+str(date.day),':',month_names[house-1], daycount+1)
date=date+dtm.timedelta(days=1)
daycount+=1
print_daily_calendar(2019)
year,mon,day,hour,minute=2000,1,1,0,0
assert sun_long(dtm.datetime(year, mon, day, hour, minute),0)==279.86755582331756
year,mon,day,hour,minute=1996,8,25,0,0
assert sun_long(dtm.datetime(year, mon, day, hour, minute),0)==152.07711722719677
def get_tamil_date(year,mon,day):
hour,minute=18,10
zone=-5.5
dt=dtm.datetime(year,mon,day,hour,minute)
sr=sun_long_precessed(dt,zone)
print(month_names[int(sr/30)])
daycount=1
while True:
if sr%30<1 and sr%30>0:
break
dt = dt - dtm.timedelta(days=1)
sr=sun_long_precessed(dt,zone)
daycount+=1
print(daycount)
year,mon,day=2019,6,15
get_tamil_date(year,mon,day)
year,mon,day=2015,7,27
get_tamil_date(year,mon,day)
year,mon,day=1950,1,26
get_tamil_date(year,mon,day)
year,mon,day=2022,6,1
get_tamil_date(year,mon,day)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment