Merge pull request #4 from bearsylla/master

替C版增加ical功能
This commit is contained in:
Chen Wei
2016-04-10 07:50:43 +08:00
4 changed files with 42 additions and 12 deletions

View File

@@ -51,7 +51,7 @@ calendar, thunderbird + lightning插件, iphone/ipad, 安卓都支持。
### C 版本 ### C 版本
C版本速度更快但暂时只在终端上输出农历,不能直接生成ical文件。 C版本速度更快但暂时只在终端上输出ical。用户必须使用`>`将stdout的结果导入到指定文件以生成ical文件。
编译: 编译:
@@ -60,12 +60,11 @@ C版本速度更快但暂时只在终端上输出农历不能直接生成i
运行: 运行:
生成某年农历 #生成某年农历
$ ./lunarcal 2015 $ ./lunarcal 2016 > chinese_lunar_2016.ics
生成数年农历
$ ./lunarcal 2015 2019
#生成数年农历
$ ./lunarcal 2016 2019 > chinese_lunar_2016_2019.ics
### 版权 ### 版权
@@ -148,11 +147,13 @@ The date must in ISO format.
There is also a C version under directory "c". Run `make` to generate the There is also a C version under directory "c". Run `make` to generate the
executable `lunarcal`. Run `lunarcal` with year will print out the Chinese Lunar executable `lunarcal`. Run `lunarcal` with year will print out the Chinese Lunar
Calendar to terminal, for example: Calendar in ical format to the terminal. Use `>` to redirect the output to a file, for example:
$ ./lunarcal 2015 # Specific year
or $ ./lunarcal 2016 > chinese_lunar_2016.ics
$ ./lunarcal 2015 2019
# or multiple years
$ ./lunarcal 2016 2019 > chinese_lunar_2016_2019.ics
[Contact me](mailto: weichen302@gmail.com) [Contact me](mailto: weichen302@gmail.com)

View File

@@ -196,7 +196,9 @@ size_t jdftime(char *isodt, double jd, char *fmt, double tz, int isut)
M = ms / 60; M = ms / 60;
S = ms % 60; S = ms % 60;
if (strcmp(fmt, "%y-%m-%d") == 0) { if (strcmp(fmt, "%y%m%d") == 0) {
sprintf(isodt, "%04d%02d%02d", y, m, d);
} else if (strcmp(fmt, "%y-%m-%d") == 0) {
sprintf(isodt, "%04d-%02d-%02d", y, m, d); sprintf(isodt, "%04d-%02d-%02d", y, m, d);
} else if (strcmp(fmt, "%y-%m-%d %H:%M") == 0) { } else if (strcmp(fmt, "%y-%m-%d %H:%M") == 0) {
sprintf(isodt, "%04d-%02d-%02d %02d:%02d", y, m, d, H, M); sprintf(isodt, "%04d-%02d-%02d %02d:%02d", y, m, d, H, M);

View File

@@ -17,10 +17,19 @@ int main(int argc, char *argv[])
exit(2); exit(2);
} }
printf("BEGIN:VCALENDAR\n"
"PRODID:-//Chen Wei//Chinese Lunar Calendar//EN\n"
"VERSION:2.0\n"
"CALSCALE:GREGORIAN\n"
"METHOD:PUBLISH\n"
"X-WR-CALNAME:农历\n"
"X-WR-TIMEZONE:Asia/Shanghai\n"
"X-WR-CALDESC:中国农历%d-%d, 包括节气. 数据来自香港天文台\n", start, end);
while (start <= end) { while (start <= end) {
cn_lunarcal(start); cn_lunarcal(start);
start++; start++;
} }
printf("END:VCALENDAR\n");
return 0; return 0;
} }

View File

@@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <time.h>
#include "astro.h" #include "astro.h"
#include "lunarcalbase.h" #include "lunarcalbase.h"
@@ -180,6 +181,7 @@ int mark_month_day(struct lunarcal *lcs[])
lc_start = newmoons[firstnm_offset]; lc_start = newmoons[firstnm_offset];
lc_end = solarterms[MAX_SOLARTERMS -1]; lc_end = solarterms[MAX_SOLARTERMS -1];
jd = lc_start; jd = lc_start;
month_day1 = 0;
len = 0; len = 0;
while (jd < lc_end) { /* fill in days into array lcs */ while (jd < lc_end) { /* fill in days into array lcs */
/* scan for month jd belongs */ /* scan for month jd belongs */
@@ -280,14 +282,30 @@ void print_lunarcal(struct lunarcal *p[], int len)
int i; int i;
char isodate[30]; char isodate[30];
char tmp[30]; char tmp[30];
char dtstart[30];
char dtend[30];
char utcstamp[30];
struct tm* utc_time;
time_t t = time(NULL);
utc_time = gmtime(&t);
sprintf (utcstamp, "%04d%02d%02dT%02d%02d%02dZ", 1900+utc_time->tm_year, utc_time->tm_mon, utc_time->tm_mday, utc_time->tm_hour, utc_time->tm_min, utc_time->tm_sec);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
jdftime(isodate, p[i]->jd, "%y-%m-%d", 0, 0); jdftime(isodate, p[i]->jd, "%y-%m-%d", 0, 0);
jdftime(dtstart, p[i]->jd, "%y%m%d", 0, 0);
jdftime(dtend, p[i]->jd, "%y%m%d", 24, 0);
if (p[i]->day == 1) if (p[i]->day == 1)
sprintf(tmp, "%s", CN_MON[p[i]->month]); sprintf(tmp, "%s", CN_MON[p[i]->month]);
else else
sprintf(tmp, "%s", CN_DAY[p[i]->day]); sprintf(tmp, "%s", CN_DAY[p[i]->day]);
if (p[i]->solarterm > -1) if (p[i]->solarterm > -1)
sprintf(tmp, "%s %s", tmp, CN_SOLARTERM[p[i]->solarterm]); sprintf(tmp, "%s %s", tmp, CN_SOLARTERM[p[i]->solarterm]);
printf("%s %s \n", isodate, tmp); printf("BEGIN:VEVENT\n"
"DTSTAMP:%s\n"
"UID:%s-lc@infinet.github.io\n"
"DTSTART;VALUE=DATE:%s\n"
"DTEND;VALUE=DATE:%s\n"
"STATUS:CONFIRMED\n"
"SUMMARY:%s\n"
"END:VEVENT\n", utcstamp, isodate, dtstart, dtend, tmp);
} }
} }