在 linux 将 vcf 生日转换为 caldav

convert vcf birthdays to caldav on linux

我有一个 vCard 文件 mycontacts.vcf:

BEGIN:VCARD
N:Montana;Joe;;;
FN:Joe Montana
BDAY;value=date:1988-05-20
END:VCARD

BEGIN:VCARD
N:Smith;Joe;;;
FN:Joe Smith
BDAY;value=date:1999-07-04
END:VCARD

如何从 linux 命令行创建 CalDAV 文件,例如使用 awk 或一个简单的 python 脚本,输出为:

BEGIN:VEVENT
SUMMARY:Joe Montana
DTSTART;VALUE=DATE:19880520
DTEND;VALUE=DATE:19880521
END:VEVENT

BEGIN:VEVENT
SUMMARY:Joe Smith
DTSTART;VALUE=DATE:19990704
DTEND;VALUE=DATE:19990705
END:VEVENT

顺便说一下,联系人是使用 pythonista 应用从 ipad 导出的:

import contacts
people = contacts.get_all_people()
for p in people:
  print(p.vcard.decode('utf-8'))

生成的 CalDAV 将导入 thunderbird。

您可以使用任何 awk 将结束日期设置为当前日期:

$ cat vcf2dav.awk
BEGIN { OFS=":" }
!NF { print; next }
{
    tag = val = [=10=]
    sub(/[:;].*$/,"",tag)
    sub(/^[^:]+:/,"",val)
}
tag ~ /^(BEGIN|END)$/ { print tag, "VEVENT" }
tag == "FN" { print "SUMMARY", val }
tag == "BDAY" {
    gsub(/-/,"",val)
    print "DTSTART;VALUE=DATE", val
    print "DTEND;VALUE=DATE", val
}

.

$ awk -f vcf2dav.awk mycontacts.vcf
BEGIN:VEVENT
SUMMARY:Joe Montana
DTSTART;VALUE=DATE:19880520
DTEND;VALUE=DATE:19880520
END:VEVENT

BEGIN:VEVENT
SUMMARY:Joe Smith
DTSTART;VALUE=DATE:19990704
DTEND;VALUE=DATE:19990704
END:VEVENT

但是如果你真的想要它是后天那么你需要使用时间函数,例如使用 GNU awk 内置时间函数:

$ cat vcf2dav.awk
BEGIN { OFS=":" }
!NF { print; next }
{
    tag = val = [=12=]
    sub(/[:;].*$/,"",tag)
    sub(/^[^:]+:/,"",val)
}
tag ~ /^(BEGIN|END)$/ { print tag, "VEVENT" }
tag == "FN" { print "SUMMARY", val }
tag == "BDAY" {
    begDate = val
    endDate = dayAfter(begDate)
    gsub(/-/,"",begDate)
    print "DTSTART;VALUE=DATE", begDate
    print "DTEND;VALUE=DATE",   endDate
}

function dayAfter(curDate,      curSecs, nxtDate) {
    curSecs = mktime(gensub(/-/," ","g",curDate)" 0 0 0")
    nxtDate = strftime("%Y%m%d",curSecs + 24*60*60)
    return nxtDate
}

.

$ awk -f vcf2dav.awk mycontacts.vcf
BEGIN:VEVENT
SUMMARY:Joe Montana
DTSTART;VALUE=DATE:19880520
DTEND;VALUE=DATE:19880521
END:VEVENT

BEGIN:VEVENT
SUMMARY:Joe Smith
DTSTART;VALUE=DATE:19990704
DTEND;VALUE=DATE:19990705
END:VEVENT

对于其他 awk,您将更改 dayAfter() 函数以调用 UNIX date 或执行您的环境中的任何其他工作以获得第二天。