Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1607584
  • 博文数量: 185
  • 博客积分: 10363
  • 博客等级: 上将
  • 技术积分: 2205
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-11 22:29
文章分类
文章存档

2013年(12)

2012年(5)

2011年(2)

2010年(12)

2009年(9)

2008年(15)

2007年(48)

2006年(82)

分类: LINUX

2010-11-18 16:20:07

这个脚本能够显示从2010年9月20日到今天之前一共会有哪些天,大家可以借鉴一下用perl处理unix时间戳的技巧~

 

#!/usr/local/perl5.12.2/bin/perl

#Author:

# Hyper Lau

#Website:

# http://heidern.cublog.cn

#File:

# unix_time.pl

#Create Date:

# Wed 17 Nov 2010 04:42:07 PM CST

use strict;
use warnings;
use Time::Local;
$|=1;
my $old_date_Y='2010';
my $old_date_M='09';
my $old_date_D='20';
# when you want to convert time to unixstamp,don't add 1 to month!!

my $old_date_unix=timelocal(0,0,0,$old_date_D,$old_date_M-1,$old_date_Y);
my $old_date=$old_date_Y.$old_date_M.$old_date_D;
my ($now_date_Y,$now_date_M,$now_date_D)=get_now_date();
my $now_date=$now_date_Y.$now_date_M.$now_date_D;
my $now_date_unix=get_now_date_unix();
while ($old_date_unix <= $now_date_unix) {
    print "now date is $now_date , unixstamp is $now_date_unix\n";
    print "old date is $old_date , unixstamp is $old_date_unix\n";
    print "processing ".get_time($old_date_unix)." data...\n";
    <STDIN>;
    print "process ok! \n";
    $old_date_unix+=86400;
}
print "===========ALL PROCESSED!!===========\n";

sub get_time {
# in unixstamp

# out "YYYY-MM-DD HH:mm:SS"

    my ($time)=@_;
    $time=time until (defined($time));
    my @time_format = (localtime($time))[5,4,3,2,1,0];
    $time_format[0] += 1900;
    $time_format[1] += 1;
    foreach $_(@time_format) {
        $_ = sprintf("%02d", $_);
    }
    my $cur_time = "$time_format[0]-$time_format[1]-$time_format[2] $time_format[3]:$time_format[4]:$time_format[5]";
    return($cur_time);
}
sub get_now_date {
# in normal

# out now date(YYYY-MM-DD)

    my @time_format = (localtime())[5,4,3];
    $time_format[0] += 1900;
    $time_format[1] += 1;
    foreach $_(@time_format) {
        $_ = sprintf("%02d", $_);
    }
    return($time_format[0],$time_format[1],$time_format[2]);
}
sub get_now_date_unix {
# in normal

#out unixstamp of now date(YYYY,MM,DD)

    my @time_format = (localtime())[5,4,3];
    $time_format[0] += 1900;
# WARNING!! when you want to convert time to unixstamp,don't add 1 to month!!

# $time_format[1] += 1;

    foreach $_(@time_format) {
        $_ = sprintf("%02d", $_);
    }
    my $cur_date_unix=timelocal(0,0,0,$time_format[2],$time_format[1],$time_format[0]);
    return($cur_date_unix);
}


阅读(2251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~