Chinaunix

标题: 根据年周,返回周一的具体日期(注:系统返回2012-1-1为52周) [打印本页]

作者: plumdavid    时间: 2013-08-16 15:29
标题: 根据年周,返回周一的具体日期(注:系统返回2012-1-1为52周)
本帖最后由 plumdavid 于 2013-08-16 18:03 编辑

根据年周数,如2013年32周,返回此周的周一日期.
注:周以星期一为第一天.

谢谢各位回复,大家多数的算法都是错误的。
作者: 代号:军刀    时间: 2013-08-16 16:00
  1. awk -vl=32 'BEGIN{t1=mktime("2013 01 01 00 00 00")+(l*7*24*3600);t2=(strftime("%u",tstamp)-1)*3600;print strftime("%Y%m%d",t1-t2)}'
复制代码

作者: plumdavid    时间: 2013-08-16 16:05
回复 2# 代号:军刀


    谢谢.
    刚研究了一下shell年周数,系统设定的有点复杂,比如:2012年1月1日,年周数是2011年52周.
    这个问题我还得再研究下.
作者: plumdavid    时间: 2013-08-16 16:06
  1. [nms@localhost week]$ date -d '2012-01-01 00:00:01' '+%V'
  2. 52
复制代码

作者: jason680    时间: 2013-08-16 16:16
回复 4# plumdavid

maybe you want to know there are many spec to define week date different

http://en.wikipedia.org/wiki/ISO_week_date
The ISO week date system is a leap week calendar system that is part of the ISO 8601 date and time standard. The system is used (mainly) in government and business for fiscal years, as well as in timekeeping.
...
作者: plumdavid    时间: 2013-08-16 16:27
jason680 发表于 2013-08-16 16:16
回复 4# plumdavid

maybe you want to know there are many spec to define week date different


多谢。
作者: guogang225    时间: 2013-08-16 16:32
wk=32;date -d "20121231 $wk week" +%Y-%m-%d
作者: 关阴月飞    时间: 2013-08-16 16:32
回复 1# plumdavid
  1. #!/bin/bash
  2. Y=$1
  3. W=$2
  4. D=$[`date -d "${Y}0101" +%u`-1]
  5. d=$[(W-1)*7-D]

  6. date -d "${Y}0101 +${d} days" "+%G-%m-%d 第%V周 星期%u"
复制代码
  1. [root@ awktest]# cat a.sh
  2. #!/bin/bash
  3. Y=$1
  4. W=$2
  5. D=$[`date -d "${Y}0101" +%u`-1]
  6. d=$[(W-1)*7-D]

  7. date -d "${Y}0101 +${d} days" "+%G-%m-%d 第%V周 星期%u"
  8. [root@ awktest]# ./a.sh 2013 32
  9. 2013-08-05 第32周 星期1
  10. [root@ awktest]# ./a.sh 2013 34
  11. 2013-08-19 第34周 星期1
  12. [root@ awktest]# ./a.sh 2013 3
  13. 2013-01-14 第03周 星期1
  14. [root@ awktest]# ./a.sh 2013 25
  15. 2013-06-17 第25周 星期1
  16. [root@ awktest]#
复制代码

作者: jason680    时间: 2013-08-16 16:35
回复 6# plumdavid

which one do you like (%V,%U,%W) ...

# man date | grep '%[UVW]'
       %g     the 2-digit year corresponding to the %V week number
       %G     the 4-digit year corresponding to the %V week number
       %U     week number of year with Sunday as first day of week (00..53)
       %V     week number of year with Monday as first day of week (01..53)
       %W     week number of year with Monday as first day of week (00..53)

# seq 1 10 | xargs -i date -d "2012-1-{} 0:0:1" +"%V,%U,%W,%w"
52,01,00,0
01,01,01,1
01,01,01,2
01,01,01,3
01,01,01,4
01,01,01,5
01,01,01,6
01,02,01,0
02,02,02,1
02,02,02,2


作者: dn833    时间: 2013-08-16 17:02
  1. #!/bin/bash
  2. YEAR=2013
  3. WEEK=32
  4. DAY=$(echo $((WEEK*7-1)))
  5. DATE=($(date -d "${YEAR}0101 +$DAY day" '+%Y %m %d'))

  6. cal -m ${DATE[1]} ${DATE[0]}|awk -v year=${DATE[0]} -v month=${DATE[1]} -v day=${DATE[2]} '$0~day{print year,month,$1}'
复制代码

作者: reyleon    时间: 2013-08-16 17:03
回复 9# jason680


   
感觉很有趣,那到底该用哪个??
作者: cao627    时间: 2013-08-16 20:17
本帖最后由 cao627 于 2013-08-16 20:18 编辑

@plumdavid
  1. January 2013      
  2. Su Mo Tu We Th Fr Sa  
  3.        1  2  3  4  5  
  4. 6  7  8  9 10 11 12  
  5. 13 14 15 16 17 18 19  
  6. 20 21 22 23 24 25 26  
  7. 27 28 29 30 31   
复制代码
如上是2013年一月的日历
如果要取2013年第2周的星期一 , 楼主心目中是哪个日期?是1月7号吗?
如果要取2013年第1周的星期一 ,  楼主是否有要求到2012年去取?

作者: reyleon    时间: 2013-08-17 10:45
诚如@cao627所说,如果要取2013年第1周的星期一 ,  楼主是否有要求到2012年去取?我感觉这个是关键!!! @plumdavid楼主,你说呢?
作者: plumdavid    时间: 2013-08-19 08:29
回复 13# reyleon


   

    对啊,2013年的第1周的星期一,应该是2012-12-31
作者: plumdavid    时间: 2013-08-19 08:29
回复 12# cao627


   

    对,2013年的第1周的星期一,应该是2012-12-31
作者: cao627    时间: 2013-08-19 19:57
  1. $ y=2013
  2. $ w=32
  3. $ a=`for i in {1..12}; do cal  $i $y; done | awk -vy=$y -vw=$w '($2 ~ y){m=$0;next}NR>=4{for(i=1;i<=NF;i++)if($i ~ /[0-9]/){d++;if(d>7*(w-2) && d<=7*(w-1) && $2 && !f){print m" "$2;f=1}}}'`
  4. $ echo $a
  5. 5 August 2013
复制代码
当w=1,即一年中的第一周的时候,$a 为空,当w为一年中最后一周的时候,$a也可能为空

所以可以写个脚本,判断
@plumdavid
如果 w=1 ,判断 cal 1 $y 的第一周是否有6个以上字段,如果低于6个字段就取 cal 12 $(($y-1)) 最后一行的$2

如果 w>1 && $a 为空 ,取cal 1 $(($y+1)) 第4行的$2.





作者: plumdavid    时间: 2013-08-20 16:57
回复 16# cao627


    挺复杂啊,我想,应该有专有的命令能解决吧。
作者: jason680    时间: 2013-08-20 17:36
本帖最后由 jason680 于 2013-08-20 17:39 编辑

回复 17# plumdavid

Would you let's know what you want to do?
   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2