/beta/include_farsiDateHandling.asp

http://github.com/khaneh/Orders · ASP · 263 lines · 227 code · 30 blank · 6 comment · 7 complexity · 708d800940007eef4aa429a7c67ee16d MD5 · raw file

  1. <%
  2. Function persian_jdn(iYear, _
  3. iMonth, _
  4. iDay) 'As Long
  5. Const PERSIAN_EPOCH = 1948321 ' The JDN of 1 Farvardin 1
  6. Dim epbase
  7. Dim epyear
  8. Dim mdays
  9. If iYear >= 0 Then
  10. epbase = iYear - 474
  11. Else
  12. epbase = iYear - 473
  13. End If
  14. epyear = 474 + (epbase Mod 2820)
  15. If iMonth <= 7 Then
  16. mdays = (CLng(iMonth) - 1) * 31
  17. Else
  18. mdays = (CLng(iMonth) - 1) * 30 + 6
  19. End If
  20. persian_jdn = CLng(iDay) _
  21. + mdays _
  22. + Fix(((epyear * 682) - 110) / 2816) _
  23. + (epyear - 1) * 365 _
  24. + Fix(epbase / 2820) * 1029983 _
  25. + (PERSIAN_EPOCH - 1)
  26. End Function
  27. Sub jdn_persian(jdn, _
  28. ByRef iYear, _
  29. ByRef iMonth, _
  30. ByRef iDay)
  31. Dim depoch
  32. Dim cycle
  33. Dim cyear
  34. Dim ycycle
  35. Dim aux1, aux2
  36. Dim yday
  37. depoch = jdn - persian_jdn(475, 1, 1)
  38. cycle = Fix(depoch / 1029983)
  39. cyear = depoch Mod 1029983
  40. If cyear = 1029982 Then
  41. ycycle = 2820
  42. Else
  43. aux1 = Fix(cyear / 366)
  44. aux2 = cyear Mod 366
  45. ycycle = Int(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1
  46. End If
  47. iYear = ycycle + (2820 * cycle) + 474
  48. If iYear <= 0 Then
  49. iYear = iYear - 1
  50. End If
  51. yday = (jdn - persian_jdn(iYear, 1, 1)) + 1
  52. If yday <= 186 Then
  53. iMonth = Ceil(yday / 31)
  54. Else
  55. iMonth = Ceil((yday - 6) / 30)
  56. End If
  57. iDay = (jdn - persian_jdn(iYear, iMonth, 1)) + 1
  58. End Sub
  59. ' We needed an alternative to Int and Fix.
  60. ' Int(8.4) = 8, Int(-8.4) = -9
  61. ' Fix(8.4) = 8, Fix(-8.4) = -8
  62. ' Ceil(8.4) = 9, Ceil(-8.4) = -9
  63. Function Ceil(number)
  64. Ceil = -Sgn(number) * Int(-Abs(number))
  65. ' or
  66. 'Ceil = CInt(number + (Sgn(number) * 0.5))
  67. End Function
  68. Const Gregorian = 1
  69. Const Julian = 2
  70. Function civil_jdn(ByVal iYear, _
  71. ByVal iMonth, _
  72. ByVal iDay ) 'As Long
  73. calendarType = Gregorian
  74. Dim lYear
  75. Dim lMonth
  76. Dim lDay
  77. If calendarType = Gregorian And ((iYear > 1582) Or _
  78. ((iYear = 1582) And (iMonth > 10)) Or _
  79. ((iYear = 1582) And (iMonth = 10) And (iDay > 14))) _
  80. Then
  81. lYear = CLng(iYear)
  82. lMonth = CLng(iMonth)
  83. lDay = CLng(iDay)
  84. civil_jdn = ((1461 * (lYear + 4800 + ((lMonth - 14) \ 12))) \ 4) _
  85. + ((367 * (lMonth - 2 - 12 * (((lMonth - 14) \ 12)))) \ 12) _
  86. - ((3 * (((lYear + 4900 + ((lMonth - 14) \ 12)) \ 100))) \ 4) _
  87. + lDay - 32075
  88. Else
  89. civil_jdn = 0 ' julian_jdn(iYear, iMonth, iDay)
  90. End If
  91. End Function
  92. Sub jdn_civil(ByVal jdn, _
  93. ByRef iyear, _
  94. ByRef imonth, _
  95. ByRef iday)
  96. Dim l
  97. Dim k
  98. Dim n
  99. Dim i
  100. Dim j
  101. If (jdn > 2299160) Then
  102. l = jdn + 68569
  103. n = ((4 * l) \ 146097)
  104. l = l - ((146097 * n + 3) \ 4)
  105. i = ((4000 * (l + 1)) \ 1461001)
  106. l = l - ((1461 * i) \ 4) + 31
  107. j = ((80 * l) \ 2447)
  108. iday = l - ((2447 * j) \ 80)
  109. l = (j \ 11)
  110. imonth = j + 2 - 12 * l
  111. iyear = 100 * (n - 49) + i + l
  112. Else
  113. Call jdn_julian(jdn, iyear, imonth, iday)
  114. End If
  115. End Sub
  116. Function shamsiToday()
  117. Dim depoch
  118. Dim cycle
  119. Dim cyear
  120. Dim ycycle
  121. Dim aux1, aux2
  122. Dim yday
  123. Dim jdn
  124. tmpDate=date()
  125. jdn=civil_jdn(clng(year(tmpDate)),clng(Month(tmpDate)),clng(Day(tmpDate)))
  126. depoch = jdn - 2121446 ' 2121446 = persian_jdn(475, 1, 1)
  127. cycle = Fix(depoch / 1029983)
  128. cyear = depoch Mod 1029983
  129. If cyear = 1029982 Then
  130. ycycle = 2820
  131. Else
  132. aux1 = Fix(cyear / 366)
  133. aux2 = cyear Mod 366
  134. ycycle = Int(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1
  135. End If
  136. iYear = ycycle + (2820 * cycle) + 474
  137. If iYear <= 0 Then
  138. iYear = iYear - 1
  139. End If
  140. yday = (jdn - persian_jdn(iYear, 1, 1)) + 1
  141. If yday <= 186 Then
  142. iMonth = Ceil(yday / 31)
  143. Else
  144. iMonth = Ceil((yday - 6) / 30)
  145. End If
  146. iDay = (jdn - persian_jdn(iYear, iMonth, 1)) + 1
  147. if iDay < 10 then iDay = "0" & iDay
  148. if iMonth < 10 then iMonth = "0" & iMonth
  149. shamsiToday=iYear & "/" & iMonth & "/" & iDay
  150. End Function
  151. Function shamsiDate(inputDate)
  152. Dim depoch
  153. Dim cycle
  154. Dim cyear
  155. Dim ycycle
  156. Dim aux1, aux2
  157. Dim yday
  158. Dim jdn
  159. jdn=civil_jdn(clng(year(inputDate)),clng(Month(inputDate)),clng(Day(inputDate)))
  160. depoch = jdn - 2121446 ' 2121446 = persian_jdn(475, 1, 1)
  161. cycle = Fix(depoch / 1029983)
  162. cyear = depoch Mod 1029983
  163. If cyear = 1029982 Then
  164. ycycle = 2820
  165. Else
  166. aux1 = Fix(cyear / 366)
  167. aux2 = cyear Mod 366
  168. ycycle = Int(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1
  169. End If
  170. iYear = ycycle + (2820 * cycle) + 474
  171. If iYear <= 0 Then
  172. iYear = iYear - 1
  173. End If
  174. yday = (jdn - persian_jdn(iYear, 1, 1)) + 1
  175. If yday <= 186 Then
  176. iMonth = Ceil(yday / 31)
  177. Else
  178. iMonth = Ceil((yday - 6) / 30)
  179. End If
  180. iDay = (jdn - persian_jdn(iYear, iMonth, 1)) + 1
  181. if iDay < 10 then iDay = "0" & iDay
  182. if iMonth < 10 then iMonth = "0" & iMonth
  183. shamsiDate=iYear & "/" & iMonth & "/" & iDay
  184. End Function
  185. Function CheckDateFormat(ByVal inputDate)
  186. CheckDateFormat=False
  187. Set RExp = New RegExp
  188. RExp.Pattern = "^13\d\d/(0[1-6]/(0[1-9]|[12][0-9]|3[01])|(0[789]|1[012])/(0[1-9]|[12][0-9]|30))$"
  189. RExp.IgnoreCase = False
  190. RExp.Global = False
  191. If RExp.Test(inputDate) Then
  192. CheckDateFormat=True
  193. end if
  194. Set RExp = nothing
  195. End Function
  196. Function currentTime10()
  197. curTime= now
  198. curHour = Hour(curTime)
  199. curMinute = Minute(curTime)
  200. curSecond = Second(curTime)
  201. if (len(curHour) = 1) then curHour = "0" & curHour
  202. if (len(curMinute) = 1) then curMinute = "0" & curMinute
  203. if (len(curSecond) = 1) then curSecond = "0" & curSecond
  204. currentTime10= curHour & ":" & curMinute & ":" & curSecond
  205. End Function
  206. function weekdaynameFA(name)
  207. select case name
  208. case "Saturday":
  209. weekdaynameFA=""
  210. case "Sunday":
  211. weekdaynameFA="ߝ"
  212. case "Monday":
  213. weekdaynameFA=""
  214. case "Tuesday":
  215. weekdaynameFA=""
  216. case "Wednesday":
  217. weekdaynameFA=""
  218. case "Thursday":
  219. weekdaynameFA="̝"
  220. case "Friday":
  221. weekdaynameFA=""
  222. case else
  223. weekdaynameFA=name
  224. end select
  225. end function
  226. function splitDate(myDate)
  227. dim out(3)
  228. out(0)=mid(myDate,1,4)
  229. out(1)=mid(myDate,6,2)
  230. out(2)=mid(myDate,9,2)
  231. splitDate=out
  232. end function
  233. %>