Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Efficient standard to military time

For my program, valid standard time formats look like this:

1:59 am
159am
159 AM
1:59 PM

I created the following code to convert the above types of
formats into military times. Does anyone know if this code
can be improved or simplified? I prefer to do time conversions
as quickly as possible. Maybe someone has a better
technique than mine?

Here is the code:
'----------------------------------------------------------
Dim reg As New VBScript_RegExp_55.RegExp
Dim sentence As String
Dim tHours As String
Dim tMins As String
Dim tMeridiem As String
Dim militaryHour As String
Dim match, matches

sentence = "6:59 pm" ' Example input time

reg.Pattern = "^\s*(1[012]|[1-9]):?([0-5][0-9])\s*(am|pm)$"
reg.IgnoreCase = True
reg.Global = True

' Check if format is valid
If reg.Test(sentence) Then
Set matches = reg.Execute(sentence)
Set match = matches(0)

tHours = match.SubMatches(0)
tMins = match.SubMatches(1)
tMeridiem = match.SubMatches(2)

If (tHours = "12") Then
' Handle midnight and noon cases
If (tMeridiem = "am") Then
militaryHour = "00"
Else
militaryHour = "12"
End If
Else
If (tMeridiem = "am") Then
militaryHour = tHours
Else
militaryHour = CStr(Val(tHours) + 12)
End If
If (Val(militaryHour) < 10) Then
militaryHour = "0" + militaryHour
End If
End If
MsgBox "Military time: " + militaryHour + tMins
Else
MsgBox "Invalid format time string"
End If




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Efficient standard to military time

Robert Crandal wrote:

For my program, valid standard time formats look like this:

1:59 am
159am
159 AM
1:59 PM

I created the following code to convert the above types of
formats into military times. Does anyone know if this code
can be improved or simplified? I prefer to do time conversions
as quickly as possible. Maybe someone has a better
technique than mine?

Here is the code:

[snip]

I'm not very good with regex's so I can't comment on that part... but your
code for the time translation can be seriously simplified. VBA can format
times into military time using Format(source$, "HHmm").

Try this:

Dim reg As New VBScript_RegExp_55.RegExp
Dim sentence As String
Dim tResult As String
Dim match, matches

sentence = "6:59 pm" ' Example input time

reg.Pattern = "^\s*(1[012]|[1-9]):?([0-5][0-9])\s*(am|pm)$"
reg.IgnoreCase = True
reg.Global = True

' Check if format is valid
If reg.Test(sentence) Then
Set matches = reg.Execute(sentence)
Set match = matches(0)

tResult = Format$(CDate(match.SubMatches(0) & ":" & _
match.SubMatches(1) & _
match.SubMatches(2)), "HHmm")

MsgBox "Military time: " & tResult
Else
MsgBox "Invalid format time string"
End If

--
Omnia mutantur, nihil interit.
(All things change, but nothing is truly lost.)
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Efficient standard to military time

The slowest aspect to your routine would be creating the RegExp object,
particularly first time though you could maintain it in scope and reuse it.
As written it your code would return an incorrect time with "AM", try
lCase(tMeridiem) = "am"

There are no doubt several other ways, eg

Sub test()
Dim arr, v
arr = Array("1:59 am", "159 AM", _
"159 AM", " 1:59 PM")
For Each v In arr
Debug.Print strDate2MilTime(v)
Next
End Sub

Function Date2MilTime(vTime As Variant) As String
Dim s As String
Dim sNum As String
On Error Resume Next
Date2MilTime = Format(CDate(vTime), "hhmm")
If Err Then
On Error GoTo errExit
s = Replace(vTime, " ", "")
If InStr(2, s, ":") = 0 Then
sNum = Val(s)
s = ":" & Right$(sNum, 2) & Mid$(s, Len(sNum) + 1, 2)
s = Left$(sNum, IIf(Len(sNum) = 3, 1, 2)) & s
End If
Date2MilTime = Format(CDate(s), "hhmm")
End If
Exit Function
errExit:
Date2MilTime = "Error"
End Function

caveat, only tested with the given examples

Regards,
Peter T



"Robert Crandal" wrote in message
...
For my program, valid standard time formats look like this:

1:59 am
159am
159 AM
1:59 PM

