Feature - Implemented time_to_string() with parameter

StDoodle

Minion
I would like to request that an overloaded version of time_to_string() be added that accepts a SimpleDateFormat string parameter, much like now_to_string() currently allows. This would allow creative scripters to do all sorts of time / date math, simply by passing the appropriate parameter formats to time_to_string() and working on the results.

I would absolutely love to have a robust gallery of time / date functions available in KoLmafia, but the utility probably doesn't justify the effort. However, if this request would be simple enough, it would expose enough information to make it reasonably easy to create a robust time / date library in ash through its use.

Thanks.
 

StDoodle

Minion
That handles some of the date stuff (one direction only, really), and none of the time stuff. I'm not saying it's impossible to make this work with the info we have, I'm just thinking that, if (and only if) it's not a huge deal to add support for my request, that it would make things much simpler.

I kinda figured that KoLmafia must already have something in place to do these date / time transformations since now_to_string() supports it, and was hoping that porting said functionality to time_to_string() would be trivial. If not, that's fine.
 

fronobulax

Developer
Staff member
I have not looked to see what is already available to mafia. I mentioned datelib because I wasn't sure whether you were reinventing the wheel or building a tricycle instead of a bicycle. Sounds like the latter.
 

fronobulax

Developer
Staff member
Now I'm confused.

Code:
time_to_string();
and
Code:
now_to_string("HH:MM:SS z")

return the same thing, subject to them being run within a second of each other and possible errors on my part dealing with 24 hour clocks vs. AM and PM.

Under the hood they are both returning "current time" so the only difference between them is in what gets stripped off in order to return a time in one case and a formatted datetime group in the other.

If all you want is the time portion of the datetime group then you can get it with formatting and you can choose your format.

If you are looking for something that takes an arbitrary string that is hopefully a portion of a datetime group in some recognizable format and returns the same time in a specified format with missing portions filled out with data from the current time then that would be new, but I would want to consider the complexity of the error checking before I agreed it was simple to implement.
 

StDoodle

Minion
Weird... I must have really mis-read some things; I could swear... argh, I dunno what I did. For some reason I thought time_to_string() could take an arbitrary time, but that doesn't make any sense. I dunno man.

What I meant to request was the ability to specify a time / date in a recognizable format, as well as specify a SimpleDateFormat, and have mafia do the conversion. I'd still love that, but I'm not sure that would be so easy as my messed-up mis-interpretation led me to believe.
 

fronobulax

Developer
Staff member
First, I went to the code, not the wiki so you may have seen "something".

I gather what you really want is what my last paragraph above alludes to. You want to normalize an arbitrary string which you believe represents a datetime group, into a standard format (so that you can then parse the format to make calculations). If that is the case, what are the sources of this "arbitrary" string? Is it possible/reasonable to make the source conform to convention (as it would be if a script were building the string, for example)?

My sense is that the above function can be done in about four lines of Java when the string is in a valid datetime format but checking for errors, trapping them and returning a reasonable value back to ash will be where most of the work is required.
 

StDoodle

Minion
Translating one date format into another isn't an issue, so I'm fine with any of them, though I tend to lean towards ISO. For time format, my preference would be hours:minutes:seconds, with the seconds being optional and the time interpreted in 24 hour format if no AM/PM given, 12 hour format otherwise. But again, that part is rather trivial to deal with; it's multi-stage math in multiple directions that is the real PITA. ;) I'm fine with having to use both at once, as well, including the full format being required in order to work.

tl;dr: Having to pass a full "yyyy-MM-dd HH:mm:ss" all at once would be absolutely fine.
 

fronobulax

Developer
Staff member
Example of use? I'm not quite able to make this into a request that both seems to me to add new capabilities and that is well enough defined that I can implement it. My problem, not yours, but some days I ride the short bus.
 

StDoodle

