gnuplot:如何获取正确的星期数? [英] gnuplot: How to get correct week numbers?
问题描述
从这个问题开始 gnuplot为什么警告:字符串中的时间格式错误,发现在某些情况下,使用时间指定符%W
和%U
的gnuplot中的星期数是错误的.
Originating from this question gnuplot why warning: Bad time format in string,
there was the finding that the week numbers in gnuplot using the time specifiers %W
and %U
are wrong in some cases.
显然,周数的定义不同.此外,一周开始时有不同的定义,例如在星期日或星期一.经常使用的周数定义之一(不过,在美国和其他一些国家/地区不使用)是根据ISO 8601.
Apparently, there are different definitions of the week numbers. Furthermore, there are different definitions when a week starts, e.g. on Sunday or Monday. One definition for week numbers, which is commonly used (however, not in the US and a few other countries) is according to ISO 8601.
代码:(以说明错误的星期数)
Code: (to illustrate wrong week numbers)
### wrong week numbering in gnuplot with %W and %U
reset session
StartDate = "24.12.2020"
myTimeFmt = "%d.%m.%Y"
SecondsPerDay = 3600*24
print " date %a %w %d %j %W %U"
print "===================================="
do for [i=0:20] {
t = strptime(myTimeFmt,StartDate) + i*SecondsPerDay
myDate = strftime(myTimeFmt." %a %w %d %j %W %U", t)
print sprintf("%s", myDate)
}
### end of code
gnuplot时间说明符:
gnuplot time specifiers:
%a abbreviated name of day of the week
%w day of the week, 0–6 (Sunday = 0)
%d day of the month, 01–31
%j day of the year, 1–366
%W week of the year (week starts on Monday)
%U week of the year (week starts on Sunday)
结果:
date %a %w %d %j %W %U
====================================
24.12.2020 Thu 04 24 359 52 52
25.12.2020 Fri 05 25 360 52 52
26.12.2020 Sat 06 26 361 52 52
27.12.2020 Sun 00 27 362 52 53
28.12.2020 Mon 01 28 363 53 53
29.12.2020 Tue 02 29 364 53 53
30.12.2020 Wed 03 30 365 53 53
31.12.2020 Thu 04 31 366 53 53
01.01.2021 Fri 05 01 001 01 01 ???
02.01.2021 Sat 06 02 002 01 01 ???
03.01.2021 Sun 00 03 003 00 01 ???
04.01.2021 Mon 01 04 004 01 01
05.01.2021 Tue 02 05 005 01 01
06.01.2021 Wed 03 06 006 01 01
07.01.2021 Thu 04 07 007 01 01
08.01.2021 Fri 05 08 008 01 01
09.01.2021 Sat 06 09 009 01 01
10.01.2021 Sun 00 10 010 01 02
11.01.2021 Mon 01 11 011 02 02
12.01.2021 Tue 02 12 012 02 02
13.01.2021 Wed 03 13 013 02 02
问题:有解决此问题的解决方法吗?
Question: Is there a workaround to fix this?
推荐答案
基于此处的描述: https://en.wikipedia.org/wiki/ISO_week_date ,我猜想ISO 8601定义的实质是:
Based on the description here: https://en.wikipedia.org/wiki/ISO_week_date, I guess the essence of the ISO 8601 definition is:
- 一周从星期一开始
- 第01周是一年中第一个星期四的一周
- 一周属于大部分时间所在的年份
- 从星期四开始或结束的年有53周,其他有52周
代码:
### correct week number according to ISO 8601
reset session
dow(t) = int(tm_wday(t)) ? tm_wday(t) : 7 # day of week 1=Mon, ..., 7=Sun
week(t) = int((11 + tm_yday(t) - dow(t))/7) # "raw"week of year
wday(d,m,y) = tm_wday(strptime("%d.%m.%Y",sprintf("%02d.%02d.%04d",d,m,y))) # week day of certain date
wpy(y) = wday(1,1,y)==4 || wday(31,12,y)==4 ? 53 : 52 # weeks per year
woy(t) = week(t) < 1 ? wpy(tm_year(t)-1) : \
week(t) > wpy(tm_year(t)) ? 1 : week(t) # week of year
yow(t) = int(week(t) < 1 ? tm_year(t)-1 : week(t) > wpy(tm_year(t)) ? \
tm_year(t)+1 : tm_year(t)) # year of week (could be previous, current or next)
StartDate = "24.12.2020"
myTimeFmt = "%d.%m.%Y"
SecondsPerDay = 3600*24
print " date %a DoW %d %j YoW WoY"
print "======================================"
do for [i=0:20] {
t = strptime(myTimeFmt,StartDate) + i*SecondsPerDay
myDate = strftime(myTimeFmt." %a", t)
myDate2 = strftime("%d %j", t)
print sprintf("%s %02d %s %04d-W%02d", myDate, dow(t), myDate2, yow(t), woy(t))
}
### end of code
结果:
date %a DoW %d %j YoW WoY
======================================
24.12.2020 Thu 04 24 359 2020-W52
25.12.2020 Fri 05 25 360 2020-W52
26.12.2020 Sat 06 26 361 2020-W52
27.12.2020 Sun 07 27 362 2020-W52
28.12.2020 Mon 01 28 363 2020-W53
29.12.2020 Tue 02 29 364 2020-W53
30.12.2020 Wed 03 30 365 2020-W53
31.12.2020 Thu 04 31 366 2020-W53
01.01.2021 Fri 05 01 001 2020-W53
02.01.2021 Sat 06 02 002 2020-W53
03.01.2021 Sun 07 03 003 2020-W53
04.01.2021 Mon 01 04 004 2021-W01
05.01.2021 Tue 02 05 005 2021-W01
06.01.2021 Wed 03 06 006 2021-W01
07.01.2021 Thu 04 07 007 2021-W01
08.01.2021 Fri 05 08 008 2021-W01
09.01.2021 Sat 06 09 009 2021-W01
10.01.2021 Sun 07 10 010 2021-W01
11.01.2021 Mon 01 11 011 2021-W02
12.01.2021 Tue 02 12 012 2021-W02
13.01.2021 Wed 03 13 013 2021-W02
为了使用星期数,例如作为时间轴标签,最好对%W
实施此操作.偶然地,SourceForge上最近有一个错误报告.因此,我认为它将在下一版本中很快得到修复.
In order to use the week numbers, e.g. as time axis labels it would be ideal to have this implemented for %W
. Accidentially, there was a recent bug report on SourceForge.
So, I assume it will be fixed pretty soon in one of the next versions.
这篇关于gnuplot:如何获取正确的星期数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!