Skip to content

Commit 3b3c3cb

Browse files
authored
Merge pull request #6173 from JackStouffer/timezone-tostring
Work On Issue 10828 - Add Writer Versions of TimeZone toStrings merged-on-behalf-of: Jonathan M Davis <[email protected]>
2 parents 12a2f9b + a55744a commit 3b3c3cb

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

std/datetime/timezone.d

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,17 +1459,33 @@ package:
14591459
west).
14601460
+/
14611461
static string toISOString(Duration utcOffset) @safe pure
1462+
{
1463+
import std.array : appender;
1464+
auto w = appender!string();
1465+
w.reserve(5);
1466+
toISOString(w, utcOffset);
1467+
return w.data;
1468+
}
1469+
1470+
// ditto
1471+
static void toISOString(W)(ref W writer, Duration utcOffset)
1472+
if (isOutputRange!(W, char))
14621473
{
14631474
import std.datetime.date : DateTimeException;
14641475
import std.exception : enforce;
1465-
import std.format : format;
1476+
import std.format : formattedWrite;
14661477
immutable absOffset = abs(utcOffset);
14671478
enforce!DateTimeException(absOffset < dur!"minutes"(1440),
14681479
"Offset from UTC must be within range (-24:00 - 24:00).");
14691480
int hours;
14701481
int minutes;
14711482
absOffset.split!("hours", "minutes")(hours, minutes);
1472-
return format(utcOffset < Duration.zero ? "-%02d%02d" : "+%02d%02d", hours, minutes);
1483+
formattedWrite(
1484+
writer,
1485+
utcOffset < Duration.zero ? "-%02d%02d" : "+%02d%02d",
1486+
hours,
1487+
minutes
1488+
);
14731489
}
14741490

14751491
@safe unittest
@@ -1514,9 +1530,19 @@ package:
15141530
west).
15151531
+/
15161532
static string toISOExtString(Duration utcOffset) @safe pure
1533+
{
1534+
import std.array : appender;
1535+
auto w = appender!string();
1536+
w.reserve(6);
1537+
toISOExtString(w, utcOffset);
1538+
return w.data;
1539+
}
1540+
1541+
// ditto
1542+
static void toISOExtString(W)(ref W writer, Duration utcOffset)
15171543
{
15181544
import std.datetime.date : DateTimeException;
1519-
import std.format : format;
1545+
import std.format : formattedWrite;
15201546
import std.exception : enforce;
15211547

15221548
immutable absOffset = abs(utcOffset);
@@ -1525,7 +1551,12 @@ package:
15251551
int hours;
15261552
int minutes;
15271553
absOffset.split!("hours", "minutes")(hours, minutes);
1528-
return format(utcOffset < Duration.zero ? "-%02d:%02d" : "+%02d:%02d", hours, minutes);
1554+
formattedWrite(
1555+
writer,
1556+
utcOffset < Duration.zero ? "-%02d:%02d" : "+%02d:%02d",
1557+
hours,
1558+
minutes
1559+
);
15291560
}
15301561

15311562
@safe unittest

0 commit comments

Comments
 (0)