MySQL:根据阳历获取农历日期函数

drop table if exists dim_solar_data;
create table dim_solar_data (
	yearid int not null PRIMARY KEY,
	data char(7) not null,
	dataint int not null
) ;

-- 插入数据
insert into dim_solar_data
select 1900,'0x04bd8',19416 union all select 1901,'0x04ae0',19168
union all select 1902,'0x0a570',42352 union all select 1903,'0x054d5',21717
union all select 1904,'0x0d260',53856 union all select 1905,'0x0d950',55632
union all select 1906,'0x16554',91476 union all select 1907,'0x056a0',22176
union all select 1908,'0x09ad0',39632 union all select 1909,'0x055d2',21970
union all select 1910,'0x04ae0',19168 union all select 1911,'0x0a5b6',42422
union all select 1912,'0x0a4d0',42192 union all select 1913,'0x0d250',53840
union all select 1914,'0x1d255',119381 union all select 1915,'0x0b540',46400
union all select 1916,'0x0d6a0',54944 union all select 1917,'0x0ada2',44450
union all select 1918,'0x095b0',38320 union all select 1919,'0x14977',84343
union all select 1920,'0x04970',18800 union all select 1921,'0x0a4b0',42160
union all select 1922,'0x0b4b5',46261 union all select 1923,'0x06a50',27216
union all select 1924,'0x06d40',27968 union all select 1925,'0x1ab54',109396
union all select 1926,'0x02b60',11104 union all select 1927,'0x09570',38256
union all select 1928,'0x052f2',21234 union all select 1929,'0x04970',18800
union all select 1930,'0x06566',25958 union all select 1931,'0x0d4a0',54432
union all select 1932,'0x0ea50',59984 union all select 1933,'0x06e95',28309
union all select 1934,'0x05ad0',23248 union all select 1935,'0x02b60',11104
union all select 1936,'0x186e3',100067 union all select 1937,'0x092e0',37600
union all select 1938,'0x1c8d7',116951 union all select 1939,'0x0c950',51536
union all select 1940,'0x0d4a0',54432 union all select 1941,'0x1d8a6',120998
union all select 1942,'0x0b550',46416 union all select 1943,'0x056a0',22176
union all select 1944,'0x1a5b4',107956 union all select 1945,'0x025d0',9680
union all select 1946,'0x092d0',37584 union all select 1947,'0x0d2b2',53938
union all select 1948,'0x0a950',43344 union all select 1949,'0x0b557',46423
union all select 1950,'0x06ca0',27808 union all select 1951,'0x0b550',46416
union all select 1952,'0x15355',86869 union all select 1953,'0x04da0',19872
union all select 1954,'0x0a5d0',42448 union all select 1955,'0x14573',83315
union all select 1956,'0x052d0',21200 union all select 1957,'0x0a9a8',43432
union all select 1958,'0x0e950',59728 union all select 1959,'0x06aa0',27296
union all select 1960,'0x0aea6',44710 union all select 1961,'0x0ab50',43856
union all select 1962,'0x04b60',19296 union all select 1963,'0x0aae4',43748
union all select 1964,'0x0a570',42352 union all select 1965,'0x05260',21088
union all select 1966,'0x0f263',62051 union all select 1967,'0x0d950',55632
union all select 1968,'0x05b57',23383 union all select 1969,'0x056a0',22176
union all select 1970,'0x096d0',38608 union all select 1971,'0x04dd5',19925
union all select 1972,'0x04ad0',19152 union all select 1973,'0x0a4d0',42192
union all select 1974,'0x0d4d4',54484 union all select 1975,'0x0d250',53840
union all select 1976,'0x0d558',54616 union all select 1977,'0x0b540',46400
union all select 1978,'0x0b5a0',46496 union all select 1979,'0x195a6',103846
union all select 1980,'0x095b0',38320 union all select 1981,'0x049b0',18864
union all select 1982,'0x0a974',43380 union all select 1983,'0x0a4b0',42160
union all select 1984,'0x0b27a',45690 union all select 1985,'0x06a50',27216
union all select 1986,'0x06d40',27968 union all select 1987,'0x0af46',44870
union all select 1988,'0x0ab60',43872 union all select 1989,'0x09570',38256
union all select 1990,'0x04af5',19189 union all select 1991,'0x04970',18800
union all select 1992,'0x064b0',25776 union all select 1993,'0x074a3',29859
union all select 1994,'0x0ea50',59984 union all select 1995,'0x06b58',27480
union all select 1996,'0x055c0',21952 union all select 1997,'0x0ab60',43872
union all select 1998,'0x096d5',38613 union all select 1999,'0x092e0',37600
union all select 2000,'0x0c960',51552 union all select 2001,'0x0d954',55636
union all select 2002,'0x0d4a0',54432 union all select 2003,'0x0da50',55888
union all select 2004,'0x07552',30034 union all select 2005,'0x056a0',22176
union all select 2006,'0x0abb7',43959 union all select 2007,'0x025d0',9680
union all select 2008,'0x092d0',37584 union all select 2009,'0x0cab5',51893
union all select 2010,'0x0a950',43344 union all select 2011,'0x0b4a0',46240
union all select 2012,'0x0baa4',47780 union all select 2013,'0x0ad50',44368
union all select 2014,'0x055d9',21977 union all select 2015,'0x04ba0',19360
union all select 2016,'0x0a5b0',42416 union all select 2017,'0x15176',86390
union all select 2018,'0x052b0',21168 union all select 2019,'0x0a930',43312
union all select 2020,'0x07954',31060 union all select 2021,'0x06aa0',27296
union all select 2022,'0x0ad50',44368 union all select 2023,'0x05b52',23378
union all select 2024,'0x04b60',19296 union all select 2025,'0x0a6e6',42726
union all select 2026,'0x0a4e0',42208 union all select 2027,'0x0d260',53856
union all select 2028,'0x0ea65',60005 union all select 2029,'0x0d530',54576
union all select 2030,'0x05aa0',23200 union all select 2031,'0x076a3',30371
union all select 2032,'0x096d0',38608 union all select 2033,'0x04bd7',19415
union all select 2034,'0x04ad0',19152 union all select 2035,'0x0a4d0',42192
union all select 2036,'0x1d0b6',118966 union all select 2037,'0x0d250',53840
union all select 2038,'0x0d520',54560 union all select 2039,'0x0dd45',56645
union all select 2040,'0x0b5a0',46496 union all select 2041,'0x056d0',22224
union all select 2042,'0x055b2',21938 union all select 2043,'0x049b0',18864
union all select 2044,'0x0a577',42359 union all select 2045,'0x0a4b0',42160
union all select 2046,'0x0aa50',43600 union all select 2047,'0x1b255',111189
union all select 2048,'0x06d20',27936 union all select 2049,'0x0ada0',44448
;


