Based on your feedback and comments on our
resent post we decided to remove all system functions from Web Rule. This change will appear in the next build of version 2.0 beta, scheduled for release in a couple of days. Thanks to Adam's suggestion, this post provides the source code of those functions and all related objects:
using System;
using System.Text.RegularExpressions;
using CodeEffects.Rule.Common;
using CodeEffects.Rule.Attributes;
namespace CodeEffects.Rule.Core
{
internal sealed class SystemFunctions
{
public SystemFunctions() { }
public int Length(string input)
{
return input == null ? 0 : input.Length;
}
public bool IsToday(DateTime? date)
{
return this.Dates(date, DateTime.Now);
}
public bool IsUtcToday(DateTime? utcDate)
{
return this.Dates(utcDate, DateTime.UtcNow);
}
public bool IsNow(DateTime? time)
{
return this.Times(time, DateTime.Now);
}
public bool IsUtcNow(DateTime? utcTime)
{
return this.Times(utcTime, DateTime.Now);
}
public bool IsEmail(string input)
{
return Regex.IsMatch(
input,
Patterns.Email,
RegexOptions.IgnoreCase);
}
public bool IsUrl(string input)
{
return Regex.IsMatch(
input,
Patterns.PublicUrl,
RegexOptions.IgnoreCase);
}
public bool IsNumeric(string input)
{
if (string.IsNullOrEmpty(input)) return false;
decimal d = decimal.MinValue;
return decimal.TryParse(input, out d);
}
public bool IsDate(string input)
{
if (string.IsNullOrEmpty(input)) return false;
DateTime d = DateTime.MinValue;
return DateTime.TryParse(input, out d);
}
public bool IsTime(string input)
{
if (string.IsNullOrEmpty(input)) return false;
TimeSpan d = TimeSpan.MinValue;
return TimeSpan.TryParse(input, out d);
}
public int Hour(DateTime date)
{
return date.Hour;
}
public int Minute(DateTime date)
{
return date.Minute;
}
public int Second(DateTime date)
{
return date.Second;
}
public int Day(DateTime date)
{
return date.Day;
}
public Month Month(DateTime? date)
{
if(date == null) return Common.Month.Unknown;
switch (date.Value.Month)
{
case 1: return Common.Month.January;
case 2: return Common.Month.February;
case 3: return Common.Month.March;
case 4: return Common.Month.April;
case 5: return Common.Month.May;
case 6: return Common.Month.June;
case 7: return Common.Month.July;
case 8: return Common.Month.August;
case 9: return Common.Month.September;
case 10: return Common.Month.October;
case 11: return Common.Month.November;
case 12: return Common.Month.December;
default: return Common.Month.Unknown;
}
}
public int Year(DateTime date)
{
return date.Year;
}
public int DayOfYear(DateTime date)
{
return date.DayOfYear;
}
public Weekday Weekday(DateTime? date)
{
if(date == null)
return Common.Weekday.Unknown;
switch (date.Value.DayOfWeek)
{
case System.DayOfWeek.Sunday:
return Common.Weekday.Sunday;
case System.DayOfWeek.Monday:
return Common.Weekday.Monday;
case System.DayOfWeek.Tuesday:
return Common.Weekday.Tuesday;
case System.DayOfWeek.Wednesday:
return Common.Weekday.Wednesday;
case System.DayOfWeek.Thursday:
return Common.Weekday.Thursday;
case System.DayOfWeek.Friday:
return Common.Weekday.Friday;
case System.DayOfWeek.Saturday:
return Common.Weekday.Saturday;
default: return Common.Weekday.Unknown;
}
}
private bool Dates(DateTime? date, DateTime now)
{
if (date == null) return false;
else return date.Value.Day == now.Day &&
date.Value.Month == now.Month &&
date.Value.Year == now.Year;
}
private bool Times(DateTime? time, DateTime now)
{
if (time == null) return false;
else return time.Value.Hour == now.Hour &&
time.Value.Minute == now.Minute;
}
}
internal struct Patterns
{
internal static string
Email
{
get
{
return
@"^([\w-]+\.)*?[\w-]" +
@"+@[\w-]+\.([\w-]+\.)*?[\w]+$";
}
}
// I splitted the pattern into several pieces
// in order to fit it on this page. You might want
// to concatenate them back in one string.
internal static string
PublicUrl
{
get
{
return
@"^(http(s?)\:\/\/(((([\w\-]+\.)?[\w\-]+" +
@"(\.[A-Za-z]{2,5})+)|(localhost))|([\d]{1,3}" +
@"\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}))+(:[\d]" +
@"{1,5})?((\/([\w\.\-\/]+)?)|(\#([\w\.\-\/]+)?" +
@"))?(\?([^\+\? ]+)?)?)$";
}
}
}
}
namespace CodeEffects.Rule.Common
{
public enum Month
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12,
[ExcludeFromEvaluation]
Unknown = 13
}
public enum Weekday
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
[ExcludeFromEvaluation]
Unknown = 7
}
}