summaryrefslogtreecommitdiff
path: root/Gestor.Model/Gestor.Model.Validation/Funcoes.cs
blob: dd943d96f1c08b8b07611f4fbeff98a569e1282c (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
	}
}