2011-02-02 8 views
1

書式設定の日付に問題があります。Pythonの書式datetimeの問題

File "c:\Python27\Lib\_strptime.py", line 322, in _strptime 
    found = format_regex.match(data_string) 

これの何が問題なのです。私は例外を取得私の最後の行で

myDate=datetime.datetime.now() 
print myDate #2011-02-02 13:54:26.162000 
stime = time.mktime(time.strptime(myDate, '%Y-%m-%d %I:%M%S.%f')) 

:これは私のコードですか?私は大人のための誕生のランダムな日付を取得しようとしている

import random 
import time 

def strTimeProp(start, end, format, prop): 
    """Get a time at a proportion of a range of two formatted times. 

    start and end should be strings specifying times formated in the 
    given format (strftime-style), giving an interval [start, end]. 
    prop specifies how a proportion of the interval to be taken after 
    start. The returned time will be in the specified format. 
    """ 

    stime = time.mktime(time.strptime(start, format)) 
    etime = time.mktime(time.strptime(end, format)) 

    ptime = stime + prop * (etime - stime) 

    return time.strftime(format, time.localtime(ptime)) 


def randomDate(start, end, prop): 
    return strTimeProp(start, end, '%Y-%m-%d %H:%M:%S.%f', prop) 

b1=datetime.datetime.now() 
print b1 
startDate=b1-datetime.timedelta(27375) 
print startDate 
endDate=b1-datetime.timedelta(6571) 
print endDate 
randomDate=randomDate(str(startDate), str(endDate), random.random()) 

EDIT をより明確にするために、これは私がstackoverflowの上で見つけるもののミックスである私の全体のコードです。

私はWindows XPを使用しています。

+1

エポックから秒数を取得しようとしていますか? – SilentGhost

+0

@user:私はあなたの許しを請う?だからあなたは何をしようとしているのですか? – SilentGhost

+0

申し訳ありませんが、私はあなたの質問を理解していない:)私は1936-02-21 14:16:18.936000のような古い日付を置くとき、私は範囲外のmktimeの引数を取得します。私はそれが時代に関連していることを読んだが、私の英語はそれを理解するために弱いです:/ – user278618

答えて

4

私は大人のためのランダムな生年月日を取得しようとしています。

OK、あなたは大人が18〜75歳であることができるよりも言うので、もし - その後、今日から非常に多くの日前であるの日付を見つけてみましょう、(6571 27375日):

from datetime import datetime, timedelta 
import random 

birthday = datetime.today() - timedelta(days = random.randrange(6571, 27375)) 

print 'Person was born on %s' % (birthday.strftime('%Y-%m-%d'))  
0

stime = time.mktime(time.strptime(str(myDate), '%Y-%m-%d %H:%M:%S.%f')) 
time.strptime()ニーズへの最初の引数は、文字列、しないようにあなたの最後の行を変更し、あなたが datetime.datetime.now()

から取り戻すdatetime対象となります

(あなたの編集のコードはdatetimeオブジェクトを文字列に変換します)

3

確かに、あなたはUnixエポックを超えているので、OverflowErrorをヒットします。簡単な解決策は、datetimeとtimedeltaオブジェクトを使用するようにコードをリファクタリングすることです。そこには日付範囲に問題はありません。私は自分自身でリファクタリングを行いました。それは動作しているように見えます(タイムゾーンの細かい部分は考慮していません)。

from datetime import datetime, timedelta 
import random 

def strTimeProp(start, end, format, prop): 
    """Get a time at a proportion of a range of two formatted times. 

    start and end should be strings specifying times formated in the 
    given format (strftime-style), giving an interval [start, end]. 
    prop specifies how a proportion of the interval to be taken after 
    start. The returned time will be in the specified format. 
    """ 

    sdatetime = datetime.strptime(start, format) 
    edatetime = datetime.strptime(end, format) 

    # get time delta and calculate new delta in days * prop 
    delta = edatetime - sdatetime # this is a timedelta 
    propdelta = timedelta(days = prop * delta.days) 

    pdatetime = sdatetime + propdelta 
    return pdatetime.strftime(format) 


def randomDate(start, end, prop): 
    return strTimeProp(start, end, '%Y-%m-%d %H:%M:%S.%f', prop) 

b1=datetime.now() 
print b1 
startDate=b1-timedelta(27375) 
print startDate 
endDate=b1-timedelta(6571) 
print endDate 
randomDate=randomDate(str(startDate), str(endDate), random.random()) 
print randomDate 

希望します。