Chinaunix首页 | 论坛 | 博客
  • 博客访问: 986819
  • 博文数量: 150
  • 博客积分: 3017
  • 博客等级: 少校
  • 技术积分: 3829
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-19 14:40
个人简介

Now in Baidu WISE team

文章分类

全部博文(150)

文章存档

2014年(8)

2013年(31)

2012年(111)

分类: Python/Ruby

2012-03-28 10:35:55

When I'm trying to sort mails I got, there is only date in RFC3501, such as 'Thu, 15 Mar 2012 15:46:10 +0800'. I don't want to impor too many modules, so I write this. Just a prototype. And need to handle the timezone and time with only single digit.

  1. #!/usr/bin/perl -w
  2. #

  3. sub ConvertRFC3501toUnixTime{
  4.     my $input = shift;


  5.     #Thu, 15 Mar 2012 15:46:10 +0800
  6.     my %months = ("Jan" => 1,"Feb" => 2,"Mar" => 3,"Apr" => 4,"May" => 5,"Jun" => 6,"Jul" => 7,"Aug" => 8,"Sep" => 9,"Oct" => 10,"Nov" => 11,"Dec" => 12);
  7.     my($day,$mon,$year,$hour,$min,$sec) = $input =~ /(\d{2}) (\w{3}) (\d{4}) (\d{2}):(\d{2}):(\d{2})/;
  8.   
  9.     my $time = $sec.$min.$hour.$day.($months{$mon}-1).($year - 1900);
  10.     return $time;
  11. }





  12. my $str = 'Thu, 28 Mar 2012 15:46:10 +0800';
  13. print localtime;
  14. print "\n";
  15. print ConvertRFC3501toUnixTime($str);


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