免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1601 | 回复: 6
打印 上一主题 下一主题

自己写的timet [复制链接]

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-24 11:08 |只看该作者 |倒序浏览
本帖最后由 grshrd49 于 2013-06-24 17:36 编辑

自己写了个timer

  1. use strict;
  2. use AnyEvent;

  3. my $cv = AnyEvent->condvar;
  4. my $w; $w = AnyEvent->timer(
  5.         after        => 1,
  6.         interval        => 1,
  7.         cb => sub {
  8.                 if (&nowtime("","","","","","23","")){
  9.                         &do1 ;
  10.                 }
  11.                 my $timeshow;
  12.                 print "$timeshow\n";
  13.         }
  14. );  
  15. $cv->recv;

  16. sub do1
  17. {
  18.         print "test1\n";
  19. }

  20. sub do2
  21. {
  22.         print "tast1 \n";
  23. }


  24. sub nowtime
  25. {       
  26.         #格式
  27.         #&nowtime("yyyy","mm","dd","hh","mm","ss","www")
  28.        
  29.         #<返回具体时间>
  30.         #传入1返回需要的具体时间 小时:分钟:秒 hh:mm:ss
  31.         #不传就不返回
  32.         #&nowtime("","","","1","1","1","")
  33.        
  34.         #<验证具体时间>
  35.         #传入具体时间,验证是否是到达当前时间
  36.         #如果是5点1分要传入05 01 , 否则会当作<返回具体时间>处理
  37.         #返回1是当前时间,返回0不是当前时间
  38.         #&nowtime("","","","14","15","44","")
  39.        
  40.         my @need_return = @_;
  41.         my %month = (
  42.                 "Jan" => "01","Feb" => "02","Mar" => "03","Apr" => "04",
  43.                 "May" => "05","Jun" => "06","Jul" => "07","Aug" => "08",
  44.                 "Sep" => "09","Oct" => "10","Nov" => "11","Dec" => "12",
  45.         );
  46.        
  47.         my $local_time = localtime();
  48.         my @date = split /\s+/,$local_time;
  49.         $date[2] = sprintf "%02d",$date[2];
  50.         my @time = split /:/,$date[3];
  51.         my @now_time = ($date[4],$month{$date[1]},$date[2],$time[0],$time[1],$time[2],$date[0]);
  52.        
  53.         #全是空值直接返回0
  54.         my $i = 0;
  55.         for(@need_return){
  56.                 if( $_ eq "" ){
  57.                         $i++;
  58.                         return 0 if( $i == 7 );
  59.                         next;       
  60.                 }
  61.                 if( $_ == 1 ){
  62.                         #有一个参数是 1 说明要返回具体时间
  63.                         my $return_time;
  64.                         $return_time = $return_time . $date[4] if ( $need_return[0] ne "" );                        #年
  65.                         $return_time = $return_time . "-" if ( $need_return[0] ne "" && $need_return[1] ne "");        #-
  66.                         $return_time = $return_time . $month{$date[1]} if ( $need_return[1] ne "" );        #月
  67.                         $return_time = $return_time . "-" if ( $need_return[1] ne "" && $need_return[2] ne "");        #-
  68.                         $return_time = $return_time . $date[2] if ( $need_return[2] ne "" );                        #日
  69.                         $return_time = $return_time . " " if ( $need_return[2] ne "" && $need_return[3] ne "");        #空格
  70.                         $return_time = $return_time . $time[0] if ( $need_return[3] ne "" );                        #时
  71.                         $return_time = $return_time . ":" if ( $need_return[3] ne "" && $need_return[4] ne "");        #:
  72.                         $return_time = $return_time . $time[1] if ( $need_return[4] ne "" );                        #分
  73.                         $return_time = $return_time . ":" if ( $need_return[4] ne "" && $need_return[5] ne "");        #:
  74.                         $return_time = $return_time . $time[2] if ( $need_return[5] ne "" );                        #秒
  75.                         $return_time = $return_time . " " if ( $need_return[5] ne "" && $need_return[6] ne "");        #空格
  76.                         $return_time = $return_time . $date[0] if ( $need_return[6] ne "" );                        #周
  77.                         return $return_time;
  78.                 }else{
  79.                         #否则比对时间
  80.                         my $n = 0;
  81.                         for(@need_return){
  82.                                 if( $_ ne "" ){
  83.                                         return 0 if( $_ ne $now_time[$n]);
  84.                                 }
  85.                                 $n++;
  86.                         }
  87.                         return 1;
  88.                 }
  89.         }
  90. }
复制代码

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
2 [报告]
发表于 2013-06-26 02:04 |只看该作者
这个timer要看多少书才知道写阿~

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
3 [报告]
发表于 2013-06-26 09:14 |只看该作者
小骆驼就足够拉
主要还是AnyEvent这个模块,就直接拿文档上的例子过来用的,其实还不是很了解AnyEvent内部的是则么实现的...
以前用for做的timer ,哈哈你懂的呀

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2013-06-26 09:27 |只看该作者
AnyEvent很适合拿来就用,没必要知道“内部是怎么实现的",就像买了一车,首先是学会怎么开好了,这一步会花不少时间。
AnyEvent是很强大,但同时学习成本也相对高。随着你对AnyEvent和周边模块的理解加深,“内部是怎么实现的”也就慢慢明白了

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
5 [报告]
发表于 2013-06-26 09:31 |只看该作者
回复 4# py

学习是充满乐趣的事情,不是嘛!!哈哈
   

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
6 [报告]
发表于 2013-06-27 02:03 |只看该作者
大神, 他在哪里用?
这个timer它的用法 ?

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
7 [报告]
发表于 2013-06-27 09:20 |只看该作者
  1. if (&nowtime("","","","","","23","")){
  2.     &do1 ;
  3. }
复制代码
在nowtime("","","","","","23","")中添加时间,
比如每天凌晨1点 nowtime("","","","01","00","00",""),时间一到就会去执行do1这个函数
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP