I filled in some of the functions from Andrei's first draft of
std.gregorian module. I hope they are good enough.
I changed some identifiers like GregYear, GregMonth, etc to
GregorianYear, GregorianMonth, etc so as to be consistent with the
'julian' identifiers but it's a minor changes that maybe I shouldn't
have touched.
Also, I have introduced a unjoin() function as a helper function. It
splits a string into an array of lines using the specified array of
characters as delimiters. I am not sure if there is already an existing
function that does the same but I could not find it. For lack of a
better word I opted for the opposite of the join() function in std.string.
string[] unjoin(string s, char[] ch)
{
uint start = 0;
uint i = 0;
string[] result;
for (i = 0; i < s.length; i++) {
if (indexOf(ch, s[i]) != -1) {
result ~= s[start..i];
start = i + 1;
}
}
if (start < i) {
result ~= s[start..$];
}
return result;
}
unittest {
string s = "2010-05-31";
string[] r = unjoin(s, ['/', '-', '.', ',', '\\']);
assert(r[0] == "2010");
assert(r[1] == "05");
assert(r[2] == "31");
}
I have modified the signature of fromString() and
fromUndelimitedString() to accept string arguments instead of char[]. I
am not sure if it is alright with Andrei.
Here's a list of what I have implemented so far:
- Date fromString(in string s)
- Date fromUndelimitedString(in string s)
- @property string toSimpleString()
- @property string toIsoString()
- @property string toIsoExtendedString()
- added string[] months used only by toSimpleString()
- unit tests
I have attached the .diff file gregorian.diff
Regards,
negerns
19c19,23
< import core.sys.posix.time;
---
> version (Windows) {
> import core.stdc.time;
> } else version (linux) {
> import core.sys.posix.time;
> }
20a25
> import std.string;
46,47c51,58
< auto d1 = fromString("2002-1-25");
< auto d2 = fromUndelimitedString("20020125");
---
> auto d = Date(2010, May, 31);
> assert(d.toSimpleString == "2010-May-31");
> assert(d.toIsoString == "20100531");
> assert(d.toIsoExtendedString == "2010-05-31");
> auto d1 = fromString("2002-5-25");
> assert(d1 == d);
> auto d2 = fromUndelimitedString("20020525");
> assert(d2 == d);
56,60c67,71
< alias ushort GregYear;
< alias ushort GregMonth;
< alias ushort GregDay;
< alias uint GregDayOfWeek;
< alias uint GregDayOfYear;
---
> alias ushort GregorianYear;
> alias ushort GregorianMonth;
> alias ushort GregorianDay;
> alias uint GregorianDayOfWeek;
> alias uint GregorianDayOfYear;
62a74
> string[] months = ["January", "February", "March", "April", "May", "June",
> "July", "August", "September", "October", "November", "December"];
76c88
< this(uint year, uint month, uint day)
---
> this(GregorianYear year, GregorianMonth month, GregorianDay day)
91c103
< @property GregYear year() const
---
> @property GregorianYear year() const
107c119
< @property GregMonth month() const
---
> @property GregorianMonth month() const
123c135
< @property GregDay day() const
---
> @property GregorianDay day() const
139,140c151
< @property Tuple!(GregYear, GregMonth, GregDay)
< yearMonthDay() const
---
> @property Tuple!(GregorianYear, GregorianMonth, GregorianDay)
> yearMonthDay() const
156c167
< @property GregDayOfWeek dayOfWeek() const
---
> @property GregorianDayOfWeek dayOfWeek() const
168c179
< @property GregDayOfYear dayOfYear() const
---
> @property GregorianDayOfYear dayOfYear() const
172c183
< return cast(GregDayOfYear) doy;
---
> return cast(GregorianDayOfYear) doy;
302c313
< return text(ymd._0, '-', ymd._1, '-', ymd._2);
---
> return text(ymd._0, '-', months[ymd._1 - 1][0..3], '-',
> zfill(to!string(ymd._2), 2));
307c318,319
< assert(0);
---
> auto ymd = yearMonthDay;
> return text(ymd._0, zfill(to!string(ymd._1), 2),
> zfill(to!string(ymd._2), 2));
312c324,325
< assert(0);
---
> auto ymd = yearMonthDay;
> return text(ymd._0, '-', zfill(to!string(ymd._1), 2), '-',
> zfill(to!string(ymd._2), 2));
379c392,423
< Date fromString(in char[] s)
---
> /**
> * Because 'split' has already been used, the weird name will be
> * changed in the future when an appropriate identifier is chosen.
> */
> string[] unjoin(string s, char[] ch)
> {
> uint start = 0;
> uint i = 0;
> string[] result;
>
> for (i = 0; i < s.length; i++) {
> if (indexOf(ch, s[i]) != -1) {
> result ~= s[start..i];
> start = i + 1;
> }
> }
> if (start < i) {
> result ~= s[start..$];
> }
> return result;
> }
>
> unittest {
> string s = "2010-05-31";
> string[] r = unjoin(s, ['/', '-', '.', ',', '\\']);
> assert(r[0] == "2010");
> assert(r[1] == "05");
> assert(r[2] == "31");
> }
>
> //Date fromString(in char[] s)
> Date fromString(in string s)
381a426,431
> string[] r = unjoin(s, ['/', '-', '.', '\\']);
> if (r.length == 0) {
> result = Date(1970, Jan, 1); // or some other default date value
> } else {
> result = Date(to!ushort(r[0]), to!ushort(r[1]), to!ushort(r[2]));
> }
385c435,436
< Date fromUndelimitedString(in char[] s)
---
> //Date fromUndelimitedString(in char[] s)
> Date fromUndelimitedString(in string s)
387a439,443
> if (s.length == 0) {
> result = Date(1970, Jan, 1); // or some other default date value
> } else {
> result = Date(to!ushort(s[0..4]), to!ushort(s[4..6]),
> to!ushort(s[6..$]));
> }