std.getopt and std.datetime

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 12 23:55:09 PDT 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 at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170513/1026522d/attachment.sig>


More information about the Digitalmars-d-learn mailing list