-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputValidator.cs
More file actions
53 lines (40 loc) · 2.01 KB
/
InputValidator.cs
File metadata and controls
53 lines (40 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchedulingApp
{
public static class InputValidator
{
// Validates and formats phone number. Returns formatted string (XXX-XXX-XXXX) if valid, otherwise null.
public static string ValidateAndFormatPhone(string input)
{
if (string.IsNullOrWhiteSpace(input)) return null;
// Removes non-digits
string digits = new string(input.Where(char.IsDigit).ToArray());
// Requires exactly 10 digits
if (digits.Length != 10)
return null;
// Formats as XXX-XXX-XXXX
return $"{digits.Substring(0, 3)}-{digits.Substring(3, 3)}-{digits.Substring(6)}";
}
// Business hours validation helper
public static bool ValidateBusinessHours(DateTime startUtc, DateTime endUtc)
{
if (endUtc <= startUtc) return false;
TimeZoneInfo eastern = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime startEst = TimeZoneInfo.ConvertTimeFromUtc(DateTime.SpecifyKind(startUtc, DateTimeKind.Utc), eastern);
DateTime endEst = TimeZoneInfo.ConvertTimeFromUtc(DateTime.SpecifyKind(endUtc, DateTimeKind.Utc), eastern);
bool startIsWeekday = startEst.DayOfWeek >= DayOfWeek.Monday && startEst.DayOfWeek <= DayOfWeek.Friday;
bool endIsWeekday = endEst.DayOfWeek >= DayOfWeek.Monday && endEst.DayOfWeek <= DayOfWeek.Friday;
bool sameEasternDay = startEst.Date == endEst.Date; // Appointments must start and end on the same calendar day in EST
TimeSpan open = new TimeSpan(9, 0, 0);
TimeSpan close = new TimeSpan(17, 0, 0);
bool withinHours =
startEst.TimeOfDay >= open &&
endEst.TimeOfDay <= close;
return startIsWeekday && endIsWeekday && sameEasternDay && withinHours;
}
}
}