正确计算天干地支生肖的函数(.net)---更正版

因为要用到一个 算干支的函数
但在网上找了好长时间,没有发现算的正确的(按春节划分如20070217丙戌-20070218则为丁亥)
所以就自写了一个,夜深了,有的地方就草草写了,有错请回复提出

注:
用的XML数据库,有兴趣的朋友可以将之转为其它数据库
如果大家需要的话我可以把文件打个包。
 -----邹健
====================================================
后注:愿来的函数没做时间合法性判断,又重新修正更改了


因为我用的是ASP.net所以要调用的XML文件名直接写在了web.config的appSettings
代码如下
目录结构
/App_code/strcl.vb本类库文件
|
/xml/AnimalSign.xml    十二生肖及地支文件
|        Date.xml                  1900-2100春节时间
|          SkyTree.xml          天干列表
|
/web.config 网站配置文件

本文将按以下顺序给出代码
datecl.vb
web.config
Date.xml
AnimalSign.xml
SkyTree.xml

=====================================datecl.vb=================================
Imports  Microsoft.VisualBasic
Public   Class  Datecl
    
Public   Enum  SkyTreeText
        天干 
=   0
        地支 
=   1
    
End Enum
    
Shared   Function  ftime( ByRef  d  As  DateTime)  As   String
        
If   DateDiff (DateInterval.Day, d, Now())  >=   1   Then
            
Return   Format (d,  " MM-dd " )
        
Else
            
Return   Format (d,  " HH:mm " )
        
End   If
    
End Function
    
Overloads   Shared   Function  SkyTree( ByRef  dat  As  DateTime,  ByRef  i  As  SkyTreeText)  As   Byte
        
