Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web / Util / DateTimeUtil.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="FileChangesMonitor.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 namespace System.Web.Util {
8
9     using System;
10
11     internal enum TimeUnit {
12         Unknown         = 0,
13         Days,
14         Hours,
15         Minutes,
16         Seconds,
17         Milliseconds
18     };
19
20     internal sealed class DateTimeUtil {
21         private DateTimeUtil() {}
22         const long FileTimeOffset = 504911232000000000;
23         static readonly DateTime    MinValuePlusOneDay = DateTime.MinValue.AddDays(1);
24         static readonly DateTime    MaxValueMinusOneDay = DateTime.MaxValue.AddDays(-1);
25
26         static internal DateTime FromFileTimeToUtc(long filetime) {
27             long universalTicks = filetime + FileTimeOffset;
28             // Dev10 733288: Caching: behavior change for CacheDependency when using UseMemoryCache=1
29             // ObjectCacheHost converts DateTime to a DateTimeOffset, and the conversion requires
30             // that DateTimeKind be set correctly
31             return new DateTime(universalTicks, DateTimeKind.Utc);
32         }
33
34         static internal DateTime ConvertToUniversalTime(DateTime localTime) {
35             if (localTime < MinValuePlusOneDay) {
36                 return DateTime.MinValue;
37             }
38
39             if (localTime > MaxValueMinusOneDay) {
40                 return DateTime.MaxValue;
41             }
42
43             return localTime.ToUniversalTime();
44         }
45
46         static internal DateTime ConvertToLocalTime(DateTime utcTime) {
47             if (utcTime < MinValuePlusOneDay) {
48                 return DateTime.MinValue;
49             }
50
51             if (utcTime > MaxValueMinusOneDay) {
52                 return DateTime.MaxValue;
53             }
54
55             return utcTime.ToLocalTime();
56         }
57
58         static internal TimeSpan GetTimeoutFromTimeUnit(int timeoutValue, TimeUnit timeoutUnit) {
59             switch (timeoutUnit) {
60                 case TimeUnit.Days:
61                     return new TimeSpan(timeoutValue, 0, 0, 0);
62                 case TimeUnit.Hours:
63                     return new TimeSpan(timeoutValue, 0, 0);
64                 case TimeUnit.Seconds:
65                     return new TimeSpan(0, 0, timeoutValue);
66                 case TimeUnit.Milliseconds:
67                     return new TimeSpan(0, 0, 0, 0, timeoutValue);
68                 case TimeUnit.Minutes:
69                     return new TimeSpan(0, timeoutValue, 0);
70                 case TimeUnit.Unknown:
71                 default:
72                     break;
73             }
74
75             throw new ArgumentException(SR.GetString(SR.InvalidArgumentValue, "timeoutUnit"));
76         }
77     }
78 }
79
80