pythonmdate函数用法_Python calendar.mdays方法代码示例

# 需要导入模块: import calendar [as 别名]

# 或者: from calendar import mdays [as 别名]

def main():

months_out = 4

now = datetime.now()

dates = [now]

for i in range(months_out):

previous = dates[i]

next_date = previous + timedelta(calendar.mdays[(previous.month % 12) + 1])

dates.append(next_date)

if not os.path.exists(conf["visual_web_dir"]):

os.makedirs(conf["visual_web_dir"])

_static_web = os.path.join(conf["visual_web_dir"], "static")

if not os.path.exists(_static_web):

_static = os.path.join(TEMPLATES_PATH, "static")

os.symlink(_static, _static_web)

for _date in dates:

gen_time = "Allocation Map for %s-%.2d" % (_date.year, _date.month)

content = generator(

None, calendar.mdays[_date.month], _date.month, _date.year, gen_time

)

file_path = os.path.join(

conf["visual_web_dir"], "%s-%.2d.html" % (_date.year, _date.month)

)

with open(file_path, "w+") as _file:

_file.write(content)

os.chmod(file_path, 0o644)

_current = os.path.join(conf["visual_web_dir"], "current.html")

_next = os.path.join(conf["visual_web_dir"], "next.html")

if os.path.exists(_current):

os.remove(_current)

if os.path.exists(_next):

os.remove(_next)

current_path = os.path.join(

conf["visual_web_dir"], "%s-%.2d.html" % (dates[0].year, dates[0].month)

)

os.symlink(current_path, _current)

next_path = os.path.join(

conf["visual_web_dir"], "%s-%.2d.html" % (dates[1].year, dates[1].month)

)

os.symlink(next_path, _next)

files = [html for html in os.listdir(conf["visual_web_dir"]) if ".html" in html]

lines = []

for file in files:

if file != "current.html" and file != "next.html" and file != "index.html":

line = "%s\n
\n" % (file, file.split(".")[0])

lines.append(line)

index_path = os.path.join(conf["visual_web_dir"], "index.html")

with open(index_path, "w+") as index:

for line in lines:

index.write(line)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import time # 获取当前时间并格式化 current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 在终端输出当前时间 print("当前时间:", current_time) # 引入Tkinter库 import tkinter as tk # 创建一个窗口 window = tk.Tk() window.geometry('300x300') window.title('电子日历') # 在窗口中添加一个Label,显示当前日期和时间 current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) time_label = tk.Label(window, text=current_time) time_label.pack() # 添加一个按钮,点击后可以显示当天相隔的天数 def show_diff_days(): today = datetime.datetime.today() selected_day = calendar.selection_get() diff = (selected_day - today).days tk.messagebox.showinfo('相隔天数', f'距离今天相隔{diff}天') diff_btn = tk.Button(window, text='计算与今天相隔天数', command=show_diff_days) diff_btn.pack() # 在窗口中添加一个日历控件,让用户可以选择日期 import calendar import datetime from tkinter import messagebox as messagebox def show_selected_date(): selected_day = calendar.selection_get() messagebox.showinfo('选择日期', f'您选择了日期:{selected_day}') calendar = calendar.Calendar(window) calendar.pack() ok_btn = tk.Button(window, text='确认', command=show_selected_date) ok_btn.pack() # 运行窗口 window.mainloop() # 引入SQLite3库 import sqlite3 # 创建或连接到数据库 conn = sqlite3.connect('calendar.db') # 创建用户事件表格 conn.execute('''CREATE TABLE events (ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE TEXT NOT NULL, EVENT TEXT NOT NULL, REMIND_TIME TEXT);''') # 在表格中插入用户事件数据 def add_event(date, event, remind_time): conn.execute(f"INSERT INTO events (DATE,EVENT,REMIND_TIME) \ VALUES ('{date}','{event}','{remind_time}')") conn.commit()
最新发布
06-02
这段代码是一个完整的电子日历程序,包含了当前时间显示、日历控件、事件添加等功能,但是在添加事件的函数`add_event()`中,SQL语句直接使用了字符串拼接,存在SQL注入的风险,建议使用参数化查询的方式来避免这个问题: ```python # 在表格中插入用户事件数据 def add_event(date, event, remind_time): conn.execute("INSERT INTO events (DATE,EVENT,REMIND_TIME) VALUES (?,?,?)", (date, event, remind_time)) conn.commit() ``` 另外,这段代码在使用`messagebox`时,应该使用`tk.messagebox`来避免命名冲突: ```python from tkinter import messagebox as tk_messagebox def show_selected_date(): selected_day = calendar.selection_get() tk_messagebox.showinfo('选择日期', f'您选择了日期:{selected_day}') ``` 综上所述,以下是经过修改后的代码: ```python import time import tkinter as tk import datetime import calendar import sqlite3 from tkinter import messagebox as tk_messagebox # 获取当前时间并格式化 current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 在终端输出当前时间 print("当前时间:", current_time) # 创建一个窗口 window = tk.Tk() window.geometry('300x300') window.title('电子日历') # 在窗口中添加一个Label,显示当前日期和时间 current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) time_label = tk.Label(window, text=current_time) time_label.pack() # 添加一个按钮,点击后可以显示当天相隔的天数 def show_diff_days(): today = datetime.datetime.today().date() selected_day = calendar.selection_get() diff = (selected_day.date() - today).days tk_messagebox.showinfo('相隔天数', f'距离今天相隔{diff}天') diff_btn = tk.Button(window, text='计算与今天相隔天数', command=show_diff_days) diff_btn.pack() # 在窗口中添加一个日历控件,让用户可以选择日期 def show_selected_date(): selected_day = calendar.selection_get() tk_messagebox.showinfo('选择日期', f'您选择了日期:{selected_day}') calendar = calendar.Calendar(window) calendar.pack() ok_btn = tk.Button(window, text='确认', command=show_selected_date) ok_btn.pack() # 创建或连接到数据库 conn = sqlite3.connect('calendar.db') # 创建用户事件表格 conn.execute('''CREATE TABLE IF NOT EXISTS events (ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE TEXT NOT NULL, EVENT TEXT NOT NULL, REMIND_TIME TEXT);''') # 在表格中插入用户事件数据 def add_event(date, event, remind_time): conn.execute("INSERT INTO events (DATE,EVENT,REMIND_TIME) VALUES (?,?,?)", (date, event, remind_time)) conn.commit() # 运行窗口 window.mainloop() ``` 这段代码通过使用参数化查询,避免了SQL注入的问题,同时使用`tk_messagebox`来避免了命名冲突。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值