summaryrefslogtreecommitdiff
path: root/Gestor.Common/Gestor.Common.Helpers/Functions.cs
blob: b9f88cf42c06640f54ab7ad830300387a3ed2a3e (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
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using Gestor.Common.Validation;

namespace Gestor.Common.Helpers;

public class Functions
{
	public static Stopwatch Stopwatch;

	public static DateTime? StartTime;

	public static int Compare(string original, string modified)
	{
		if (original == null)
		{
			original = "";
		}
		if (modified == null)
		{
			modified = "";
		}
		string text = original.ToLongNullable().ToString();
		if (!string.IsNullOrWhiteSpace(text))
		{
			original = text;
		}
		text = modified.ToLongNullable().ToString();
		if (!string.IsNullOrWhiteSpace(text))
		{
			modified = text;
		}
		int length = original.Length;
		int length2 = modified.Length;
		int[,] array = new int[length + 1, length2 + 1];
		for (int i = 0; i <= length; i++)
		{
			array[i, 0] = i;
		}
		for (int j = 0; j <= length2; j++)
		{
			array[0, j] = j;
		}
		for (int k = 1; k <= length; k++)
		{
			for (int l = 1; l <= length2; l++)
			{
				int num = ((modified[l - 1] != original[k - 1]) ? 1 : 0);
				int[] source = new int[3]
				{
					array[k - 1, l] + 1,
					array[k, l - 1] + 1,
					array[k - 1, l - 1] + num
				};
				array[k, l] = source.Min();
				if (k > 1 && l > 1 && original[k - 1] == modified[l - 2] && original[k - 2] == modified[l - 1])
				{
					array[k, l] = Math.Min(array[k, l], array[k - 2, l - 2] + num);
				}
			}
		}
		return array[length, length2];
	}

	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;
		}
	}
}