Reputation: 65
I'm working on a project with TimeDomains. In this project a TimeDomain has to be transformed to written text.
For example, the following TimeDomain is passed as a string: [[(t2){d5}]*[(h16){h2}]]
The problem here is that I can't think of a way to determine which day the d-value represents based on the t-value. If d would be a fixed value it would be easy, however d could be anything from 1 to 7.
I have the following code:
string dayFromName;
StringBuilder stringBuilder = new StringBuilder();
foreach (string s in TimeDomain)
{
var indexOfFirstBraceDayFrom = s.IndexOf('t') + 1;
var indexOfLastBraceDayFrom = s.IndexOf(')');
var dayFromTemp = s.Substring(indexOfFirstBraceDayFrom, indexOfLastBraceDayFrom - indexOfFirstBraceDayFrom).Split(' ');
var dayFrom = dayFromTemp[0];
var indexOfFirstBraceDayTo = s.IndexOf('d') + 1;
var indexOfLastBraceDayTo = s.IndexOf('}');
var dayToTemp = s.Substring(indexOfFirstBraceDayTo, indexOfLastBraceDayTo - indexOfFirstBraceDayTo).Split(' ');
var dayTo = dayToTemp[0];
if (dayFrom == "1")
dayFromName = "Sunday";
if (dayFrom == "2")
dayFromName = "Monday";
if (dayFrom == "3")
dayFromName = "Tuesday";
if (dayFrom == "4")
dayFromName = "Wednesday";
if (dayFrom == "5")
dayFromName = "Thursday";
if (dayFrom == "6")
dayFromName = "Friday";
if (dayFrom == "7")
dayFromName = "Saturday";
stringBuilder.Append(dayFromName + "to");
}
However I'm not sure how to define the day after "to" based on the value of D in reference to T. Do you have any suggestions?
Upvotes: 2
Views: 109
Reputation: 4833
Reusing your code it will look like:
string dayFromName;
StringBuilder stringBuilder = new StringBuilder();
foreach (string s in TimeDomain)
{
var dayTomName = string.Empty;
var dayFromName = string.Empty;
var indexOfFirstBraceDayFrom = s.IndexOf('t') + 1;
var indexOfLastBraceDayFrom = s.IndexOf(')');
var dayFromTemp = s.Substring(indexOfFirstBraceDayFrom, indexOfLastBraceDayFrom - indexOfFirstBraceDayFrom).Split(' ');
var dayFrom = dayFromTemp[0];
var indexOfFirstBraceDayTo = s.IndexOf('d') + 1;
var indexOfLastBraceDayTo = s.IndexOf('}');
var dayToTemp = s.Substring(indexOfFirstBraceDayTo, indexOfLastBraceDayTo - indexOfFirstBraceDayTo).Split(' ');
var dayTo = dayToTemp[0];
dayToName = ((System.DayOfWeek)((int.Parse(dayFrom) + int.Parse(dayTo) - 2) % 7)).ToString();
dayFromName = ((System.DayOfWeek)(int.Parse(dayFrom) - 1)).ToString();
}
Upvotes: 0
Reputation: 21766
You can just use System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames
with the only difference that the index starts at 0
. The below query will return Sunday assuming your current culture is English:
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.DayNames[0])
So you can just do this:
int dayNumber = int.Parse(dayFrom) - 1;
dayFromName = CultureInfo.CurrentCulture.DateTimeFormat.DayNames[dayNumber];
Or even shorter and culture independent as pointed out by @cFrozenDeath:
var dayFromName2 = (System.DayOfWeek)dayNumber;
Upvotes: 1
Reputation: 33506
Instead of building an if-1-if-2-if-3 "if-tree", create a lookup dictionary:
var daylookup = new Dictionary<int, string>();
daylookup[1] = "Sunday";
daylookup[2] = "Monday";
...
daylookup[7] = "Saturday";
now, instead of writing lots of IFs, you can simply ask the dictionary:
dayName = daylookup[ dayNumber ];
This has an extra bonus. Since the day number is just an integer number, you can add, subtract, etc and nothing changes:
dayName = daylookup[ dayNumber + 1 ]; // the NEXT day
//or
dayName = daylookup[ dayNumber + 2 ]; // the DAY AFTER NEXT
//or
dayName = daylookup[ dayNumber + N ]; // the Nth day forward
that's the basic idea.
However, dictionaries and math is not as simple. Unless you put more, your Dictionary has only entries for 1,2,3,4,5,6,7 and trying to read an entry i.e. 10 (day 6+2) will throw an exception: there is no entry for "10".
That's easily solvable:
%
( not Day+Offset, but (Day+Offset)%7 )Upvotes: 3
Reputation: 137128
I don't see the problem.
Once you've worked out the day from the d value you can work out what the "to" day is from that. If you add the "d" value (taking into account the inclusivity and then do modulo arithmetic on the result you can get the day number:
int toDay = (tValue + dValue - 1) % 7;
Then do the same if/switch statement (refactor your code!) as for the from day.
You'll need to parse your "t" and "d" values into integers, but having got it as an integer you'll be able to convert to the day name via the DayOfWeek
enumeration - though you'll have to offset your number to the value expected by Microsoft.
Upvotes: 3