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.win...@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
signature.asc
Description: This is a digitally signed message part