DateLib -- a way to predict future moon phases

heeheehee

Developer
Staff member
In response to Bale's comment here (albeit a year late), I created this library of moon/date-related functions.

Basically, some things to note:
  • A variable with the date record can be created with dateform(int).
    • Most functions have been overloaded, so that this is not strictly necessary.
    • Dates supplied for dateform() must be formatted like today_to_string() (that is, YYYYMMDD)
  • datediff() finds the number of days between two dates.
  • moon_phase(date) returns the moon phase on a given date. The 0-parameter form of this function already exists.
  • Similarly, moon_light(date) returns the total moon brightness on a given date. The 0-parameter form of this function already exists.
  • grimacite_darkness(date) is a new one, though. It returns the darkness, as used by Grimacite gear.

Comments, questions, suggestions? (Or horrifying bugs?)
5/31 (circa 2 p.m. Pacific time): Bugfix.
7/30: Huh, just realized that DateLib didn't even need the absolute value function. Uploaded "fixed" version (although this is just the removal of one line of code)
 

Attachments

  • datelib.ash
    7.3 KB · Views: 257
Last edited:

fronobulax

Developer
Staff member
Interesting. Thank you. To clarify, every time you use "date" above as a parameter you mean a Real World date (as a string in YYYYMMDD format where all character are digits) rather than a date from the KoL calendar. Correct?

I considered implementing datediff as a "native" function using the underlying Java libraries as a means to avoid coding up all the cruft associated with leap years and variable length months but realized I had only one application for it and wasn't sure anyone else did.
 

heeheehee

Developer
Staff member
Yep, real world date. The only applications I can really see for these functions are modifiers of depleted grimacite and similar items on a given day.
 

fronobulax

Developer
Staff member
I am a DC completest and use data from Jicken Wings. My application would have been to refresh the local cache from Jicken Wings if the data was older than a threshold, regardless of other factors. I may get around to grabbing your library and doing that.
 

Bale

Minion
Holy moley! heeheehee has really impressed me. This really makes me wish that I had a use for it!
 

icon315

Member
Ha i actually have a use for this awesome

EDIT: Is there a way to check the Moon light given from the MiniMoon?
 
Last edited:

heeheehee

Developer
Staff member
PHP:
hamburglarLight(ronaldPhase,grimacePhase,mm)

Where...
PHP:
   int mm = (datediff(dateform(20060503),target)* 2 % 11 + 11 ) % 11;
   int grimacePhase = moon_phase(target) / 2;
   int ronaldPhase = moon_phase(target) % 8;
 

icon315

Member
So would hamburglarLight(ronaldPhase,grimacePhase,mm) Return 1 if the moon is lit?

EDIT: I get the error Unknown variable 'target' (relay_Daily_Info.ash, line 14)
 
Last edited:

heeheehee

Developer
Staff member
Erm, target is your target date, using the date record (so you could dateform() an integer in the form of YYYYMMDD). Let's say you want to use today as a simple test. You'd use dateform(today_to_string().to_int()). (I could overload dateform() to take a string, as well, but I'm not sure if that's worth the effort -- it'd only really apply if you're using today_to_string(), which is only really applicable for getting minimoon info. It is a simple fix, though. Just add, after the declaration of dateform(),
PHP:
date dateform(string a) {return dateform(a.to_int());}
)

It returns 1 if lit, 0 if dark, and -1 if dark and covering a lit segment.

Edit: Ack, something's wrong with the algorithm. It's returning -1 for today, when it should be 1. I'll look into it.
 
Last edited:

icon315

Member
Well the moon is lit, but the function is returning -1

PHP:
int mm = (datediff(dateform(20060503),dateform(today_to_string().to_int()))* 2 % 11 + 11 ) % 11;  
int grimacePhase = moon_phase(dateform(today_to_string().to_int())) / 2;  
int ronaldPhase = moon_phase(dateform(today_to_string().to_int())) % 8; 
int Ham = hamburglarLight(ronaldPhase,grimacePhase,mm);
write ("<center>"+ Ham) ;
 

heeheehee

Developer
Staff member
Updated script.

Yeah, I think I fixed the problem. For some odd, the offset was off by 1. (Also, I overloaded today_to_string(), as it took all of four seconds to copy/paste it into the script. today_to_string().to_int() is no longer needed.)
 

fronobulax

Developer
Staff member
Finally got around to using datelib. Thanks. However, the latest version as posted does not play nicely with the latest version of zlib. Both define int abs(int). My preference would be to have datelib adjust (and perhaps require zlib) becaus I only use datelib in one script but zlib in several.
 

heeheehee

Developer
Staff member
Fair enough, done. Simple enough bit of code (although I really would love for there to be a "boolean defined(string)" function to check if a function is defined.
 
Last edited:

Razorsoup

Member
When using datediff(), the while loop in leapsbetween() was causing me some significant lag so I came up with this for a datediff function:

Code:
int dateAsInt(string date) {
	//date as string formatted "yyyyMMdd" like today_to_string() returns
	int dateYear = format_date_time("yyyyMMdd", date, "yyyy").to_int() - 1;
	int dateDay = format_date_time("yyyyMMdd", date, "D").to_int() - 1;
	return dateYear * 365 + to_int(dateYear / 4) - to_int(dateYear / 100) + to_int(dateYear / 400) + dateDay;
}

int dateAsInt(int date) {
	return dateAsInt(date.to_string());
}

int dateDiff(string date1, string date2) {
	return dateAsInt(date2) - dateAsInt(date1);
}

int dateDiff(int date1, int date2) {
	return dateAsInt(date2) - dateAsInt(date1);
}

It converts both dates to an integer which is the number of days since Jan 1, 1AD and subtracts them from each other. Through my testing this has given me the same values as the datediff function in Excel so I think it's working correctly. Just wanted to post it here as an alternative in case anyone else has any issues in the future.
 

gausie

D̰͕̝͚̤̥̙̐̇̑͗̒e͍͔͎͈͔ͥ̉̔̅́̈l̠̪̜͓̲ͧ̍̈́͛v̻̾ͤe͗̃ͥ̐̊ͬp̔͒ͪ
Staff member
As of r20474, mafia now has support for converting from date to timestamp and visaversa, which should negate the need for the problematic while loop and help with many more things too!
 
Top