forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTimeout.cs
More file actions
63 lines (52 loc) · 1.64 KB
/
NpgsqlTimeout.cs
File metadata and controls
63 lines (52 loc) · 1.64 KB
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
using System;
using System.Globalization;
using System.Threading;
// This type has been moved to the Npgsql.Util namespace for 5.0, but is kept here for backwards compatibility
// with 4.0.
namespace Npgsql
{
/// <summary>
/// Represents a timeout that will expire at some point.
/// </summary>
public readonly struct NpgsqlTimeout
{
readonly DateTime _expiration;
internal DateTime Expiration => _expiration;
internal static NpgsqlTimeout Infinite = new NpgsqlTimeout(TimeSpan.Zero);
internal NpgsqlTimeout(TimeSpan expiration)
{
_expiration = expiration == TimeSpan.Zero
? DateTime.MaxValue
: DateTime.UtcNow + expiration;
}
internal void Check()
{
if (HasExpired)
throw new TimeoutException();
}
internal bool IsSet => _expiration != DateTime.MaxValue;
internal bool HasExpired => DateTime.UtcNow >= Expiration;
internal TimeSpan TimeLeft => IsSet ? Expiration - DateTime.UtcNow : Timeout.InfiniteTimeSpan;
}
sealed class CultureSetter : IDisposable
{
readonly CultureInfo _oldCulture;
internal CultureSetter(CultureInfo newCulture)
{
_oldCulture = CultureInfo.CurrentCulture;
#if NET461
Thread.CurrentThread.CurrentCulture = newCulture;
#else
CultureInfo.CurrentCulture = newCulture;
#endif
}
public void Dispose()
{
#if NET461
Thread.CurrentThread.CurrentCulture = _oldCulture;
#else
CultureInfo.CurrentCulture = _oldCulture;
#endif
}
}
}