Thread overview
std.getopt and std.datetime
May 13, 2017
Russel Winder
May 13, 2017
Vladimir Panteleev
May 13, 2017
Russel Winder
May 13, 2017
Is there a canonical, idiomatic way of processing std.datetime objects using std.getopt?

Currently, I am suffering:

/usr/include/d/std/getopt.d(921): Error: static assert  "Dunno how to deal with type SysTime*"

which on the one hand is understandable, albeit dreadful English, but then I suppose it is American not English, as std.getopt is only going to process builtin types, but it is very annoying.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

May 13, 2017
On Saturday, 13 May 2017 at 05:53:25 UTC, Russel Winder wrote:
> Is there a canonical, idiomatic way of processing std.datetime objects using std.getopt?

As std.getopt is going to give you strings, you need to convert strings to SysTime values, e.g. using fromSimpleString:

import std.datetime;
import std.getopt;
import std.stdio;

void main()
{
	string[] args = ["program", "--date", "2017-May-13 05:58:59"];
	SysTime t;
	getopt(args,
		"date", (string _, string s) { t = SysTime.fromSimpleString(s); },
	);
	writeln(t);
}

For more flexibility, you'll need a date parser. Mine is here:
https://github.com/CyberShadow/ae/blob/master/utils/time/parse.d
May 13, 2017
On Sat, 2017-05-13 at 06:05 +0000, Vladimir Panteleev via Digitalmars- d-learn wrote:
> On Saturday, 13 May 2017 at 05:53:25 UTC, Russel Winder wrote:
> > Is there a canonical, idiomatic way of processing std.datetime objects using std.getopt?
> 
> As std.getopt is going to give you strings, you need to convert strings to SysTime values, e.g. using fromSimpleString:

std.getopt appears also to be able to deal with integers as well as strings.

> import std.datetime;
> import std.getopt;
> import std.stdio;
> 
> void main()
> {
> 	string[] args = ["program", "--date", "2017-May-13 05:58:59"];
> 	SysTime t;
> 	getopt(args,
> 		"date", (string _, string s) { t =
> SysTime.fromSimpleString(s);
> },
> 	);
> 	writeln(t);
> }

I hadn't realised you could put a function in the argument sequence, I had the address of the variable to amend:

    SysTime t;
    auto buffer = t.toISOString();
    getopt(args,
    "date|d",
    "Some explanation of the d option.",
    &buffer);
    t = SysTime.fromISOString(buffer);

I think I like your way better. :-)

> For more flexibility, you'll need a date parser. Mine is here: https://github.com/CyberShadow/ae/blob/master/utils/time/parse.d

I am only interested in ISO8601 dates. However looking at this code is interesting as I learnt some stuff.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder