2012-08-24 39 views
1

我在這個formart中有一個日期:Wdy, DD-Mon-YY HH:MM:SS GMT,我需要解析並將其轉換爲struct std.date.Date或同等至。我通過使用split()的調用寫了一個非常簡單的函數(請查看我的代碼下面)。問題是性能 - 我正在尋找一個更快的算法來做到這一點。在分割令牌後,我使用to!int(tok)將其轉換爲int,然後放入結構中。有沒有最好的方法來做到這一點?內置D功能非常感謝!一些外部模塊(如果不是太複雜)。解析日期格式:「Wdy,DD-Mon-YY HH:MM:SS GMT」以D語言結構日期

注意:我是一個以D編程語言開始的C程序員。所以,你會看到一些C路解決方案。

struct MyDate { 
    int day; 
    int month; 
    int year; 
    int hour; 
    int minute; 
    int second; 
    //int ms; 

}; 

int index_of_month(string mont) 
{ 

    immutable string[] months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; 

    for(int i = 0, len = months.length; i < len; ++i) 
    if(months[i] == mont) 
     return i; 

    return -1; 
} 

void main() 
{ 
    //date format Wdy, DD-Mon-YY HH:MM:SS GMT 
    string d = "Sun, 24-Aug-2014 15:00:24 GMT"; 
    string[] parts = split(d, " "); 
    string[] _date = split(parts[1], "-"); 
    string[] _hour = split(parts[2], ":"); 

    debug { 
    for(int i = 0; i < parts.length; ++i) 
     writefln("[%d]: %s", i, parts[i]); 

    } else { 

    //Handling erros goes here 
    //... 

    //I write my onw date struct because 
    // std.date.Date cannot be compiled 
    // even by using -d flag; and std.datetime.Date is not equivalent to. 
    // NOTE: I think that I will have problems when try to compare it 
    // to Clock.currTime().. 
    MyDate date; 

    //as you can seed below, I did a lot of 
    // string to integer conversion; 
    // I don't know much aboud D, but I think 
    // that it's so slow. I'm looking for a best 
    // solution(in perfomance questions). 
    date.day = to!int(_date[0]); // 0 .. 31 min/max 2 digits. 

    //I know about enum Month from std.datatime; 
    // but there is no(as far as I know) equivalent to Enum.GetValues()/Enum.GetNames() 
    // of C#.NET and .indexOf(), so I write my onw implemantation... 
    date.month = index_of_month(toLower(_date[1])); // 1 .. 12 min/max 2 digits. 

    //maybe do it by using e.g(C-way), 
    // int n = 0; n = (n * 10) + (_date[2][0] - '0'); n = (n * 10) + (_date[2][0] - '0'); and so.. 
    // and lave the compiler do promotion 
    // can be better? 
    date.year = to!int(_date[2]); // min 2, max 4 digits. 
    date.hour = to!int(_hour[0]); // 0 .. 23 min/max 2 digits. 
    date.minute = to!int(_hour[1]); // 0 .. 59 min/max 2 digits. 
    date.second = to!int(_hour[2]); // 0 .. 59 min/max 2 digits. 

    writefln("date.day = %d\n" 
     "date.month = %d\n" 
     "date.year = %d\n" 
     "date.hour = %d\n" 
     "date.minute = %d\n" 
     "date.second = %d\n", 
     date.day, date.month, 
     date.year, date.hour, 
     date.minute, date.second); 


    } 

} 

而且,當然,任何改善此代碼的建議都非常值得讚賞。提前致謝。

+2

您是否知道std.date已被棄用且存在更好的std.datetime? (http://dlang.org/phobos/std_datetime.html) –

回答

2

這就是我想出:

import std.stdio; 
import std.format; 
import std.string; 
import std.datetime; 
import std.algorithm; 

enum MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; 

int month_to_index(string month) { 
    return cast(int)MONTHS.countUntil(month.toLower()); 
} 

void main() { 
    string date = "Sun, 24-Aug-2014 15:00:24 GMT"; 

    string a; 
    int day; 
    string month; 
    int year; 
    int hour; 
    int minute; 
    int second; 
    string tz; 

    date.formattedRead("%s, %d-%s-%d %d:%d:%d %s", &a, &day, &month, &year, &hour, &minute, &second, &tz); 

    DateTime dt = DateTime(year, month.month_to_index(), day, hour, minute, second); 

    writeln(dt); 
} 

std.datetime確實需要某種strptimestrftime

+0

「std.datetime真的需要某種strptime和strftime」它在todo列表中,但確切的設計還沒有被整理出來,其他的東西有優先級更高,所以還沒有發生。它會在某些時候。 –