www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.getopt and std.datetime

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
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 12
parent reply Vladimir Panteleev <thecybershadow.lists gmail.com> writes:
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 12
parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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=20
 objects using std.getopt?
=20 As std.getopt is going to give you strings, you need to convert=20 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;
=20
 void main()
 {
 	string[] args =3D ["program", "--date", "2017-May-13 05:58:59"];
 	SysTime t;
 	getopt(args,
 		"date", (string _, string s) { t =3D
 SysTime.fromSimpleString(s);=20
 },
 	);
 	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 =3D t.toISOString(); getopt(args, "date|d", "Some explanation of the d option.", &buffer); t =3D 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.=20 --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 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 12