Skip to content

获取周数和每周起始截止日期函数取值错误 #2

@risoyo

Description

@risoyo

在边界处会出现数据错误问题,比如20220213是周日,本周应为2022-02-07 ~ 2022-02-13 Week 7
但是在使用时

.action{$ini_date := toDate "2006-01-02" "2021-05-02"}
.action{$now_date := now}
.action{$day := mod (div ($now_date.Sub $ini_date).Hours 24) 7}
.action{$leftday :=mul (sub $day 1)  24}
.action{$rightday :=mul (sub 7 $day) 24}
.action{$leftHour :=nospace (cat "-" $leftday "h") }
.action{$rightHour :=nospace (cat "+" $rightday "h")}
.action{$ng_duration := (toDate "2006-01-02" "2021-12-26").Sub now}
.action{$weekhours:=mul 7 24}
.action{$duration:=sub 0 $ng_duration.Hours }
.action{$week := add (div (div (now.Sub (toDate "2006-01-02" "2021-12-26")).Hours 24) 7) 1}
.action{$today:= (now | date "2006-01")}
.action{$weekResult:= (list $today "Week" $week| join " ")}


.action{(now | date_modify $leftHour) | date "2006-01-02"} ~ .action{(now | date_modify $rightHour) | date "2006-01-02"}  Week .action{$week}

计算出的今日为2022-02-13 ~ 2022-02-20 Week 8,一周的起始、结束、周数在周日这一天均出现了错误
分析了函数发现在计算$day时,使用总天数摩7,而后面计算一周的始末时又用这个摩7的结果乘以24来作为date_modify的时间,这样存在的问题是摩7所得的数为(0,1,2,3,4,5,6)这样在计算用于date_modify的leftHour时,最大时间是120小时,而非一周完整7天对应的144小时

所以应在计算$day时,加1进行修正.action{$day := add (mod (div ($now_date.Sub $ini_date).Hours 24) 7) 1},使得结果的范围在(1,2,3,4,5,6,7)中
同时对坐标日进行调整,调整为适合个人习惯的以周一为每周的起点.action{$ini_date := toDate "2006-01-02" "2021-05-03"}

调整后对应到周计划、周总结的函数为

.action{$ini_date := toDate "2006-01-02" "2021-05-03"}
.action{$now_date := now}
.action{$day := add (mod (div ($now_date.Sub $ini_date).Hours 24) 7) 1}
.action{$leftday :=mul (sub $day 1)  24}
.action{$rightday :=mul (sub 7 $day) 24}
.action{$leftHour :=nospace (cat "-" $leftday "h") }
.action{$rightHour :=nospace (cat "+" $rightday "h")}
.action{$ng_duration := (toDate "2006-01-02" "2021-12-27").Sub now}
.action{$weekhours:=mul 7 24}
.action{$duration:=sub 0 $ng_duration.Hours }
.action{$week := add (div (div (now.Sub (toDate "2006-01-02" "2021-12-27")).Hours 24) 7) 1}
.action{$today:= (now | date "2006-01")}
.action{$weekResult:= (list $today "Week" $week| join " ")}

修改后调整系统时间,使用20220101、20220102、20220213、20220214、20220217、20220219、20220227、20220228日期进行验证,结果均为正确的

另针对repo的readme中四象限这一栏中“另外还加了一个列出未完成项目的功能,目前只能列出所有笔记中的待办事项,而不能单独指定某个笔记。”
提供一个页内未完成待办事项的检索思路,指定本页某块的name为'2022-02 Week 7 Dairy'后,按照root_id搜索本页内的待完成事项

SELECT * FROM blocks WHERE id in (select parent_id from blocks where type !='l' ) and type='i' and subtype = 't' and markdown like '%[ ]%' and content !='' and created <= '20220214' and created >= '20220206' and root_id !='' and root_id in (select root_id from blocks where name = '2022-02 Week 7 Dairy' )  order by created DESC

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions