2012-03-30 8 views
0

現在の日付でSQLスクリプトファイルを更新するための支援が必要です。例えば。 Pythonから。 SQLスクリプトには、from-> to dateフィルタがあります。今月の記録を取得する。私はLinuxマシン上のCRONジョブからこのスクリプトを実行します。また、日付を自動的に更新する必要があります。Pythonで現在の日付のスクリプトを更新する

どのようにですか?

;SQL script text to update. Before running sqlite3 shell command. 
... 
and date(tc1.time,'unixepoch') 
    BETWEEN date('2012-03-01','start of month') 
    and date('2012-04-01','start of month','+1 month','-1 day') 
and date(tc2.time,'unixepoch') 
BETWEEN date('2012-03-01','start of month') 
    and date('2012-04-01','start of month','+1 month','-1 day') 

日付システムの日付が2012-03-30の場合。

>date +"%Y-%m-%d" 
2012-03-30 

私は2012-03-01から2012-04-01に行きたいと思っています。日付が2012-04-20または2012-04-21の場合は、2012-04-01から2012-05-01までの出力を希望します。

提案がありますか?

答えて

1

これはトリックを行う必要があります。

import datetime 
d=datetime.date.today() 
start_current_month=datetime.date(d.year,d.month,1) 

if d.month==12: 
    new_year=d.year+1 
    new_month=1 
else: 
    new_year=d.year 
    new_month=d.month+1 

start_next_month=datetime.date(new_year,new_month,1) 

str_start_current_month=start_current_month.isoformat() 
str_start_next_month=start_next_month.isoformat() 

str_start_current_month=start_current_month.isoformat() 
str_start_next_month=start_next_month.isoformat() 
関連する問題