Created
January 12, 2017 12:02
-
-
Save mykeels/e51fadc58ec1aee5ec0eb2b98caf3038 to your computer and use it in GitHub Desktop.
WorkHourInfo can take strings as parameters, and handle datetime too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WorkHourInfo | |
{ | |
public string dayOfWeek { get; set; } | |
public DateTime? startTime { get; set; } | |
public DateTime? endTime { get; set; } | |
public string start | |
{ | |
get | |
{ | |
return startTime == null ? "closed" : Convert.ToDateTime(startTime).ToShortTimeString(); | |
} | |
} | |
public string end | |
{ | |
get | |
{ | |
return endTime == null ? "closed" : Convert.ToDateTime(endTime).ToShortTimeString(); | |
} | |
} | |
public bool closed | |
{ | |
get | |
{ | |
return (startTime == null) && (endTime == null); | |
} | |
} | |
public string workHrs | |
{ | |
set | |
{ | |
this.setWorkHours(value); | |
} | |
} | |
public WorkHourInfo() { } | |
public WorkHourInfo(string start, string end, string dayOfWeek = null) | |
{ | |
this.startTime = DateTime.Parse(start); | |
this.endTime = DateTime.Parse(end); | |
this.dayOfWeek = dayOfWeek; | |
} | |
public WorkHourInfo(DateTime start, DateTime end, string dayOfWeek = null) | |
{ | |
this.startTime = start; | |
this.endTime = start; | |
this.dayOfWeek = dayOfWeek; | |
} | |
/// <summary> | |
/// Expects Format: "Monday:09.00AM - 08.30PM" | |
/// </summary> | |
/// <param name="dbString"></param> | |
public WorkHourInfo(string dbString) | |
{ | |
this.setWorkHours(dbString); | |
} | |
private void setWorkHours(string dbString) | |
{ | |
if (String.IsNullOrEmpty(dbString)) return; | |
string[] splits = dbString.Split(':'); | |
this.dayOfWeek = WeekDay.GetWeekDay(splits.FirstOrDefault()); | |
if (splits.LastOrDefault()?.ToLower() != "closed") | |
{ | |
string[] times = splits.LastOrDefault()?.Replace(".", ":").Split('-'); | |
this.startTime = DateTime.Parse(times.FirstOrDefault()?.Trim()); | |
this.endTime = DateTime.Parse(times.LastOrDefault()?.Trim()); | |
} | |
} | |
public class WeekDay | |
{ | |
public const string Monday = "Monday"; | |
public const string Tuesday = "Tuesday"; | |
public const string Wednesday = "Wednesday"; | |
public const string Thursday = "Thursday"; | |
public const string Friday = "Friday"; | |
public const string Saturday = "Saturday"; | |
public const string Sunday = "Sunday"; | |
public static string[] GetWeekDays() | |
{ | |
return new string[] | |
{ | |
Sunday, | |
Monday, | |
Tuesday, | |
Wednesday, | |
Thursday, | |
Friday, | |
Saturday | |
}; | |
} | |
public static string GetWeekDay(string partialWeekDay) //some week days are mispelled ... this fixes that, i guess | |
{ | |
return GetWeekDays().First((day) => | |
{ | |
return day.ToLower().Substring(0, 3) == partialWeekDay.ToLower().Substring(0, 3); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment