Обновление скрипта с текущей датой в python - PullRequest
0 голосов
/ 30 марта 2012

Мне нужна помощь в обновлении файла сценария SQL с текущей датой.Например, из питона.В сценарии SQL у меня есть фильтр с даты на дату.Чтобы получить записи этого месяца.Я буду запускать этот скрипт из задания CRON на машине с Linux.И нужно автоматически обновлять даты от и до.

Как?

;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 Ответ

1 голос
/ 30 марта 2012

Это должно сработать:

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()
...