-- 转换农历年
drop function if exists f_get_lunar_year;
create function f_get_lunar_year(num varchar(10))
	returns varchar(10)
begin
	/* eg: select f_get_lunar_year('2013'); */

	declare temp_num 				int;
	declare chn_str 				varchar(10);
	declare num_str					varchar(20);
	declare res_year 				varchar(20);
	declare i								int;
	declare j								int;

	set chn_str = '零一二三四五六七八九';
	set temp_num = num + 0;
	set i = 1;
	set res_year = '';

	if temp_num >1000 or (temp_num > 0 and temp_num < 10) then
		set num_str = temp_num;
		begin
			while i <= length(num_str) do
				set j = substring(num_str from i for 1)+1;
				set res_year = concat(res_year,substring(chn_str from j for 1));
				set i = i+1;
			end while;
		end;
	end if;

	if temp_num = 10 then
		set num_str = temp_num;
		set res_year = '十';
	end if;

	if temp_num > 10 and temp_num < 20 then
		set num_str = temp_num;
		set j = substring(num_str from 2 for 1)+1;
		set res_year = concat('十',substring(chn_str from j for 1));
	end if;

	if temp_num = 20 then
		set num_str = temp_num;
		set res_year = '二十';
	end if;

	if temp_num > 20 and temp_num < 30 then
		set num_str = temp_num;
		set j = substring(num_str from 2 for 1)+1;
		set res_year = concat('二十',substring(chn_str from j for 1));
	end if;

	if temp_num = 30 then
		set num_str = temp_num;
		set res_year = '三十';
	end if;

	if temp_num = 31 then
		set num_str = temp_num;
		set j = substring(num_str from 2 for 1)+1;
		set res_year = concat('三十',substring(chn_str from j for 1));
	end if;

	return res_year;