Minion
Currently, I'm working on a set of scripts for clan management. Part of the process is to track who used which dungeons and / or borrowed something from the stash and / or returned it. In order to coordinate everything between various admins, I'm converting everything into UTC date / time. Then, I have to be able to check how long it's been since several things (ie how long a debt is outstanding, when a reminder should be given in the future, etc.). I'd be using the current date / time for some things, and date / time scraped from kol (specifically clan activity for the most part) for others. I can already parse dates / times into UTC without too much trouble, so I'll omit that example. But what I'd love to be able to do is use a combination of the "day of year" format and the year (only) to easily see how many days since / until certain benchmarks have / will be met, by passing a formatted date / time to the funtion and various formatting types (one that returns "day of year" and one that returns just the year, perhaps).
 

Bale

Minion
tl;dr: StDoodle needs different admins running the script to be able to coordinate their clocks so that they know who parsed what. Local time is much less useful than game time for that purpose.

Brevity is not StDoodle's gift.
 

StDoodle

Minion
Heh, I tried to answer both "example of why" and "example of how" 'cause I wasn't sure which he meant. But yeah, no, brevity is not my strong suit. :p
 

fronobulax

Developer
Staff member
No problem with verbosity or lack thereof. I'm not especially clear myself.

Inputs: String containing a date time group in SimpleDateFormat,
String containing a SimpleDateFormat specification

Output - First input string formatted according to the format in the second string.

My hypothesis, which I will confirm tomorrow is that I can just pass the parameters to the appropriate Java classes and let Java handle filling out "missing" components and invalid inputs so the biggest problem may be in defining what happens with bad input.
 

fronobulax

Developer
Staff member
r10078. This was harder than I thought.

format_dtg(inFormat, dtg, outFormat)

All parameters are strings. inFormat and outFormat are Java SimpleDateFormats. inFormat describes the format of dtg. In the absence of errors this reformats dtg into the format specified by outFormat. Pretty much any error returns "Bad parameter(s) passed to format_dtg". Errors occur if either format is not a SimpleDateFormat or if inFormat does not describe dtg well enough to parse it. Note that if the output calls for something that is missing in the input, something will get filled in anyway.

Code:
string input;
string format;
string informat;

input = today_to_string();
informat = "yyyyMMdd";
format = "yyyy MM dd";
print(input+" * "+format_dtg(informat, input, format));
format = "yyyy MM dd hh:mm:ss";
print(input+" * "+format_dtg(informat, input, format));
input = time_to_string();
print(input+" * "+format_dtg(informat, input, format));
informat = "hh:mm:ss z";
print(input+" * "+format_dtg(informat, input, format));

yields

20111206 * 2011 12 06
20111206 * 2011 12 06 12:00:00
11:43:35 EST * Bad parameter(s) passed to format_dtg
11:43:35 EST * 1970 01 01 11:43:35

If a minion would politely remind me to update the wiki once this has survived and proven useful, that would be appreciated.

P.S. "dtg" means Date Time Group and I recall that is not the most natural way for other folks to describe things. I am not committed to the name and it is trivial to change but felt "format" really described what was going on better than any alternatives I could think of.
 
Last edited:

Veracity

Developer
Staff member
I have no opinion on whether "Date Time Group" is natural or not, but I guarantee that "dtg" is not natural. There is no reason to abbreviate or use acronyms in library function names; a long, understandable name requires the user to type a few more characters - once, when typing the script. Call it "format_date_time()" or something.

Or, get more feedback on what to call it, but do not use abbreviations.
 

fronobulax

Developer
Staff member
Or, get more feedback on what to call it, but do not use abbreviations.

Roger. Name will be changed to format_date_time unless I get a better suggestion within the next couple of hours.

People around me say deeteegee as if it were a real word. I gravitated towards it but as I was writing the post commit comments I had a vague recollection that I had tried to use it in mafia before and had a similar reaction. Maybe next time I will remember.
 

holatuwol

Developer
I'm almost tempted to just give ASH access to Java Date objects. Then it'd just be format() and parse(). Or maybe parse_as_date().
 

StDoodle

Minion
Ooh that looks awesome, thank you for putting in more work than was anticipated (well, thanks for all work really).

Regarding the name, I tend to agree that more descriptive = better here; if a scripter doesn't like typing it, they can always pass it through their own super-abbreviated function *cough zarqon cough :p *. I tend to think V's suggestion or similar sounds good and unambiguous.
 
Top