summaryrefslogtreecommitdiff
path: root/Gestor.Model/Gestor.Model.Validation/Funcoes.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Gestor.Model/Gestor.Model.Validation/Funcoes.cs')
-rw-r--r--Gestor.Model/Gestor.Model.Validation/Funcoes.cs107
1 files changed, 107 insertions, 0 deletions
diff --git a/Gestor.Model/Gestor.Model.Validation/Funcoes.cs b/Gestor.Model/Gestor.Model.Validation/Funcoes.cs
new file mode 100644
index 0000000..dd943d9
--- /dev/null
+++ b/Gestor.Model/Gestor.Model.Validation/Funcoes.cs
@@ -0,0 +1,107 @@
+using System;
+using System.ComponentModel;
+using System.Diagnostics;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Reflection;
+using Gestor.Model.Common;
+
+namespace Gestor.Model.Validation;
+
+public static class Funcoes
+{
+ public static Stopwatch Stopwatch;
+
+ public static DateTime? StartTime;
+
+ public static string GetDescription(this Enum genericEnum)
+ {
+ if (genericEnum == null)
+ {
+ return "";
+ }
+ MemberInfo[] member = genericEnum.GetType().GetMember(genericEnum.ToString());
+ if (member.Length == 0)
+ {
+ return genericEnum.ToString();
+ }
+ object[] customAttributes = member[0].GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
+ if (!customAttributes.Any())
+ {
+ return genericEnum.ToString();
+ }
+ return ((DescriptionAttribute)customAttributes.ElementAt(0)).Description;
+ }
+
+ public static ParentescoVinculo? ParentescoInverso(ParentescoVinculo? parentesco)
+ {
+ return parentesco switch
+ {
+ ParentescoVinculo.MaePai => ParentescoVinculo.Filho,
+ ParentescoVinculo.Filho => ParentescoVinculo.MaePai,
+ ParentescoVinculo.Avo => ParentescoVinculo.Neto,
+ ParentescoVinculo.Neto => ParentescoVinculo.Avo,
+ ParentescoVinculo.Tio => ParentescoVinculo.Sobrinho,
+ ParentescoVinculo.Sobrinho => ParentescoVinculo.Tio,
+ ParentescoVinculo.Bisavo => ParentescoVinculo.Bisneto,
+ ParentescoVinculo.Bisneto => ParentescoVinculo.Bisavo,
+ ParentescoVinculo.Sogro => ParentescoVinculo.Genro,
+ ParentescoVinculo.Genro => ParentescoVinculo.Sogro,
+ ParentescoVinculo.MadrastaPadrasto => ParentescoVinculo.Enteado,
+ ParentescoVinculo.Enteado => ParentescoVinculo.MadrastaPadrasto,
+ ParentescoVinculo.Proprietario => ParentescoVinculo.Propriedade,
+ ParentescoVinculo.Propriedade => ParentescoVinculo.Proprietario,
+ ParentescoVinculo.Funcionario => ParentescoVinculo.Contratante,
+ ParentescoVinculo.Contratante => ParentescoVinculo.Funcionario,
+ _ => parentesco,
+ };
+ }
+
+ public static DateTime GetNetworkTime()
+ {
+ try
+ {
+ if (StartTime.HasValue)
+ {
+ return StartTime.Value.AddMilliseconds(Stopwatch.ElapsedMilliseconds);
+ }
+ byte[] array = new byte[48];
+ array[0] = 27;
+ IPEndPoint remoteEP = new IPEndPoint(Dns.GetHostEntry("time.google.com").AddressList.First((IPAddress a) => a.AddressFamily == AddressFamily.InterNetwork), 123);
+ using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
+ {
+ socket.Connect(remoteEP);
+ socket.ReceiveTimeout = 3000;
+ socket.Send(array);
+ socket.Receive(array);
+ socket.Close();
+ }
+ ulong num = ((ulong)array[40] << 24) | ((ulong)array[41] << 16) | ((ulong)array[42] << 8) | array[43];
+ ulong num2 = ((ulong)array[44] << 24) | ((ulong)array[45] << 16) | ((ulong)array[46] << 8) | array[47];
+ ulong num3 = num * 1000 + num2 * 1000 / 4294967296L;
+ DateTime value = new DateTime(1900, 1, 1).AddMilliseconds((long)num3).ToLocalTime();
+ StartTime = value;
+ Stopwatch = Stopwatch.StartNew();
+ return StartTime.Value;
+ }
+ catch (Exception)
+ {
+ StartTime = DateTime.Now;
+ Stopwatch = Stopwatch.StartNew();
+ return StartTime.Value;
+ }
+ }
+
+ public static int GetAge(DateTime birth)
+ {
+ DateTime date = GetNetworkTime().Date;
+ int num = date.Year - birth.Year;
+ DateTime dateTime = date.AddYears(-num);
+ if (birth > dateTime)
+ {
+ num--;
+ }
+ return num;
+ }
+}