end
;

-- 转换农历月
-- select f_get_lunar_month(1);
drop function if exists f_get_lunar_month;
create function f_get_lunar_month(num varchar(10))
	returns varchar(10)
BEGIN
	declare temp_num 				int;
	declare chn_str 				varchar(10);
	declare num_str					varchar(20);
	declare res_month 				varchar(20);
	declare j								int;

	set chn_str = '正二三四五六七八九';
	set temp_num = num + 0;
	set res_month = '';

	if temp_num > 0 and temp_num < 10 then
		set num_str = temp_num;
		set j = substring(num_str from 1 for 1);
		set res_month = substring(chn_str from j for 1);
	end if;

	if temp_num = 10 then
		set res_month = '十';
	end if;
	if temp_num = 11 then
		set res_month = '冬';
	end if;
	if temp_num = 12 then
		set res_month = '腊';
	end if;

	return res_month;
END
;

-- 转换农历日
-- select f_get_lunar_day(23);
drop function if exists f_get_lunar_day;
create function f_get_lunar_day(num varchar(10))
	returns varchar(10)
BEGIN
	declare temp_num int;
	declare chn_str varchar(10);
	declare num_str	varchar(20);
	declare res_day varchar(20);
	declare i int;
	declare j int;

	set chn_str = '一二三四五六七八九';
	set temp_num = num + 0;
	set res_day = '';
	set i = 1;

	if temp_num > 0 and temp_num < 10 then
		set num_str = temp_num;
		set j = substring(num_str from 1 for 1);
		set res_day = concat('初',substring(chn_str from j for 1));
	end if;

	if temp_num = 10 then
		set res_day = '初十';
	end if;

	if temp_num > 10 and temp_num < 20 then
		set num_str = temp_num;
		set j = substring(num_str from 2 for 1);
		set res_day = concat('十',substring(chn_str from j for 1));
	end if;

	if temp_num = 20 then
		set num_str = temp_num;
		set res_day = '二十';
	end if;

	if temp_num > 20 and temp_num < 30 then
		set num_str = temp_num;
		set j = substring(num_str from 2 for 1);
		set res_day = concat('二十',substring(chn_str from j for 1));
	end if;

	if temp_num = 30 then
		set num_str = temp_num;
		set res_day = '三十';
	end if;

	RETURN res_day;
END
;


