std.getopt and std.datetime

Vladimir Panteleev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 12 23:05:37 PDT 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


More information about the Digitalmars-d-learn mailing list