' 得到干支序号
         Dim  Dom  As   New  System.Xml.XmlDocument
        Dom.Load(My.Request.MapPath(
" / " &  ConfigurationManager.AppSettings( " AnimalSignDate " ))
        
Dim  Node  As  System.Xml.XmlNode
        
Dim  y  As  Int16  =  Int16.Parse( Year (dat))
        
Dim  m  As   Byte   =   Byte .Parse( Month (dat))
        
Dim  d  As   Byte   =   Byte .Parse( Day (dat))
        Node 
=  Dom.SelectSingleNode( " //item[@year= "   &  y  &   " ] " )
        
Dim  start  As  Int16
        
Dim  n  As   Byte
        
Dim  result  As   Byte
        
If  i  =   1   Then
            start 
=   1901
            n 
=   12
        
Else
            start 
=   1905
            n 
=   10
        
End   If
        result 
=  ((y  -  start)  Mod  n)  +   2
        result 
=   IIf (result  <=   0 , result  +  n, result)
        
If   Byte .Parse(Node.Attributes( " month " ).InnerXml)  >  m  Then  result  =  result  -   1
        
If   Byte .Parse(Node.Attributes( " month " ).InnerXml)  =  m  And   Byte .Parse(Node.Attributes( " day " ).InnerXml)  >  d  Then  result  =  result  -   1
        result 
=   IIf (result  =   0 , n, result)
        result 
=   IIf (result  =  n  +   1 1 , result)
        
Return  result
        
' 本最多用时16MS一般1MS内可以完成
     End Function
    
Overloads   Shared   Function  SkyTree( ByRef  dats  As   String ByRef  i  As  SkyTreeText)  As   Byte   ' 得到干支序号
         If   Not   IsDate (dats)  Then   Return   0
        
Return  SkyTree(DateTime.Parse(dats), i)
    
End Function
    
Shared   Function  XmlItemName( ByRef  Xpath  As   String ByRef  name  As   String ByRef  xmlfilename  As   String As   String   ' 通用读取XML
         Dim  Dom  As   New  System.Xml.XmlDocument
        Dom.Load(My.Request.MapPath(
" / " &  ConfigurationManager.AppSettings(xmlfilename))
        
Dim  Node  As  System.Xml.XmlNode
        Node 
=  Dom.SelectSingleNode(Xpath)
        
Return  Node.Attributes(name).InnerXml
    
End Function
    
Overloads   Shared   Function  AnimalSignName( ByRef  Num  As   Byte As   String   ' 返回生肖名
         Return  XmlItemName( " //item[@id= "   &  Num.ToString  &   " ] " " name " " AnimalSign " )
    
End Function
    
Overloads   Shared   Function  AnimalSignName( ByRef  dat  As  DateTime)  As   String   ' 返回生肖名
         Return  AnimalSignName(SkyTree(dat, SkyTreeText.地支))
    
End Function
    
Overloads   Shared   Function  AnimalSignName( ByRef  dats  As   String As   String   ' 返回生肖名
         If   Not   IsDate (dats)  Then   Return   ""
        
Return  AnimalSignName(SkyTree(DateTime.Parse(dats), SkyTreeText.地支))
    
End Function

    
Overloads   Shared   Function  TreeName( ByRef  dat  As  DateTime)  As   String   ' 返回为地支名
         Return  XmlItemName( " //item[@id= "   &  SkyTree(dat, SkyTreeText.地支)  &   " ] " " tree " " AnimalSign " )
    
End Function
    
Overloads   Shared   Function  TreeName( ByRef  dats  As   String As   String   ' 返回为地支名
         If   Not   IsDate (dats)  Then   Return   ""
        
Return  TreeName(DateTime.Parse(dats))
    
End Function
    
Overloads   Shared   Function  SkyName( ByRef  dat  As  DateTime)  As   String   ' 返回天干名
         Return  XmlItemName( " //item[@id= "   &  SkyTree(dat, SkyTreeText.天干).ToString  &   " ] " " tree " " SkyTree " )
    
End Function
    
Overloads   Shared   Function  SkyName( ByRef  dats  As   String As   String   ' 返回天干名
         If   Not   IsDate (dats)  Then   Return   ""
        
Return  SkyName(DateTime.Parse(dats))
    
End Function
    
Overloads   Shared   Function  SkyTreeName( ByRef  dat  As  DateTime)  As   String   ' 返回天干地支
         Return  SkyName(dat)  &  TreeName(dat)
    
End Function
    
Overloads   Shared   Function  SkyTreeName( ByRef  dats  As   String As   String   ' 返回天干地支
         Return  SkyName(dats)  &  TreeName(dats)
    
End Function
    
Overloads   Shared   Function  StarSignName( ByRef  dats  As   String As   String
        
Return  StarSignName(StarSign(dats))
    
End Function
    
Overloads   Shared   Function  StarSignName( ByRef  dat  As  DateTime)  As   String
        
Return  StarSignName(StarSign(dat))
    
End Function
    
Overloads   Shared   Function  StarSignName( ByRef  Num  As   Byte As   String
        
Return  XmlItemName( " /root/item[@id= "   &  Num.ToString  &   " ] " " name " " StarSign " )
    
End Function
    
Shared   Function  StarSign( ByRef  dats  As   String As   Byte
        
If   Not   IsDate (dats)  Then   Return   0
        
Return  StarSign(DateTime.Parse(dats))
    
End Function
    
Shared   Function  StarSign( ByRef  dat  As  DateTime)  As   Byte
        
If   Not   IsDate (dat)  Then   Return   0
        
Dim  m  As   Byte   =   Byte .Parse( Month (dat))
        
Dim  d  As   Byte   =   Byte .Parse( Day (dat))
        
Dim  y  As  Int16  =   2007
        
Dim  temp  As  DateTime  =  DateTime.Parse(m  &   " / "   &  d  &   " /2007 " )
        
Select   Case  temp
            
Case  # 3 / 21 / 2007 To  # 4 / 19 / 2007 #
                StarSign 
=   1
            
Case  # 4 / 20 / 2007 To  # 5 / 20 / 2007 #
                StarSign 
=   2
            
Case  # 5 / 21 / 2007 To  # 6 / 21 / 2007 #
                StarSign 
=   3
            
Case  # 6 / 22 / 2007 To  # 7 / 22 / 2007 #
                StarSign 
=   4
            
Case  # 7 / 23 / 2007 To  # 8 / 22 / 2007 #
                StarSign 
=   5
            
Case  # 8 / 23 / 2007 To  # 9 / 22 / 2007 #
                StarSign 
=   6
            
Case  # 9 / 23 / 2007 To  # 10 / 23 / 2007 #
                StarSign 
=   7
            
Case  # 10 / 24 / 2007 To  # 11 / 21 / 2007 #
                StarSign 
=   8
            
Case  # 11 / 22 / 2007 To  # 12 / 21 / 2007 #
                StarSign 
=   9
            
Case  # 12 / 22 / 2007 To  # 12 / 31 / 2007 #
                StarSign 
=   10
            
Case  # 1 / 1 / 2007 To  # 1 / 19 / 2007 #
                StarSign 
=   10
            
Case  # 1 / 20 / 2007 To  # 2 / 18 / 2007 #
                StarSign 
=   11
            
Case  # 2 / 19 / 2007 To  # 3 / 20 / 2007 #
                StarSign 
=   12
            
Case   Else
                StarSign 
=   0
        
End   Select
        
Return  StarSign
    
End Function
End Class

==============web.config中加以下结点
< appSettings >
        
< add  key ="AnimalSignDate"  value ="/Xml/Date.xml" />
        
< add  key ="AnimalSign"  value ="/Xml/AnimalSign.xml" />
        
< add  key ="StarSign"  value ="/Xml/StarSign.xml" />
                
< add  key ="SkyTree"  value ="/Xml/SkyTree.xml" />
</ appSettings >

==============Date.xml

<? xml version="1.0" encoding="UTF-8" ?>
< root >
< item  month ="1"  day ="31"  year ="1900"   />
< item  month ="2"  day ="19"  year ="1901"   />
< item  month ="2"  day ="8"  year ="1902"   />
< item  month ="1"  day ="29"  year ="1903"   />
< item  month ="2"  day ="16"  year ="1904"   />
< item  month ="2"  day ="4"  year ="1905"   />
< item  month ="1"  day ="25"  year ="1906"   />
< item  month ="2"  day ="13"  year ="1907"   />
< item  month ="2"  day ="2"  year ="1908"   />
< item  month ="1"  day ="22"  year ="1909"   />
< item  month ="2"  day ="10"  year ="1910"   />
< item  month ="1"  day ="30"  year ="1911"   />
< item  month ="2"  day ="18"  year ="1912"   />
< item  month ="2"  day ="6"  year ="1913"   />
< item  month ="1"  day ="26"  year ="1914"   />
< item  month ="2"  day ="14"  year ="1915"   />
< item  month ="2"  day ="4"  year ="1916"   />
< item  month ="1"  day ="23"  year ="1917"   />
< item  month ="2"  day ="11"  year ="1918"   />
< item  month ="2"  day ="1"  year ="1919"   />
< item  month ="2"  day ="20"  year ="1920"   />
< item  month ="2"  day ="8"  year ="1921"   />
< item  month ="1"  day ="28"  year ="1922"   />
< item  month ="2"  day ="16"  year ="1923"   />
< item  month ="2"  day ="5"  year ="1924"   />
< item  month ="1"  day ="24"  year ="1925"   />
< item  month ="2"  day ="13"  year ="1926"   />
< item  month ="2"  day ="2"  year ="1927"   />
< item  month ="1"  day ="23"  year ="1928"   />
< item  month ="2"  day ="10"  year ="1929"   />
< item  month ="1"  day ="30"  year ="1930"   />
< item  month ="2"  day ="17"  year ="1931"   />
< item  month ="2"  day ="6"  year ="1932"   />
< item  month ="1"  day ="26"  year ="1933"   />
< item  month ="2"  day ="14"  year ="1934"   />
< item  month ="2"  day ="4"  year ="1935"   />
< item  month ="1"  day ="24"  year ="1936"   />
< item  month ="2"  day ="11"  year ="1937"   />
< item  month ="1"  day ="31"  year ="1938"   />
< item  month ="2"  day ="19"  year ="1939"   />
< item  month ="2"  day ="8"  year ="1940"   />
< item  month ="1"  day ="27"  year ="1941"   />
< item  month ="2"  day ="15"  year ="1942"   />
< item  month ="2"  day ="5"  year ="1943"   />
< item  month ="1"  day ="25"  year ="1944"   />
< item  month ="2"  day ="13"  year ="1945"   />
< item  month ="2"  day ="2"  year ="1946"   />
< item  month ="1"  day ="22"  year ="1947"   />
< item  month ="2"  day ="10"  year ="1948"   />
< item  month ="1"  day ="29"  year ="1949"   />
< item  month ="2"  day ="17"  year ="1950"   />
< item  month ="2"  day ="6"  year ="1951"   />
< item  month ="1"  day ="27"  year ="1952"   />
< item  month ="2"  day ="14"  year ="1953"   />
< item  month ="2"  day ="3"  year ="1954"   />
< item  month ="1"  day ="24"  year ="1955"   />
< item  month ="2"  day ="12"  year ="1956"   />
< item  month ="1"  day ="31"  year ="1957"   />
< item  month ="2"  day ="18"  year ="1958"   />
< item  month ="2"  day ="8"  year ="1959"   />
< item  month ="1"  day ="28"  year ="1960"   />
< item  month ="2"  day ="15"  year ="1961"   />
< item  month ="2"  day ="5"  year ="1962"   />
< item  month ="1"  day ="25"  year ="1963"   />
< item  month ="2"  day ="13"  year ="1964"   />
< item  month ="2"  day ="2"  year ="1965"   />
< item  month ="1"  day ="21"  year ="1966"   />
< item  month ="2"  day ="9"  year ="1967"   />
< item  month ="1"  day ="30"  year ="1968"   />
< item  month ="2"  day ="17"  year ="1969"   />
< item  month ="2"  day ="6"  year ="1970"   />
< item  month ="1"  day ="27"  year ="1971"   />
< item  month ="2"  day ="15"  year ="1972"   />
< item  month ="2"  day ="3"  year ="1973"   />
< item  month ="1"  day ="23"  year ="1974"   />
< item  month ="2"  day ="11"  year ="1975"   />
< item  month ="1"  day ="31"  year ="1976"   />
< item  month ="2"  day ="18"  year ="1977"   />
< item  month ="2"  day ="7"  year ="1978"   />
< item  month ="1"  day ="28"  year ="1979"   />
< item  month ="2"  day ="16"  year ="1980"   />
< item  month ="2"  day ="5"  year ="1981"   />
< item  month ="1"  day ="25"  year ="1982"   />
< item  month ="2"  day ="13"  year ="1983"   />
< item  month ="2"  day ="2"  year ="1984"   />
< item  month ="2"  day ="20"  year ="1985"   />
< item  month ="2"  day ="9"  year ="1986"   />
< item  month ="1"  day ="29"  year ="1987"   />
< item  month ="2"  day ="17"  year ="1988"   />
< item  month ="2"  day ="6"  year ="1989"   />
< item  month ="1"  day ="27"  year ="1990"   />
< item  month ="2"  day ="15"  year ="1991"   />
< item  month ="2"  day ="4"  year ="1992"   />
< item  month ="1"  day ="23"  year ="1993"   />
< item  month ="2"  day ="10"  year ="1994"   />
< item  month ="1"  day ="31"  year ="1995"   />
< item  month ="2"  day ="19"  year ="1996"   />
< item  month ="2"  day ="7"  year ="1997"   />
< item  month ="1"  day ="28"  year ="1998"   />
< item  month ="2"  day ="16"  year ="1999"   />
< item  month ="2"  day ="5"  year ="2000"   />
< item  month ="1"  day ="24"  year ="2001"   />
< item  month ="2"  day ="12"  year ="2002"   />
< item  month ="2"  day ="1"  year ="2003"   />
< item  month ="1"  day ="22"  year ="2004"   />
< item  month ="2"  day ="9"  year ="2005"   />
< item  month ="1"  day ="29"  year ="2006"   />
< item  month ="2"  day ="18"  year ="2007"   />
< item  month ="2"  day ="7"  year ="2008"   />
< item  month ="1"  day ="26"  year ="2009"   />
< item  month ="2"  day ="14"  year ="2010"   />
< item  month ="2"  day ="3"  year ="2011"   />
< item  month ="1"  day ="23"  year ="2012"   />
< item  month ="2"  day ="10"  year ="2013"   />
< item  month ="1"  day ="31"  year ="2014"   />
< item  month ="2"  day ="19"  year ="2015"   />
< item  month ="2"  day ="8"  year ="2016"   />
< item  month ="1"  day ="28"  year ="2017"   />
< item  month ="2"  day ="16"  year ="2018"   />
< item  month ="2"  day ="5"  year ="2019"   />
< item  month ="1"  day ="25"  year ="2020"   />
< item  month ="2"  day ="12"  year ="2021"   />
< item  month ="2"  day ="1"  year ="2022"   />
< item  month ="1"  day ="22"  year ="2023"   />
< item  month ="2"  day ="10"  year ="2024"   />
< item  month ="1"  day ="29"  year ="2025"   />
< item  month ="2"  day ="17"  year ="2026"   />
< item  month ="2"  day ="6"  year ="2027"   />
< item  month ="1"  day ="26"  year ="2028"   />
< item  month ="2"  day ="13"  year ="2029"   />
< item  month ="2"  day ="3"  year ="2030"   />
< item  month ="1"  day ="23"  year ="2031"   />
< item  month ="2"  day ="11"  year ="2032"   />
< item  month ="1"  day ="31"  year ="2033"   />
< item  month ="2"  day ="19"  year ="2034"   />
< item  month ="2"  day ="8"  year ="2035"   />
< item  month ="1"  day ="28"  year ="2036"   />
< item  month ="2"  day ="15"  year ="2037"   />
< item  month ="2"  day ="4"  year ="2038"   />
< item  month ="1"  day ="24"  year ="2039"   />
< item  month ="2"  day ="12"  year ="2040"   />
< item  month ="2"  day ="1"  year ="2041"   />
< item  month ="1"  day ="22"  year ="2042"   />
< item  month ="2"  day ="10"  year ="2043"   />
< item  month ="1"  day ="30"  year ="2044"   />
< item  month ="2"  day ="17"  year ="2045"   />
< item  month ="2"  day ="6"  year ="2046"   />
< item  month ="1"  day ="26"  year ="2047"   />
< item  month ="2"  day ="14"  year ="2048"   />
< item  month ="2"  day ="2"  year ="2049"   />
< item  month ="1"  day ="23"  year ="2050"   />
< item  month ="2"  day ="11"  year ="2051"   />
< item  month ="2"  day ="1"  year ="2052"   />
< item  month ="2"  day ="19"  year ="2053"   />
< item  month ="2"  day ="8"  year ="2054"   />
< item  month ="1"  day ="28"  year ="2055"   />
< item  month ="2"  day ="15"  year ="2056"   />
< item  month ="2"  day ="4"  year ="2057"   />
< item  month ="1"  day ="24"  year ="2058"   />
< item  month ="2"  day ="12"  year ="2059"   />
< item  month ="2"  day ="2"  year ="2060"   />
< item  month ="1"  day ="21"  year ="2061"   />
< item  month ="2"  day ="9"  year ="2062"   />
< item  month ="1"  day ="29"  year ="2063"   />
< item  month ="2"  day ="17"  year ="2064"   />
< item  month ="2"  day ="5"  year ="2065"   />
< item  month ="1"  day ="26"  year ="2066"   />
< item  month ="2"  day ="14"  year ="2067"   />
< item  month ="2"  day ="3"  year ="2068"   />
< item  month ="1"  day ="23"  year ="2069"   />
< item  month ="2"  day ="11"  year ="2070"   />
< item  month ="1"  day ="31"  year ="2071"   />
< item  month ="2"  day ="19"  year ="2072"   />
< item  month ="2"  day ="7"  year ="2073"   />
< item  month ="1"  day ="27"  year ="2074"   />
< item  month ="2"  day ="15"  year ="2075"   />
< item  month ="2"  day ="5"  year ="2076"   />
< item  month ="1"  day ="24"  year ="2077"   />
< item  month ="2"  day ="12"  year ="2078"   />
< item  month ="2"  day ="2"  year ="2079"   />
< item  month ="1"  day ="22"  year ="2080"   />
< item  month ="2"  day ="9"  year ="2081"   />
< item  month ="1"  day ="29"  year ="2082"   />
< item  month ="2"  day ="17"  year ="2083"   />
< item  month ="2"  day ="6"  year ="2084"   />
< item  month ="1"  day ="26"  year ="2085"   />
< item  month ="2"  day ="14"  year ="2086"   />
< item  month ="2"  day ="3"  year ="2087"   />
< item  month ="1"  day ="24"  year ="2088"   />
< item  month ="2"  day ="10"  year ="2089"   />
< item  month ="1"  day ="30"  year ="2090"   />
< item  month ="2"  day ="18"  year ="2091"   />
< item  month ="2"  day ="7"  year ="2092"   />
< item  month ="1"  day ="27"  year ="2093"   />
< item  month ="2"  day ="15"  year ="2094"   />
< item  month ="2"  day ="5"  year ="2095"   />
< item  month ="1"  day ="25"  year ="2096"   />
< item  month ="2"  day ="12"  year ="2097"   />
< item  month ="2"  day ="1"  year ="2098"   />
< item  month ="1"  day ="21"  year ="2099"   />
< item  month ="2"  day ="9"  year ="2100"   />
</ root >


===============SkyTree.xml
<? xml version="1.0" encoding="UTF-8" ?>
< root >
< item  id ="1"  tree ="甲"   />
< item  id ="2"  tree ="乙"   />
< item  id ="3"  tree ="丙"   />
< item  id ="4"  tree ="丁"   />
< item  id ="5"  tree ="戊"   />
< item  id ="6"  tree ="己"   />
< item  id ="7"  tree ="庚"   />
< item  id ="8"  tree ="辛"   />
< item  id ="9"  tree ="壬"   />
< item  id ="10"  tree ="癸"   />
</ root >



=================AnimalSign.xml
<? xml version="1.0" encoding="UTF-8" ?>
< root >
< item  id ="1"  name ="鼠"  tree ="子"   />
< item  id ="2"  name ="牛"  tree ="丑"   />
< item  id ="3"  name ="虎"  tree ="寅"   />
< item  id ="4"  name ="兔"  tree ="卯"   />
< item  id ="5"  name ="龙"  tree ="辰"   />
< item  id ="6"  name ="蛇"  tree ="巳"   />
< item  id ="7"  name ="马"  tree ="午"   />
< item  id ="8"  name ="羊"  tree ="未"   />
< item  id ="9"  name ="猴"  tree ="申"   />
< item  id ="10"  name ="鸡"  tree ="酉"   />
< item  id ="11"  name ="狗"  tree ="戌"   />
< item  id ="12"  name ="猪"  tree ="亥"   />
</ root >

===========================StarSign.xml
<? xml version="1.0" encoding="UTF-8" ?>
< root >
< item  id ="0"  name =""   />
< item  id ="1"  name ="白羊"   />
< item  id ="2"  name ="金牛"   />
< item  id ="3"  name ="双子"   />
< item  id ="4"  name ="巨蟹"   />
< item  id ="5"  name ="狮子"   />
< item  id ="6"  name ="处女"   />
< item  id ="7"  name ="天秤"   />
< item  id ="8"  name ="天蝎"   />
< item  id ="9"  name ="射手"   />
< item  id ="10"  name ="摩羯"   />
< item  id ="11"  name ="水瓶"   />
< item  id ="12"  name ="双鱼"   />
</ root >

原创作品欢迎转载,请保留作者信息:邹健20070106深夜

你可能感兴趣的:(.net,String,function,tree,byte,encoding)