-- 根据阳历获取农历
-- select f_get_lunar('2013-07-01');
drop function f_get_lunar;
create function f_get_lunar(i_SolarDay varchar(30))
returns varchar(30)
begin
	DECLARE   v_OffSet         INT;
	DECLARE   v_Lunar          INT;          # 农历年是否含闰月,几月是闰月,闰月天数,其它月天数
	DECLARE   v_YearDays       INT;          # 农历年所含天数
	DECLARE   v_MonthDays      INT;          # 农历月所含天数
	DECLARE   v_LeapMonthDays  INT;          # 农历闰月所含天数
	DECLARE   v_LeapMonth      INT;          # 农历年闰哪个月 1-12 , 没闰传回 0
	DECLARE   v_LeapFlag       INT;          # 某农历月是否为闰月  1:是  0:不是
	DECLARE   v_MonthNo        INT;          # 某农历月所对应的2进制数 如农历3月: 001000000000
	DECLARE   i                INT;
	DECLARE   j                INT;
	DECLARE   k                INT;

	DECLARE   v_Year           INT;          # i_SolarDay 对应的农历年
	DECLARE   v_Month          INT;          # i_SolarDay 对应的农历月
	DECLARE   v_Day            INT;          # i_SolarDay 对应的农历日

	DECLARE   o_OutputDate     VARCHAR(25); # 返回值  格式:农历 ****年 **(闰)月 **日

	DECLARE   e_ErrMsg         VARCHAR(200);
	declare   v_j              int;
	declare   v_temp_m         varchar(100);
	declare   v_temp_d         varchar(100);

	# -e_ErrDate        EXCEPTION;
	#输入参数判断
	/* IF i_SolarDay<TO_DATE('1900-01-31','YYYY-MM-DD') OR i_SolarDay>=TO_DATE('2050-01-23','YYYY-MM-DD') THEN
	 RAISE e_ErrDate;
	 END IF ;
	*/

	# i_SolarDay 到 1900-01-30(即农历1900-01-01的前一天) 的天数
	# v_OffSet := TRUNC(i_SolarDay, 'DD') - TO_DATE('1900-01-30', 'YYYY-MM-DD');

	DECLARE exit HANDLER FOR SQLEXCEPTION,SQLWARNING,NOT FOUND
	begin
		set o_OutputDate ='获取农历失败';
		return o_OutputDate;
	end;

	set v_OffSet = DATEDIFF(i_SolarDay,str_to_date('1900-01-30', '%Y-%c-%d'));
	# 确定农历年开始
	set i = 1900;
	WHILE i < 2050 AND v_OffSet > 0 DO
    	set v_YearDays = 348;    #  29*12 以每年12个农历月,每个农历月含29个农历日为基数
		set v_LeapMonthDays = 0;
		# 取出农历年是否含闰月,几月是闰月,闰月天数,其它月天数
		# 如农历2001年: 0x0d954(16进制) -> 55636(10进制) -> 0 110110010101 0100(2进制)
		# 1,2,4,5,8,10,12月大, 3,6,7,9,11月小, 4月为闰月,闰月小
		SELECT dataint INTO v_Lunar FROM dim_solar_data WHERE yearid = i;
		# 传回农历年的总天数
		set j = 32768;    #   100000000000 0000 -> 32768

	    # 依次判断v_Lunar年个月是否为大月,是则加一天
	    WHILE j > 8 Do       # 闰月另行判断 8 -> 0 000000000000 1000
			set v_j = v_Lunar&j;
			if v_j+0 >0 then
				set v_YearDays = v_YearDays + 1;
			END IF;
			set j = j/2;            # 判断下一个月是否为大
	    END WHILE;

	    # 传回农历年闰哪个月 1-12 , 没闰传回 0   15 -> 1 0000
	    set v_LeapMonth = v_Lunar&15 + 0;

		# 传回农历年闰月的天数 ,加在年的总天数上
		IF v_LeapMonth > 0 THEN
			# 判断闰月大小 65536 -> 1 000000000000 0000
			if v_Lunar&65536 + 0 > 0 then
				set v_LeapMonthDays = 30;
			ELSE
				set v_LeapMonthDays = 29;
			END IF;
			set v_YearDays = v_YearDays + v_LeapMonthDays;
		END IF;

		set  v_OffSet = v_OffSet - v_YearDays;
	    set i = i + 1;
	END WHILE;

	IF v_OffSet <= 0 THEN
		# i_SolarDay 在所属农历年(即i年)中的第 v_OffSet 天
		set v_OffSet = v_OffSet + v_YearDays;
		set  i = i - 1;
	END IF;

	# 确定农历年结束
	set v_Year = i;

	# 确定农历月开始
	set i = 1;
	SELECT dataint INTO v_Lunar FROM dim_solar_data WHERE yearid = v_Year;

	# 判断那个月是润月
	# 如农历2001年 (55636,15 -> 0 1101100101010100, 1111 -> 4) 即润4月,且闰月小
	set v_LeapMonth = v_Lunar&15 + 0;
	set v_LeapFlag = 0;

	WHILE i < 13 AND v_OffSet > 0 DO
    	# 判断是否为闰月
		set v_MonthDays = 0;
		IF (v_LeapMonth > 0 AND i = (v_LeapMonth + 1) AND v_LeapFlag = 0) THEN
			# 是闰月
			set i = i - 1;
			set k = i;                # 保存是闰月的时i的值
			set v_LeapFlag = 1;

			# 传回农历年闰月的天数
			if v_Lunar&65536+0 > 0 then
				set v_MonthDays = 30;
			ELSE
				set v_MonthDays = 29;
			END IF;
		ELSE
			# 不是闰月
			set  j = 1;
			set  v_MonthNo = 65536;
			# 计算 i 月对应的2进制数 如农历3月: 001000000000
			WHILE j<= i DO
				set v_MonthNo = v_MonthNo/2;
				set j = j + 1;
			END WHILE;
			# 计算农历 v_Year 年 i 月的天数
			if v_Lunar&v_MonthNo + 0 > 0 then
				set v_MonthDays = 30;
			ELSE
				set v_MonthDays = 29;
			END IF;
    	END IF;

    	# 解除闰月
    	IF v_LeapFlag = 1 AND i = v_LeapMonth +1 THEN
			set  v_LeapFlag = 0;
    	END IF;
	    set v_OffSet = v_OffSet - v_MonthDays;
	    set i = i + 1;
	END while;

	IF v_OffSet <= 0 THEN
		# i_SolarDay 在所属农历月(即i月)中的第 v_OffSet 天
		set  v_OffSet = v_OffSet + v_MonthDays;
		set  i = i - 1;
	END IF;

	# 确定农历月结束
	set v_Month = i;
	# 确定农历日结束
	set v_Day = v_OffSet;
	# 格式化返回值
	set o_OutputDate =CONCAT(f_get_lunar_year(v_Year),'年');

	IF k = i THEN
		set v_temp_m = concat('润',f_get_lunar_month(v_Month),'月');
	ELSE
		set v_temp_m = concat(f_get_lunar_month(v_Month),'月');
	END IF;

	set v_temp_d = f_get_lunar_day(v_Day);
	set o_OutputDate = CONCAT(CONCAT(o_OutputDate,v_temp_m),v_temp_d);

	RETURN o_OutputDate;
