VB2012:我正在从遗留系统中读取数据。其中一个字段是时间,我正在将其读入DateTime变量。我使用“hhmmt”格式用DateTime.ParseExact解析日期。我唯一的问题是遗留系统显示A代表AM,P代表PM代表中午N的特殊情况。那么.NET不喜欢N指示符,所以我检测到它并将其改为P.很棒。
003 0300 AAABBB 845A 1200N
005 1400 CCCDDD 1055A 240P
007 7000 EEEFFF 306P 531P
现在我做一些处理并将数据打印到文件中。我想以遗留系统中打印的格式打印出时间。我的第一个想法是使用自定义DateTimeFormatInfo
Dim info As New DateTimeFormatInfo
info.PMDesignator = "N"
并将其作为IFormatProvider
提供给我的字符串格式化程序trip.ArriveTime.ToString("hmmt", info)
但是我意识到设置DateTimeFormatInfo是静态的,当时间是12:00P或中午时没有任何用处。有没有办法创建一个能够解释这个独特场景的格式,并在中午时使用N后缀但是为其他时间保留标准A表示AM和P表示PM?
使用可能的解决方案进行更新:
Imports System.Text.RegularExpressions
Imports System.Globalization
Module mdlExtensions
Private rgxTimePeriod As New Regex("t+")
<System.Runtime.CompilerServices.Extension()> _
Public Function LegacyFormat(dt As DateTime, fmt As String, Optional provider As IFormatProvider = Nothing) As String
Dim formatted As String
If dt.TimeOfDay = New TimeSpan(12, 0, 0) Then
fmt = rgxTimePeriod.Replace(fmt, "N")
End If
If provider Is Nothing Then
formatted = dt.ToString(fmt)
Else
formatted = dt.ToString(fmt, provider)
End If
Return formatted
End Function
<System.Runtime.CompilerServices.Extension()> _
Public Function ToLegacy(dt As DateTime, fmt As String, Optional provider As IFormatProvider = Nothing) As String
'setup the master DateTimeFormatInfo
Dim ci As CultureInfo = CType(provider, CultureInfo)
Dim mstrDtfi As DateTimeFormatInfo
If provider Is Nothing Then
'designate a new DateTimeFormatInfo class if Nothing was passed in
mstrDtfi = New DateTimeFormatInfo
Else
'get a reference to the DateTimeFormatInfo class of the FormatProvider that was passed in
mstrDtfi = ci.DateTimeFormat
End If
'check to see if the time is noon and set a new PMDesignator if it is
If dt.TimeOfDay = New TimeSpan(12, 0, 0) Then
mstrDtfi.PMDesignator = "NN"
End If
'check to see if the time is midnight and set a new AMDesignator if it is
If dt.TimeOfDay = New TimeSpan(0, 0, 0) Then
mstrDtfi.AMDesignator = "MM"
End If
'now format the date string with the proper provider
Dim formattedDate As String
If provider Is Nothing Then
formattedDate = dt.ToString(fmt, mstrDtfi)
Else
formattedDate = dt.ToString(fmt, ci)
End If
Return formattedDate
End Function
End Module
答案 0 :(得分:0)
我的目标是提供一种通用的解决方案,其格式可以像ddmm hhmmt或hmtt一样改变,并且重载会处理各种格式,除了&#34; t&#34;的特殊情况。说明符。
一个简单的函数仍然可以处理这个问题。这是一个扩展方法的例子。
Module Extensions
Private rgxTimePeriod As New Regex("t+")
<System.Runtime.CompilerServices.Extension()> _
Public Function LegacyFmt(dt As DateTime, fmt As String, Optional provider As IFormatProvider = Nothing) As String
Dim formatted As String
If dt.TimeOfDay = New TimeSpan(12, 0, 0) Then
fmt = rgxTimePeriod.Replace(fmt, "N")
End If
If provider Is Nothing Then
formatted = dt.ToString(fmt)
Else
formatted = dt.ToString(fmt, provider)
End If
Return formatted
End Function
End Module
?#12:00#.LegacyFmt("hmmt", Globalization.CultureInfo.InvariantCulture)
"1200N"
?now.LegacyFmt("ddmm")
"2551"
?#2:01#.LegacyFmt("hmmt")
?#12:00#.LegacyFmt("hmtt")
"120N"
"201A"
?#2:01#.LegacyFmt("hmtt")
"21AM"