Jump to content

Date manipulation: Difference between revisions

Added FreeBASIC
(Change name of result.)
(Added FreeBASIC)
Line 748:
2009-03-08T12:30:00Z London
</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
#include "vbcompat.bi"
 
Sub split (s As String, sepList As String, result() As String, removeEmpty As Boolean = False)
If s = "" OrElse sepList = "" Then
Redim result(0)
result(0) = s
Return
End If
Dim As Integer i, j, count = 0, empty = 0, length
Dim As Integer position(Len(s) + 1)
position(0) = 0
For i = 0 To len(s) - 1
For j = 0 to Len(sepList) - 1
If s[i] = sepList[j] Then
count += 1
position(count) = i + 1
End If
Next j
Next i
 
Redim result(count)
If count = 0 Then
result(0) = s
Return
End If
 
position(count + 1) = len(s) + 1
For i = 1 To count + 1
length = position(i) - position(i - 1) - 1
result(i - 1 - empty) = Mid(s, position(i - 1) + 1, length)
If removeEmpty Andalso CBool(length = 0) Then empty += 1
Next
 
If empty > 0 Then Redim Preserve result(count - empty)
End Sub
 
Function parseDate(dt As String, zone As String) As Double
Dim result() As String
split dt, " ", result(), True
Dim As Long m, d, y, h, mn
Dim am As Boolean
Dim index As Integer
Select Case Lcase(result(0))
Case "january" : m = 1
Case "february" : m = 2
Case "march" : m = 3
Case "april" : m = 4
Case "may" : m = 5
Case "june" : m = 6
Case "july" : m = 7
Case "august" : m = 8
Case "september" : m = 9
Case "october" : m = 10
Case "november" : m = 11
Case "december" : m = 12
End Select
d = ValInt(result(1))
y = ValInt(result(2))
result(3) = LCase(result(3))
am = (Right(result(3), 2) = "am")
index = Instr(result(3), ":")
h = ValInt(Left(result(3), index - 1))
If Not am Then
h = h + 12
If h = 24 Then h = 12
End If
mn = ValInt(Mid(result(3), index + 1, 2))
zone = result(4)
Return DateSerial(y, m, d) + TimeSerial(h, mn, 0)
End Function
 
Dim zone As String
Dim ds As Double = parseDate("March 7 2009 7:30pm EST", zone)
Print "Original Date/Time : "; Format(ds, "mmmm d yyyy h:nnam/pm ") + " " + zone
ds = DateAdd("h", 12, ds)
Print "12 hours later : "; Format(ds, "mmmm d yyyy h:nnam/pm ") + " " + zone
' add 5 hours to convert EST to UTC
ds = DateAdd("h", 5, ds)
Print "Equiv to Date/Time : "; Format(ds, "mmmm d yyyy h:nnam/pm ") + " UTC"
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
Original Date/Time : March 7 2009 7:30pm EST
12 hours later : March 8 2009 7:30am EST
Equiv to Date/Time : March 8 2009 12:30pm UTC
</pre>
 
=={{header|Frink}}==
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.