Actionscript 3 - 解析yyyy-mm-dd hh:mm:ss到Date对象的最快方法?

我一直在试图找到一种真正快速的方法来将yyyy - mm - dd [hh:mm:ss]解析到Date对象中。以下是我尝试过的3种方法,以及每种方法解析50,000个日期时间字符串的时间。

有没有人知道这样做的更快的方法或提示加快方法?

castMethod1 takes 3673 ms 
castMethod2 takes 3812 ms 
castMethod3 takes 3931 ms

码:

private function castMethod1(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    var year:int = int(dateString.substr(0,4));
    var month:int = int(dateString.substr(5,2))  -  1;
    var day:int = int(dateString.substr(8,2));

    if ( year == 0 && month == 0 && day == 0 ) {
        return null;
    }

    if ( dateString.length == 10 ) {
        return new Date(year, month, day);
    }

    var hour:int = int(dateString.substr(11,2));
    var minute:int = int(dateString.substr(14,2));
    var second:int = int(dateString.substr(17,2));

    return new Date(year, month, day, hour, minute, second);
}

-

private function castMethod2(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    if ( dateString.indexOf("0000  -  00  -  00") !=   -  1 ) {
        return null;
    }

    dateString = dateString.split("  -  ").join("/");

    return new Date(Date.parse( dateString ));
}

-

private function castMethod3(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    var mainParts:Array = dateString.split(" ");
    var dateParts:Array = mainParts[0].split("  -  ");

    if ( Number(dateParts[0])+Number(dateParts[1])+Number(dateParts[2]) == 0 ) {
        return null;
    }

    return new Date( Date.parse( dateParts.join("/")+(mainParts[1]?" "+mainParts[1]:" ") ) );
}

No, Date.parse will not handle dashes by default. And I need to return null for date time strings like "0000 - 00 - 00".

0
额外 编辑
意见: 4

7 答案

我猜Date.Parse()不起作用?

0
额外
不,它不! !
额外 作者 Marci-man,

那么方法2似乎是最好的方法:

private function castMethod2(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    if ( dateString.indexOf("0000-00-00") != -1 ) {
        return null;
    }

    dateString = dateString.split("-").join("/");

    return new Date(Date.parse( dateString ));
}
0
额外

我一直在使用下面的snipplet来解析UTC日期字符串:

private function parseUTCDate( str : String ) : Date {
    var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)Z/);

    var d : Date = new Date();

    d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));
    d.setUTCHours(int(matches[4]), int(matches[5]), int(matches[6]), 0);

    return d;
}

只要删除时间部分,它应该适合您的需求:

private function parseDate( str : String ) : Date {
    var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d)/);

    var d : Date = new Date();

    d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));

    return d;
}

No idea about the speed, I haven't been worried about that in my applications. 50K iterations in significantly less than a second on my machine.

0
额外
请告诉我们对于3个示例函数花了多长时间进行50K次迭代,因此我们可以将您的“不到一秒钟”放在视图中。
额外 作者 bzlm,
我想知道如果你用const关键字替换var和d,会不会有额外的表现。
额外 作者 UnknownJoe,

这是我在经过一番捣鼓之后能够达到的最快速度:

private function castMethod4(dateString:String):Date {          
    if ( dateString == null ) 
        return null;    
    if ( dateString.length != 10 && dateString.length != 19) 
        return null;

    dateString = dateString.replace("-", "/");
    dateString = dateString.replace("-", "/");

    return new Date(Date.parse( dateString ));
}

我在计算机上为castMethod2()在我的计算机上获得了大约470ms的迭代次数,而在我的版本中获得了300ms的迭代次数(这与63%的时间完成的工作量相同)。我肯定会说,除非你解析愚蠢的日期,否则两者都是“足够好”。

0
额外

因为Date.parse()不接受所有可能的格式,所以我们可以使用DataFormat()可以理解的formatString使用DateFormatter预先格式化传递的dateString值,例如

// English formatter
var stringValue = "2010.10.06"
var dateCommonFormatter : DateFormatter = new DateFormatter();
dateCommonFormatter.formatString = "YYYY/MM/DD";

var formattedStringValue : String = dateCommonFormatter.format(stringValue);                                
var dateFromString : Date = new Date(Date.parse(formattedStringValue));
0
额外
我们可以使用 DateFormatter.parseDateString()
额外 作者 approxiblue,

这是我的实现。试试这个。

public static function dateToUtcTime(date:Date):String {
  var tmp:Array = new Array();
  var char:String;
  var output:String = '';

  // create format YYMMDDhhmmssZ
  // ensure 2 digits are used for each format entry, so 0x00 suffuxed at each byte

  tmp.push(date.secondsUTC);
  tmp.push(date.minutesUTC);
  tmp.push(date.hoursUTC);
  tmp.push(date.getUTCDate());
  tmp.push(date.getUTCMonth() + 1); // months 0-11
  tmp.push(date.getUTCFullYear() % 100);


  for(var i:int=0; i < 6/* 7 items pushed*/; ++i) {
    char = String(tmp.pop());
    trace("char: " + char);
    if(char.length < 2)
      output += "0";
    output += char;
  }

  output += 'Z';

  return output;
}
0
额外
var strDate:String = "2013-01-24 01:02:40";

function dateParser(s:String):Date{
    var regexp:RegExp = /(\d{4})\-(\d{1,2})\-(\d{1,2}) (\d{2})\:(\d{2})\:(\d{2})/;
    var _result:Object = regexp.exec(s);

    return new Date(
        parseInt(_result[1]),
        parseInt(_result[2])-1,
        parseInt(_result[3]),
        parseInt(_result[4]),
        parseInt(_result[5]),
        parseInt(_result[6])
    );
}

var myDate:Date = dateParser(strDate);
0
额外