[Tutorial] Julian Day [Similar to timestamps]
#1

Julian Day
I was recently given a java project from school. it was a bank type software it had many types of account. In one type i needed to calculate interest according to days from last login, tried for hours to take out difference between days with help of dates but couldn't take out so after googling I came across Julian Day which solved my problem. So i thought that I should share this thing with you guys.

Introduction


Julian day is the continuous count of days since the beginning of the Julian Period[November 24, 4714 BC] used primarily by astronomers.
The Julian Day Number (JDN) is the integer assigned to a whole solar day in the Julian day count starting from noon Greenwich Mean Time, with........

Read more here

Functions for it


So here are the functions in PAWN to get Julian Day value from a date and vice-versa.
pawn Code:
stock DateToJulian(year, month, day)
{
       new a = (14 - month) / 12;
       new y = year + 4800 - a;
       new m = month + 12 * a - 3;
       new jdn = day + (153 * m + 2)/5 + 365*y + y/4 - y/100 + y/400 - 32045;
       return jdn;
}
pawn Code:
stock JulianToDate(jdn, &year, &month, &day)
{
    jdn -= 1721119;
    year = (400 * jdn) / 146097;
    while (365*year + year/4 - year/100 + year/400 < jdn)
    year++;
    year--;
    jdn -= 365*year + year/4 - year/100 + year/400;
    month = (5*jdn + 457) / 153;
    day = jdn - (153*month - 457) / 5;
    if (month > 12)
    month -= 12, year++;
    if (year <= 0)
    year--;
}

Eamples


So these examples are using above functions
pawn Code:
main()
{
  new julian = DateToJulian(2014, 12, 29);
  printf("2014/12/19 Julian Day: %i", julian);
  new d,m,y;
  JulianToDate(julian, y, m, d);
  printf("Its date converted again : %i/%i/%i",y,m,d);
}
Quote:
Output:
2014/12/29 Julian Day: 2457021
Its date converted again : 2014/12/29

To get difference between two dates in days:
pawn Code:
new diffrence = DateToJulian(y1, m1, d1) - DateToJulian(y2, m2, d2);

Possible uses

-Temporary ban/vip system based on days only.
Can't think of anything else but I am pretty sure you guys could
Reply
#2

Nice, you can demonstrate a temporary system with that?
Reply
#3

Very good system .. i will add it in my script
Reply
#4

This is good idea,can i help me to use this?

new year, month, day, days;
days= day+time; // time is for usage days for bans example 30 days of ban
DateToJulian(god, mje, dans);
PBanInfo[id][pBanDay] = days;
PBanInfo[id][pBanMonth] = month;
PBanInfo[id][pBanYear] = year;
Reply
#5

Couldn't you just use an a mod b setup where a is the number of milliseconds (3600s = 1 hour) in UNIX time (time()) and b is 86400 (or the sec equivalent of 24 hours).

Example:
1420348413 [01/04/2015 @ 5:13am (UTC)] from UNIX time is the time a ban is set for 6 days (and the number of days the ban is long is set to banDays). If this variable is set to banTime...

You could do this-

if(banTime % 3600 > banDays) unbanPlayer(playerid);
else Kick(playerid);

The mod of the ban time in UNIX time and 3600 would yield the number of hours since the ban was set.

The only purpose Julian time serves is in large picture things. UNIX time is basically the same as Julian time, just starts on January 1st, 1970 instead of in 4714 BC...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)