1 #Region "Microsoft.VisualBasic::d63cd54a853a12b7706f778f74ce678c, Microsoft.VisualBasic.Core\Extensions\ValueTypes\DateTimeHelper.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module DateTimeHelper
35     
36     '         Properties: MonthList
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         FunctionDateSeq, FillDateZero, FromUnixTimeStamp, GetMonthInteger, IsEmpty
40     '                   UnixTimeStamp, YYMMDD
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 Imports System.Runtime.CompilerServices
48
49 Namespace ValueTypes
50
51     Public Module DateTimeHelper
52
53         ''' <summary>
54         ''' List of month names and its <see cref="Integer"/> value in a year
55         ''' </summary>
56         ''' <returns></returns>
57         Public ReadOnly Property MonthList As Dictionary(Of StringInteger)
58
59         Sub New()
60             MonthList = New Dictionary(Of StringInteger)
61
62             MonthList.Add("January", 1)
63             MonthList.Add("Jan", 1)
64
65             MonthList.Add("February", 2)
66             MonthList.Add("Feb", 2)
67
68             MonthList.Add("March", 3)
69             MonthList.Add("Mar", 3)
70
71             MonthList.Add("April", 4)
72             MonthList.Add("Apr", 4)
73
74             MonthList.Add("May", 5)
75
76             MonthList.Add("June", 6)
77             MonthList.Add("Jun", 6)
78
79             MonthList.Add("July", 7)
80             MonthList.Add("Jul", 7)
81
82             MonthList.Add("August", 8)
83             MonthList.Add("Aug", 8)
84
85             MonthList.Add("September", 9)
86             MonthList.Add("Sep", 9)
87
88             MonthList.Add("October", 10)
89             MonthList.Add("Oct", 10)
90
91             MonthList.Add("November", 11)
92             MonthList.Add("Nov", 11)
93
94             MonthList.Add("December", 12)
95             MonthList.Add("Dec", 12)
96         End Sub
97
98         ''' <summary>
99         ''' 从全称或者简称解析出月份的数字
100         ''' </summary>
101         ''' <param name="mon">大小写不敏感</param>
102         ''' <returns></returns>
103         Public Function GetMonthInteger(mon As StringAs Integer
104             If Not MonthList.ContainsKey(mon) Then
105                 For Each k As String In MonthList.Keys
106                     If String.Equals(mon, k, StringComparison.OrdinalIgnoreCase) Then
107                         Return MonthList(k)
108                     End If
109                 Next
110
111                 Return -1
112             Else
113                 Return MonthList(mon)
114             End If
115         End Function
116
117         ''' <summary>
118         ''' 00
119         ''' </summary>
120         ''' <param name="d"></param>
121         ''' <returns></returns>
122         ''' 
123         <MethodImpl(MethodImplOptions.AggressiveInlining)>
124         Public Function FillDateZero(d As IntegerAs String
125             Return If(d >= 10, d, "0" & d)
126         End Function
127
128         ''' <summary>
129         ''' 枚举出在<paramref name="start"/>到<paramref name="ends"/>这个时间窗里面的所有日期,单位为天
130         ''' </summary>
131         ''' <param name="start"></param>
132         ''' <param name="ends"></param>
133         ''' <returns>返回值里面包含有起始和结束的日期</returns>
134         Public Iterator Function DateSeq(start As Date, ends As DateAs IEnumerable(Of Date)
135             Do While start <= ends
136                 ' Yield $"{start.Year}-{If(start.Month < 10, "0" & start.Month, start.Month)}-{If(start.Day < 10, "0" & start.Day, start.Day)}"
137                 Yield start
138                 start = start.AddDays(1)
139             Loop
140         End Function
141
142         ''' <summary>
143         ''' yyyy-mm-dd
144         ''' </summary>
145         ''' <param name="x"></param>
146         ''' <returns></returns>
147         <Extension>
148         Public Function YYMMDD(x As DateAs String
149             Dim mm$ = If(x.Month < 10, "0" & x.Month, x.Month)
150             Dim dd$ = If(x.Day < 10, "0" & x.Day, x.Day)
151             Return $"{x.Year}-{mm}-{dd}"
152         End Function
153
154         ''' <summary>
155         ''' Convert <see cref="DateTime"/> to unix time stamp
156         ''' </summary>
157         ''' <param name="time"></param>
158         ''' <returns></returns>
159         <MethodImpl(MethodImplOptions.AggressiveInlining)>
160         <Extension>
161         Public Function UnixTimeStamp(time As DateTime) As Long
162             Static ZERO As New DateTime(1970, 1, 1, 0, 0, 0)
163             Return (time.ToUniversalTime - ZERO).TotalSeconds
164         End Function
165
166         ''' <summary>
167         ''' 将Unix时间戳转换为可读的日期
168         ''' </summary>
169         ''' <param name="unixDateTime"></param>
170         ''' <returns></returns>
171         <MethodImpl(MethodImplOptions.AggressiveInlining)>
172         <Extension>
173         Public Function FromUnixTimeStamp(unixDateTime As LongAs Date
174             Return DateTimeOffset _
175                 .FromUnixTimeSeconds(unixDateTime) _
176                 .DateTime _
177                 .ToLocalTime()
178         End Function
179
180         Const ZeroDate1$ = "0001-01-01, 00:00:00"
181         Const ZeroDate2$ = "0000-00-00, 00:00:00"
182         ''' <summary>
183         ''' 对于unix timestamp而言,这个日期是零
184         ''' </summary>
185         Const ZeroDate3$ = "1970-01-01, 08:00:00"
186
187         <Extension>
188         Public Function IsEmpty(time As DateOptional unixTimestamp As Boolean = FalseAs Boolean
189             Dim ts = time.FormatTime()
190
191             If ts = ZeroDate1 OrElse ts = ZeroDate2 Then
192                 Return True
193             ElseIf unixTimestamp AndAlso ts = ZeroDate3 Then
194                 Return True
195             Else
196                 Return False
197             End If
198         End Function
199     End Module
200 End Namespace