I created the following code to convert the above types of
formats into military times. Does anyone know if this code
can be improved or simplified? I prefer to do time conversions
as quickly as possible. Maybe someone has a better
technique than mine?

Here is the code:
'----------------------------------------------------------
Dim reg As New VBScript_RegExp_55.RegExp
Dim sentence As String
Dim tHours As String
Dim tMins As String
Dim tMeridiem As String
Dim militaryHour As String
Dim match, matches

sentence = "6:59 pm" ' Example input time

reg.Pattern = "^\s*(1[012]|[1-9]):?([0-5][0-9])\s*(am|pm)$"
reg.IgnoreCase = True
reg.Global = True

' Check if format is valid
If reg.Test(sentence) Then
Set matches = reg.Execute(sentence)
Set match = matches(0)

tHours = match.SubMatches(0)
tMins = match.SubMatches(1)
tMeridiem = match.SubMatches(2)

If (tHours = "12") Then
' Handle midnight and noon cases
If (tMeridiem = "am") Then
militaryHour = "00"
Else
militaryHour = "12"
End If
Else
If (tMeridiem = "am") Then
militaryHour = tHours
Else
militaryHour = CStr(Val(tHours) + 12)
End If
If (Val(militaryHour) < 10) Then
militaryHour = "0" + militaryHour
End If
End If
MsgBox "Military time: " + militaryHour + tMins
Else
MsgBox "Invalid format time string"
End If






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Efficient standard to military time

"Peter T" wrote:
The slowest aspect to your routine would be creating the RegExp object,
particularly first time though you could maintain it in scope and reuse
it. As written it your code would return an incorrect time with "AM", try
lCase(tMeridiem) = "am"


Wow, I don't know how I missed that "AM" error. I thought because I
set IgnoreCase equal to True that it would have worked for both
"am" and "AM" cases. Guess I was wrong.


There are no doubt several other ways, eg

Sub test()
Dim arr, v
arr = Array("1:59 am", "159 AM", _
"159 AM", " 1:59 PM")
For Each v In arr
Debug.Print strDate2MilTime(v)
Next
End Sub

Function Date2MilTime(vTime As Variant) As String
Dim s As String
Dim sNum As String
On Error Resume Next
Date2MilTime = Format(CDate(vTime), "hhmm")
If Err Then
On Error GoTo errExit
s = Replace(vTime, " ", "")
If InStr(2, s, ":") = 0 Then
sNum = Val(s)
s = ":" & Right$(sNum, 2) & Mid$(s, Len(sNum) + 1, 2)
s = Left$(sNum, IIf(Len(sNum) = 3, 1, 2)) & s
End If
Date2MilTime = Format(CDate(s), "hhmm")
End If
Exit Function
errExit:
Date2MilTime = "Error"
End Function

caveat, only tested with the given examples


Nice alternative solution. I wonder if this will be faster than
the previous solution?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Efficient standard to military time

"Robert Crandal" wrote in message

Nice alternative solution. I wonder if this will be faster than
the previous solution?


I did test before posting (because you said speed was important) and found
my alternative was about twice as fast as the RegExp, and that was
maintaining and reusing the RegExp object. That said if only calling a few
times the difference would be negligible. However when not reusing but
recreating the RegExp object each time it was tens of times faster, as
expected.

Regards,
Peter T




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Efficient standard to military time


"Robert Crandal" wrote in message

As written it your code would return an incorrect time with "AM", try
lCase(tMeridiem) = "am"


I thought because I
set IgnoreCase equal to True that it would have worked for both
"am" and "AM" cases. Guess I was wrong.


IgnoreCase makes matching against the pattern insensitive, however the
SubMatches will return the matched element of the input string in its
original case.

Regards,
Peter T


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Standard Time to Military Time in Excel mtvschultz Excel Discussion (Misc queries) 3 May 5th 23 11:42 AM
Converting standard time to military time C. Excel Worksheet Functions 9 June 1st 17 10:06 PM
formula for converting military time to standard time, etc Pattio Excel Discussion (Misc queries) 8 February 17th 08 01:12 AM
How to I convert standard time to Military or 24 hour format? Nacho Excel Discussion (Misc queries) 5 June 28th 06 07:14 PM
Convert data into standard military time format geog Excel Discussion (Misc queries) 2 December 12th 05 07:46 PM


All times are GMT +1. The time now is 08:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"