- UID
- 51057
- 麦力
- 262
- 注册时间
- 2019-6-26
- 最后登录
- 2021-3-17
- 精华
- 0
- 阅读权限
- 20
- 在线时间
- 8 小时
|
没学过c,写起来真难。这个农历数据是从js版转过来的,手表上测试过了应该没问题
农历年份对应真实的农历年份,比如2019年1月是属于农历的2018
闰月的话输出后会大于12,减掉12就是置闰的那个月份
没转换成中文,需要的同学自己改吧。
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "maibu_sdk.h"
- #include "maibu_res.h"
- static const int lunarArr[82]={51536,//2019
- 86756,21856,43856,88866,27936,59990,29264,25776,51573,51888,//20-29
- 21920,44387,46736,95531,46368,45648,107702,42160,19120,21941,//30-39
- 23248,46752,111906,55584,119383,53840,42320,84693,19296,23376,//40-49
- 55971,60560,125224,59680,53856,42342,42352,19808,27988,30032,//50-59
- 29776,59699,26928,86711,21168,42416,87461,22176,46672,95396,//60-69
- 46240,108888,43344,21200,43734,43856,23200,47700,55888,54432,//70-79
- 117075,51552,103655,21856,43856,88869,27936,59984,58532,26800,//80-89
- 51576,19120,21936,44390,46752,29984,94804,46160,43184,84402,//90-99
- 19120//2100
- };
- static const uint16_t monthSum[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
- static uint16_t lunarYear=2019;
- static uint8_t lunarMonth=9;
- static uint8_t lunarDay=15;
- void toLunar(uint16_t y,uint8_t m,uint8_t d){
- //2019年2月5日,农历正月初一
- int tmp=0;
- if(y%4==0 && m<3 && d<30){
- tmp=1;
- }
- int days=(y-2019)*365+(y-2016)/4+monthSum[m]+d-35-tmp;
- uint8_t subEnd=0;
- uint8_t yy=0;
- uint8_t run,i,dayspermon,monsperyear;
- while(subEnd==0){
- //这个外部的循环每次检查一年的数据。
-
- //先拿到这年的数据,取出闰月,右移四位。
- tmp=lunarArr[yy];
- run=tmp % 16;
- tmp=tmp/16;
- monsperyear=(run==0)? 12 : 13;
- //下面内部的循环则是检查这次的年份中每个月份的数据。
- for(i=1;i<=monsperyear;i++){
- dayspermon=(tmp%2==1)? 30 : 29;
- if(days<=dayspermon){
- subEnd=1;
- break;
- }else{
- days=days-dayspermon;
- tmp=tmp/2;
- }
- }
- if(subEnd==1){
- break;
- }
- yy++;
- }
- if(run==0){
- lunarMonth=i;
- }else{
- if(i<=run){
- lunarMonth=i;
- }else if(i==run+1){
- lunarMonth=i+11;
- }else{
- lunarMonth=i-1;
- }
- }
- lunarYear=2019+yy;
- lunarDay=days;
- }
- int main()
- {
- /*创建窗口*/
- P_Window p_window = app_window_create();
- if (NULL == p_window)
- {
- return 0;
- }
- /*创建文本图层框架范围*/
- GRect frame = {{0, 45}, {40, 128}};
- char buf[50]={0};
- toLunar(2033,12,22);
- sprintf(buf,"%d年%d月%d日",lunarYear,lunarMonth,lunarDay);
- /*生成文本结构体, 依次为文本内容、文本显示框架、对齐方式、字体字号*/
- LayerText text = {buf, frame, GAlignCenter, GColorWhite, U_GBK_SIMSUNBD_16};
- /*创建文本图层*/
- P_Layer layer = app_layer_create_text(&text);
- /*添加文本图层到窗口中*/
- app_window_add_layer(p_window, layer);
- /*把窗口放入窗口栈中显示*/
- app_window_stack_push(p_window);
- return 1;
- }
复制代码 |
|