Adding ical support to the C version.

This commit is contained in:
SyllaBear
2016-04-03 10:24:18 +08:00
parent 3681f1c45b
commit 054fbd76f4
4 changed files with 41 additions and 12 deletions

View File

@@ -51,7 +51,7 @@ calendar, thunderbird + lightning插件, iphone/ipad, 安卓都支持。
### C 版本
C版本速度更快但暂时只在终端上输出农历,不能直接生成ical文件。
C版本速度更快但暂时只在终端上输出ical。用户必须使用`>`将stdout的结果导入到指定文件以生成ical文件。
编译:
@@ -60,12 +60,11 @@ C版本速度更快但暂时只在终端上输出农历不能直接生成i
运行:
生成某年农历
$ ./lunarcal 2015
生成数年农历
$ ./lunarcal 2015 2019
#生成某年农历
$ ./lunarcal 2016 > chinese_lunar_2016.ics
#生成数年农历
$ ./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
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
or
$ ./lunarcal 2015 2019
# Specific year
$ ./lunarcal 2016 > chinese_lunar_2016.ics
# or multiple years
$ ./lunarcal 2016 2019 > chinese_lunar_2016_2019.ics
[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;
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);
} else if (strcmp(fmt, "%y-%m-%d %H:%M") == 0) {
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);
}
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) {
cn_lunarcal(start);
start++;
}
printf("END:VCALENDAR\n");
return 0;
}

View File

@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "astro.h"
#include "lunarcalbase.h"
@@ -280,14 +281,30 @@ void print_lunarcal(struct lunarcal *p[], int len)
int i;
char isodate[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++) {
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)
sprintf(tmp, "%s", CN_MON[p[i]->month]);
else
sprintf(tmp, "%s", CN_DAY[p[i]->day]);
if (p[i]->solarterm > -1)
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);
}
}