📄 protime.c
字号:
#include <time.h>int GetSysTime(char type){ long localtm; struct tm *curtime; localtm = time(0L); curtime = localtime(&localtm); switch(type){ case 'D': return((curtime->tm_year + 1900) * 10000 + (curtime->tm_mon + 1) * 100 + curtime->tm_mday); case 'T': return(curtime->tm_hour * 10000 +curtime->tm_min * 100 +curtime->tm_sec); break; case 'W': return(curtime->tm_wday); break; }}int GetYmd(int d1, char type){ switch(type){ case 'Y': return(d1 / 10000); case 'M': return(d1 % 10000 / 100); case 'D': return(d1 % 100); }}int IsLyear(int d1){ int year; year=GetYmd(d1, 'Y'); return(year % 4 == 0 && year % 100 != 0 || year % 400 == 0);}int IsDate(int d1){ if(GetYmd(d1, 'D') <1) return(0); switch(GetYmd(d1, 'M')) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(GetYmd(d1, 'D') > 31) return(0); else return(1); case 2: if(IsLyear(d1) && GetYmd(d1, 'D') > 29) return(0); if(!IsLyear(d1) && GetYmd(d1, 'D') > 28) return(0); return(1); case 4: case 6: case 9: case 11: if(GetYmd(d1, 'D') > 30) return(0); else return(1); default: return(0); }}int MonthDay(int d1){ if(!IsDate(d1)) return(0); switch(GetYmd(d1, 'M')) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return(31); case 2: if(IsLyear(d1)) return(29); else return(28); case 4: case 6: case 9: case 11: return(30); }}int YearStart(int d1){ return(GetYmd(d1, 'Y') * 10000 + 100 + 1 );} int YearEnd(int d1){ return(GetYmd(d1, 'Y') * 10000 + 1200 + 31 );} int MonthStart(int d1){ return(d1 / 100 * 100 + 1 );} int MonthEnd(int d1){ return(d1 / 100 * 100 + MonthDay(d1));} int AddDate(int d1, int cnt, char type){ int i, year, month; switch(type) { case 'D': for(i = 1; i <= cnt; i++) while(!IsDate(++d1)); return(d1); case 'M': year = GetYmd(d1, 'Y') + cnt / 12; month = GetYmd(d1, 'M') + cnt % 12; if(month > 12 ) { month = month - 12; year++; } d1 = year * 10000 + month * 100 + GetYmd(d1, 'D'); while(!IsDate(d1)) d1--; return(d1); case 'Y': d1 = d1 + cnt * 10000; while(!IsDate(d1)) d1--; return(d1); }}int SubDate(int d1, int cnt, char type){ int i, year, month; switch(type) { case 'D': for(i = 1; i <= cnt; i++) while(!IsDate(--d1)); return(d1); case 'M': year = GetYmd(d1, 'Y') - cnt / 12; month = GetYmd(d1, 'M') - cnt % 12; if(month < 1 ) { year--; month = month + 12; } d1 = year * 10000 + month * 100 + GetYmd(d1, 'D'); while(!IsDate(d1)) d1--; return(d1); case 'Y': d1 = d1 - cnt * 10000; while(!IsDate(d1)) d1--; return(d1); }} int CalcSavDate(int d1, int d2){ if(GetYmd(d1, 'D') == 31) d1--; if(GetYmd(d2, 'D') == 31) d2--; if(d1 < d2 ) return((GetYmd(d2 ,'Y') - GetYmd(d1, 'Y')) * 360 + (GetYmd(d2, 'M') - GetYmd(d1, 'M')) * 30 + GetYmd(d2, 'D') - GetYmd(d1, 'D')); else return 0;}int CalcCurDate(int d1, int d2){ int day; if(d1 >= d2) return(0); day = GetYmd(d2, 'D') - GetYmd(d1, 'D');//零头天数 while( d1 / 100 != d2 / 100 ) { day += MonthDay(d1); d1 = AddDate(d1, 1, 'M'); } return(day); }void ChineseDate(char *str, int d1, char type){ int day,n1,n2,n3,n4; char *num[]={"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; switch(type) { case 'Y': day = GetYmd(d1, 'Y'); n1 = day % 10; n2 = (day / 10) %10; n3 = (day / 100) %10; n4 = (day / 1000) %10; /*年按数值直接写入*/ sprintf(str,"%s%s%s%s", num[n4], num[n3], num[n2], num[n1]); break; case 'M': day = GetYmd(d1, 'M'); n1 = day % 10; n2 = (day / 10) %10; /*月为壹,贰和壹拾在其前加零*/ if(day == 1 || day == 2 || day == 10) strcat(str, "零"); if(n2 == 1) strcat(str, "壹拾"); if(n1 > 0 ) strcat(str,num[n1]); break; case 'D': day = GetYmd(d1, 'D'); n1 = day % 10; n2 = (day / 10) %10; /*日为壹至壹拾,贰拾,叁拾在其前加零*/ if(day >=1 && day <=10 || day == 20 || day == 30) strcat(str,"零"); if(n2 >=1 ) { strcat(str,num[n2]); strcat(str,"拾"); } if(n1 > 0 ) strcat(str,num[n1]); break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -