#nullable enable #region using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text.RegularExpressions; using MySql.Data.MySqlClient; #endregion namespace website { public static class Utilities { private static readonly string MySqlPass = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "mysql.pass"); private static readonly string MySqlHost = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "mysql.host"); private static readonly string MySqlUser = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "mysql.user"); public static MySqlConnection getConnection() { return new MySqlConnection("server=" + MySqlHost + ";pwd=" + MySqlPass + ";uid=" + MySqlUser + ";database=san_antonio_senior_golf;Port=5618;Convert Zero Datetime=True;"); } /// /// Verify if string date can be converted to DateTime /// /// /// True if date matches "^[0-9]{4}/[0-9]{2}/[0-9]{2}$" public static bool verifyDate(string? date) { if (date == null) return false; Regex regex = new("^[0-9]{4}/[0-9]{2}/[0-9]{2}$", RegexOptions.IgnoreCase); return regex.IsMatch(date); } /// /// Check if date param is applicable with verifyDate /// /// Not null string /// DateTime equivalent of date param // TODO Creating 3 regex's is inefficient public static DateTime stringDateToDateTime([NotNull] string date) { int year = int.Parse(new Regex("^[0-9]{4}$").Match(date).Value); int month = int.Parse(new Regex("/[0-9]{2}/$").Match(date).Value); int day = int.Parse(new Regex("/[0-9]{2}$").Match(date).NextMatch().Value); return new DateTime(year, month, day); } /// /// Verify if string time can be converted to TimeSpan using stringTimeToTimeSpan /// /// /// True if time matches "^[0-9]{2}:[0-9]{2}:[0-9]{2}$" public static bool verifyTime(string? time) { if (time == null) return true; Regex regex = new("^[0-9]{2}:[0-9]{2}:[0-9]{2}$", RegexOptions.IgnoreCase); return regex.IsMatch(time); } /// /// Check if time param is applicable with verifyTime /// /// Not null string /// TimeSpan equivalent of time string public static TimeSpan stringTimeToTimeSpan([NotNull] string time) { Match match = new Regex("^[0-9]{2}$").Match(time); int hours = int.Parse(match.Value); int minutes = int.Parse(match.NextMatch().Value); int seconds = int.Parse(match.NextMatch().Value); return new TimeSpan(hours, minutes, seconds); } } }