Skip to content

Instantly share code, notes, and snippets.

@metaist
Created December 7, 2012 02:06
Show Gist options
  • Save metaist/4230161 to your computer and use it in GitHub Desktop.
Save metaist/4230161 to your computer and use it in GitHub Desktop.
Convert dates using HebCal's web service.
'Version 0.1.2 // 2012-12-07
'Required References:
' Microsoft Scripting Library
' Microsoft XML
Dim cache As New Dictionary
Dim fso As New FileSystemObject
Public Const LIMIT_CACHE = False 'keep cache small
Public Const REFRESH_CACHE = True 'delete old cache
Public Const MAX_CACHE_SIZE = 1000 'entries
Public Const MAX_CACHE_AGE = 30 'days
Private Const CACHE_FILE = "\cache.txt" 'cache filename
Private Const CACHE_STARTED = "STARTED" 'cache key for age
Public Enum ConvertDirection
Heb2Greg = 1
Greg2Heb = 2
End Enum
Public Sub LoadCache()
path = ThisWorkbook.path & CACHE_FILE
If Not fso.FileExists(path) Then
cache.Add CACHE_STARTED, Format(Now(), "yyyy-mm-dd")
Exit Sub 'no cache file
End If
filenum = FreeFile
Open path For Input As filenum
Do Until EOF(filenum)
Line Input #filenum, buffer
parts = Split(buffer, "|")
cache.Add parts(1), parts(0)
If LIMIT_CACHE And cache.Count >= MAX_CACHE_SIZE Then Exit Do
Loop
Close filenum
If REFRESH_CACHE And cache.Exists(CACHE_STARTED) _
And DateDiff("d", cache(CACHE_STARTED), Now()) >= MAX_CACHE_AGE Then _
cache.RemoveAll
If Not cache.Exists(CACHE_STARTED) Then _
cache.Add CACHE_STARTED, Format(Now(), "yyyy-mm-dd")
End Sub
Public Sub SaveCache()
path = ThisWorkbook.path & CACHE_FILE
If fso.FileExists(path) Then: fso.DeleteFile path
filenum = FreeFile
Open path For Output As filenum
For Each key In cache.Keys
Print #filenum, key & "|" & cache(key)
Next
Close filenum
End Sub
Public Function ConvertDate(givenYear, givenMonth, givenDay, _
Optional direction As ConvertDirection = Heb2Greg)
Dim http As MSXML2.XMLHTTP
Dim xml As MSXML2.DOMDocument
Dim attrs As MSXML2.IXMLDOMNamedNodeMap
Dim search_date As String
Dim result As Variant
givenYear = Format(Trim(givenYear), "00")
givenMonth = Format(Trim(ProperCase(givenMonth)), "00")
givenDay = Format(Trim(givenDay), "00")
If givenMonth = "Adar" Then givenMonth = "Adar1" ' auto-adjust
search_date = givenYear & "-" & givenMonth & "-" & givenDay
If cache.Exists(search_date) Then
result = cache(search_date)
Else
Set http = New MSXML2.XMLHTTP
hebcal_url = "http://www.hebcal.com/converter/?cfg=xml&"
hebcal_tag = ""
Select Case direction
Case Heb2Greg:
hebcal_url = hebcal_url & "hy=" & givenYear & "&hm=" & givenMonth _
& "&hd=" & givenDay & "&h2g=1"
hebcal_tag = "gregorian"
Case Greg2Heb:
hebcal_url = hebcal_url & "gy=" & givenYear & "&gm=" & givenMonth _
& "&gd=" & givenDay & "&g2h=1"
hebcal_tag = "hebrew"
End Select
http.Open "GET", hebcal_url, False
http.Send
Do While http.readyState <> 4: DoEvents: Loop
Set xml = New MSXML2.DOMDocument
xml.LoadXML http.responseText ' Have a response.
Set attrs = xml.getElementsByTagName(hebcal_tag)(0).Attributes
result = attrs.getNamedItem("year").Text & "-" _
& Format(attrs.getNamedItem("month").Text, "00") & "-" _
& Format(attrs.getNamedItem("day").Text, "00")
If direction = Heb2Greg Then result = DateValue(result) 'parse date
If Not LIMIT_CACHE Or cache.Count < MAX_CACHE_SIZE Then _
cache.Add search_date, result
End If
If REFRESH_CACHE And _
DateDiff("d", cache(CACHE_STARTED), Now()) >= MAX_CACHE_AGE Then
cache.RemoveAll
cache.Add CACHE_STARTED, Format(Now(), "yyyy-mm-dd")
End If
ConvertDate = result
End Function
Public Function HebYear(str): HebYear = Split(str, "-")(0)
End Function
Public Function HebMonth(str): HebMonth = Split(str, "-")(1)
End Function
Public Function HebDay(str): HebDay = Split(str, "-")(2)
End Function
Public Function ProperCase(str) As String
ProperCase = UCase(Left(str, 1)) & LCase(Mid(str, 2))
End Function
@metaist
Copy link
Author

metaist commented Dec 7, 2012

Features a cache (that writes to a file with age and limit settings) to make date conversion faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment