@@ -12,6 +12,7 @@ module std.datetime.date;
1212import core.time ;// : TimeException;
1313import std.traits : isSomeString, Unqual;
1414import std.typecons : Flag;
15+ import std.range.primitives : isOutputRange;
1516
1617version (unittest ) import std.exception : assertThrown;
1718
@@ -7184,27 +7185,22 @@ public:
71847185
71857186
71867187 /+ +
7187- Converts this $(LREF Date) to a string with the format YYYYMMDD.
7188+ Converts this $(LREF Date) to a string with the format `YYYYMMDD`.
7189+ If `writer` is set, the resulting string will be written directly
7190+ to it.
7191+
7192+ Params:
7193+ writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
7194+ Returns:
7195+ A `string` when not using an output range; `void` otherwise.
71887196 +/
71897197 string toISOString () const @safe pure nothrow
71907198 {
7191- import std.format : format;
7192- try
7193- {
7194- if (_year >= 0 )
7195- {
7196- if (_year < 10_000)
7197- return format (" %04d%02d%02d" , _year, _month, _day);
7198- else
7199- return format (" +%05d%02d%02d" , _year, _month, _day);
7200- }
7201- else if (_year > - 10_000)
7202- return format (" %05d%02d%02d" , _year, _month, _day);
7203- else
7204- return format (" %06d%02d%02d" , _year, _month, _day);
7205- }
7206- catch (Exception e)
7207- assert (0 , " format() threw." );
7199+ import std.array : appender;
7200+ auto w = appender! string ();
7201+ w.reserve (8 );
7202+ toISOString(w);
7203+ return w.data;
72087204 }
72097205
72107206 // /
@@ -7239,28 +7235,58 @@ public:
72397235 assert (idate.toISOString() == " 19990706" );
72407236 }
72417237
7242- /+ +
7243- Converts this $(LREF Date) to a string with the format YYYY-MM-DD.
7244- +/
7245- string toISOExtString () const @safe pure nothrow
7238+ // / ditto
7239+ void toISOString (W)(ref W writer) const
7240+ if (isOutputRange! (W, char ))
72467241 {
7247- import std.format : format ;
7242+ import std.format : formattedWrite ;
72487243 try
72497244 {
72507245 if (_year >= 0 )
72517246 {
72527247 if (_year < 10_000)
7253- return format ( " %04d- %02d- %02d" , _year, _month, _day);
7248+ formattedWrite(writer, " %04d%02d%02d" , _year, _month, _day);
72547249 else
7255- return format ( " +%05d- %02d- %02d" , _year, _month, _day);
7250+ formattedWrite(writer, " +%05d%02d%02d" , _year, _month, _day);
72567251 }
72577252 else if (_year > - 10_000)
7258- return format ( " %05d- %02d- %02d" , _year, _month, _day);
7253+ formattedWrite(writer, " %05d%02d%02d" , _year, _month, _day);
72597254 else
7260- return format ( " %06d- %02d- %02d" , _year, _month, _day);
7255+ formattedWrite(writer, " %06d%02d%02d" , _year, _month, _day);
72617256 }
72627257 catch (Exception e)
7263- assert (0 , " format() threw." );
7258+ assert (0 , " formattedWrite() threw." );
7259+ }
7260+
7261+ @safe pure unittest
7262+ {
7263+ import std.array : appender;
7264+
7265+ auto w = appender! (char [])();
7266+ Date (2010 , 7 , 4 ).toISOString(w);
7267+ assert (w.data == " 20100704" );
7268+ w.clear();
7269+ Date (1998 , 12 , 25 ).toISOString(w);
7270+ assert (w.data == " 19981225" );
7271+ }
7272+
7273+ /+ +
7274+ Converts this $(LREF Date) to a string with the format `YYYY-MM-DD`.
7275+ If `writer` is set, the resulting string will be written directly
7276+ to it.
7277+
7278+ Params:
7279+ writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
7280+ Returns:
7281+ A `string` when not using an output range; `void` otherwise.
7282+ +/
7283+ string toISOExtString () const @safe pure nothrow
7284+ {
7285+ import std.array : appender;
7286+ auto w = appender! string ();
7287+ w.reserve (10 );
7288+ toISOExtString(w);
7289+ return w.data;
72647290 }
72657291
72667292 // /
@@ -7295,28 +7321,58 @@ public:
72957321 assert (idate.toISOExtString() == " 1999-07-06" );
72967322 }
72977323
7298- /+ +
7299- Converts this $(LREF Date) to a string with the format YYYY-Mon-DD.
7300- +/
7301- string toSimpleString () const @safe pure nothrow
7324+ // / ditto
7325+ void toISOExtString (W)(ref W writer) const
7326+ if (isOutputRange! (W, char ))
73027327 {
7303- import std.format : format ;
7328+ import std.format : formattedWrite ;
73047329 try
73057330 {
73067331 if (_year >= 0 )
73077332 {
73087333 if (_year < 10_000)
7309- return format ( " %04d-%s -%02d" , _year, monthToString( _month) , _day);
7334+ formattedWrite(writer, " %04d-%02d -%02d" , _year, _month, _day);
73107335 else
7311- return format ( " +%05d-%s -%02d" , _year, monthToString( _month) , _day);
7336+ formattedWrite(writer, " +%05d-%02d -%02d" , _year, _month, _day);
73127337 }
73137338 else if (_year > - 10_000)
7314- return format ( " %05d-%s -%02d" , _year, monthToString( _month) , _day);
7339+ formattedWrite(writer, " %05d-%02d -%02d" , _year, _month, _day);
73157340 else
7316- return format ( " %06d-%s -%02d" , _year, monthToString( _month) , _day);
7341+ formattedWrite(writer, " %06d-%02d -%02d" , _year, _month, _day);
73177342 }
73187343 catch (Exception e)
7319- assert (0 , " format() threw." );
7344+ assert (0 , " formattedWrite() threw." );
7345+ }
7346+
7347+ @safe pure unittest
7348+ {
7349+ import std.array : appender;
7350+
7351+ auto w = appender! (char [])();
7352+ Date (2010 , 7 , 4 ).toISOExtString(w);
7353+ assert (w.data == " 2010-07-04" );
7354+ w.clear();
7355+ Date (- 4 , 1 , 5 ).toISOExtString(w);
7356+ assert (w.data == " -0004-01-05" );
7357+ }
7358+
7359+ /+ +
7360+ Converts this $(LREF Date) to a string with the format `YYYY-Mon-DD`.
7361+ If `writer` is set, the resulting string will be written directly
7362+ to it.
7363+
7364+ Params:
7365+ writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
7366+ Returns:
7367+ A `string` when not using an output range; `void` otherwise.
7368+ +/
7369+ string toSimpleString () const @safe pure nothrow
7370+ {
7371+ import std.array : appender;
7372+ auto w = appender! string ();
7373+ w.reserve (11 );
7374+ toSimpleString(w);
7375+ return w.data;
73207376 }
73217377
73227378 // /
@@ -7351,6 +7407,40 @@ public:
73517407 assert (idate.toSimpleString() == " 1999-Jul-06" );
73527408 }
73537409
7410+ // / ditto
7411+ void toSimpleString (W)(ref W writer) const
7412+ if (isOutputRange! (W, char ))
7413+ {
7414+ import std.format : formattedWrite;
7415+ try
7416+ {
7417+ if (_year >= 0 )
7418+ {
7419+ if (_year < 10_000)
7420+ formattedWrite(writer, " %04d-%s-%02d" , _year, monthToString(_month), _day);
7421+ else
7422+ formattedWrite(writer, " +%05d-%s-%02d" , _year, monthToString(_month), _day);
7423+ }
7424+ else if (_year > - 10_000)
7425+ formattedWrite(writer, " %05d-%s-%02d" , _year, monthToString(_month), _day);
7426+ else
7427+ formattedWrite(writer, " %06d-%s-%02d" , _year, monthToString(_month), _day);
7428+ }
7429+ catch (Exception e)
7430+ assert (0 , " formattedWrite() threw." );
7431+ }
7432+
7433+ @safe pure unittest
7434+ {
7435+ import std.array : appender;
7436+
7437+ auto w = appender! (char [])();
7438+ Date (9 , 12 , 4 ).toSimpleString(w);
7439+ assert (w.data == " 0009-Dec-04" );
7440+ w.clear();
7441+ Date (- 10000 , 10 , 20 ).toSimpleString(w);
7442+ assert (w.data == " -10000-Oct-20" );
7443+ }
73547444
73557445 /+ +
73567446 Converts this $(LREF Date) to a string.
@@ -7390,6 +7480,12 @@ public:
73907480 assert (idate.toString());
73917481 }
73927482
7483+ // / ditto
7484+ void toString (W)(ref W writer) const
7485+ if (isOutputRange! (W, char ))
7486+ {
7487+ toSimpleString(writer);
7488+ }
73937489
73947490 /+ +
73957491 Creates a $(LREF Date) from a string with the format YYYYMMDD. Whitespace
0 commit comments