END
;
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
MySQL提供了丰富的时间函数获取日期,可以在查询语句中使用这些函数获取需要的日期信息。 1. NOW()函数获取当前日期和时间。该函数返回一个DATETIME类型的值,包含当前的日期和时间。 2. CURDATE()函数获取当前日期。该函数返回一个DATE类型的值,只包含当前的日期,时间部分为'00:00:00'。 3. CURTIME()函数获取当前时间。该函数返回一个TIME类型的值,只包含当前的时间,日期部分为'0000-00-00'。 4. DATE()函数从一个DATETIME或TIMESTAMP类型的值中提取日期部分。该函数返回一个DATE类型的值,只包含日期,时间部分被忽略。 5. EXTRACT()函数提取日期和时间部分。可以使用EXTRACT函数从一个DATETIME或TIMESTAMP类型的值中提取年份、月份、日期、小时、分钟或秒数。 6. DATE_FORMAT()函数格式化日期和时间。可以使用DATE_FORMAT函数将一个日期或时间值按照指定的格式转换成字符串。 例如,要获取当前日期和时间可以使用NOW()函数: SELECT NOW(); 要获取当前日期可以使用CURDATE()函数: SELECT CURDATE(); 要获取当前时间可以使用CURTIME()函数: SELECT CURTIME(); 要从一个DATETIME类型的值中提取日期部分可以使用DATE()函数: SELECT DATE('2022-01-01 12:34:56'); 要提取一个DATETIME类型的值的年份可以使用EXTRACT函数: SELECT EXTRACT(YEAR FROM '2022-01-01 12:34:56'); 要将一个日期或时间值按照指定的格式转换成字符串可以使用DATE_FORMAT函数: SELECT DATE_FORMAT('2022-01-01', '%Y年%m月%d日'); 总之,MySQL的时间函数提供了丰富的功能,可以根据需求灵活地获取日期信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值