1、安装
brew install python
2、配置 环境变量
·打开
sudo vi ~/.bash_profile
·添加
export PATH="/opt/homebrew/bin:$PATH"
·重载
source ~/.bash_profile
✗ source ~/.zshrc # 按需
3、验证
·方式 1
ls -l /opt/homebrew/bin/python3
·方式 2
echo $PATH
· 方式 3
/opt/homebrew/bin/python3 --version
4、shell 运行 python
/opt/homebrew/bin/python3 /path/to/your/script/demo.py
- 方法 1
#!/bin/bash
# 定义函数
process_timestamp() {
timestamp=$1
formatted_date=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y-%m-%d %H'))")
id=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y%m%d%H'))")
echo "timestamp: $timestamp"
echo "id: $id"
echo "Formatted Date: $formatted_date"
}
# 获取当前时间,格式为 yyyy-MM-dd HH:mm:ss
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前时间:$current_time"
begin_timestamp=$(python3 -c "from datetime import datetime, timezone; print(int(datetime.now(timezone.utc).replace(minute=0, second=0, microsecond=0). timestamp()))")
begin_local_time=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($begin_timestamp).strftime('%Y-%m-%d %H'))")
echo "begin_timestamp: $begin_timestamp"
echo "begin_local_time: $begin_local_time"
for ((i=0; i<=3; i++)); do
timestamp=$((begin_timestamp - 3600*i))
process_timestamp $timestamp
done
- 方法 2
#!/bin/bash
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前时间:$current_time"
begin_timestamp=$(python3 -c "from datetime import datetime, timezone; print(int(datetime.now(timezone.utc).replace(minute=0, second=0, microsecond=0). timestamp()))")
begin_local_time=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($begin_timestamp).strftime('%Y-%m-%d %H'))")
echo "begin_timestamp: $begin_timestamp"
echo "begin_local_time: $begin_local_time"
for ((i=0; i<=3; i++)); do
timestamp=$((begin_timestamp - 3600*i))
formatted_date=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp(int('$timestamp')).strftime('%Y-%m-%d %H'))")
id=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp(int('$timestamp')).strftime('%Y%m%d%H'))")
echo "timestamp: $timestamp"
echo "id: $id"
echo "Formatted Date: $formatted_date"
done
结果
当前时间:2024-04-26 19:04:50
begin_hour: 11
begin_utc_time: 2024-04-26 11
begin_timestamp: 1714100400
timestamp: 1714100400
id: 2024042611
Formatted Date: 2024-04-26 11
timestamp: 1714096800
id: 2024042610
Formatted Date: 2024-04-26 10
timestamp: 1714093200
id: 2024042609
Formatted Date: 2024-04-26 09
timestamp: 1714089600
id: 2024042608
Formatted Date: 2024-04-26 08
5、拓展
5.1、倒叙打印 小时、天、周、月 的开始和结束时间
5.1.0、批量执行脚本
#!/bin/bash
# 保存要执行的脚本文件名到一个数组中
scripts=(
"datetest_hour.sh"
"datetest_day.sh"
"datetest_week.sh"
"datetest_month.sh"
)
# 遍历脚本数组,逐个执行脚本
for script in "${scripts[@]}"
do
echo "执行脚本: $script"
bash "$script"
echo "脚本执行完成: $script"
done
5.1.1、按小时打印开始和结束
#!/bin/bash
number=3
# 1小时
timeSeconds=3600
# 定义函数
process_timestamp() {
timestamp=$1
next_hour_timestamp=$((timestamp + $timeSeconds))
timestampStr=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y-%m-%d %H'))")
id=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y%m%d%H'))")
dateIndex=$(python3 -c "from datetime import datetime, timezone; print(datetime.fromtimestamp($timestamp, timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'))")
current_utc_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "id: $id"
echo "timestampStr: $timestampStr"
echo "timestamp(s):$timestamp"
echo "dateIndex: $dateIndex"
echo "next_hour_timestamp(s):$next_hour_timestamp"
echo "current_utc_time:$current_utc_time"
}
# 获取当前时间,格式为 yyyy-MM-dd HH:mm:ss
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前时间:$current_time"
begin_timestamp=$(python3 -c "from datetime import datetime, timezone; print(int(datetime.now(timezone.utc).replace(minute=0, second=0, microsecond=0). timestamp()))")
begin_local_time=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($begin_timestamp).strftime('%Y-%m-%d %H'))")
echo "begin_timestamp: $begin_timestamp"
echo "begin_local_time: $begin_local_time"
# 按小时循环-最近 n 个小时
for ((i=0; i<=$number; i++)); do
timestamp=$((begin_timestamp - $timeSeconds*i))
process_timestamp $timestamp
done
5.1.2、按天打印开始和结束
#!/bin/bash
number=3
# 1小时
timeSeconds=$((3600 * 24))
# 定义函数
process_timestamp() {
timestamp=$1
next_hour_timestamp=$((timestamp + $timeSeconds))
timestampStr=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y-%m-%d'))")
id=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y%m%d'))")
dateIndex=$(python3 -c "from datetime import datetime, timezone; print(datetime.fromtimestamp($timestamp, timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'))")
current_utc_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "id: $id"
echo "timestampStr: $timestampStr"
echo "timestamp(s):$timestamp"
echo "dateIndex: $dateIndex"
echo "next_hour_timestamp(s):$next_hour_timestamp"
echo "current_utc_time:$current_utc_time"
}
# 获取当前时间,格式为 yyyy-MM-dd HH:mm:ss
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前时间:$current_time"
begin_timestamp=$(python3 -c "from datetime import datetime, timezone; print(int(datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0).timestamp()))")
begin_local_time=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($begin_timestamp).strftime('%Y-%m-%d'))")
echo "begin_timestamp: $begin_timestamp"
echo "begin_local_time: $begin_local_time"
# 按小时循环-最近 n 个小时
for ((i=0; i<=$number; i++)); do
timestamp=$((begin_timestamp - $timeSeconds*i))
process_timestamp $timestamp
done
5.1.3、按周打印开始和结束
#!/bin/bash
number=3
# 1小时
timeSeconds=$((3600 * 24 * 7))
# 定义函数
process_timestamp() {
timestamp=$1
next_hour_timestamp=$((timestamp + $timeSeconds))
timestampStr=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y-%m-%d'))")
id=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y%m%d'))")
dateIndex=$(python3 -c "from datetime import datetime, timezone; print(datetime.fromtimestamp($timestamp, timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'))")
current_utc_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "id: $id"
echo "timestampStr: $timestampStr"
echo "timestamp(s):$timestamp"
echo "dateIndex: $dateIndex"
echo "next_hour_timestamp(s):$next_hour_timestamp"
echo "current_utc_time:$current_utc_time"
}
# 获取当前时间,格式为 yyyy-MM-dd HH:mm:ss
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前时间:$current_time"
begin_timestamp=$(python3 -c "from datetime import datetime, timedelta, timezone; today = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0); monday = today - timedelta(days=today.weekday()); begin_time = monday.replace(hour=0, minute=0, second=0); print(int(begin_time.timestamp()))")
begin_local_time=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($begin_timestamp).strftime('%Y-%m-%d'))")
echo "begin_timestamp: $begin_timestamp"
echo "begin_local_time: $begin_local_time"
# 按小时循环-最近 n 个小时
for ((i=0; i<=$number; i++)); do
timestamp=$((begin_timestamp - $timeSeconds*i))
process_timestamp $timestamp
done
5.1.4、按月打印开始和结束
#!/bin/bash
number=3
# 定义函数
process_timestamp() {
timestamp=$1
next_hour_timestamp=$2
timestampStr=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y-%m-%d'))")
id=$(python3 -c "from datetime import datetime; print(datetime.fromtimestamp($timestamp).strftime('%Y%m%d'))")
dateIndex=$(python3 -c "from datetime import datetime, timezone; print(datetime.fromtimestamp($timestamp, timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'))")
current_utc_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "id: $id"
echo "timestampStr: $timestampStr"
echo "timestamp(s): $timestamp"
echo "dateIndex: $dateIndex"
echo "next_hour_timestamp(s): $next_hour_timestamp"
echo "current_utc_time: $current_utc_time"
}
# 获取当前时间,格式为 yyyy-MM-dd HH:mm:ss
current_time=$(date +"%Y-%m-%d %H:%M:%S")
echo "当前时间:$current_time"
# 按小时循环-最近 n 个小时
for ((index=-1; index<=$number; index++)); do
if [ $index -eq -1 ]; then
begin_timestamp=$(python3 -c "
from datetime import datetime, timezone
current_month = datetime.now(timezone.utc).replace(day=1, hour=0, minute=0, second=0, microsecond=0)
begin_timestamp = int(current_month.timestamp())
print(begin_timestamp)
")
else
begin_timestamp=$(python3 -c "
from datetime import datetime, timedelta, timezone, time
current_date = datetime.now()
first_day = current_date.replace(day=1).strftime('%Y-%m-%d')
first_day_dt = datetime.strptime(first_day, '%Y-%m-%d')
previous_100th_month_first_day = first_day_dt.replace(day=1) - timedelta(days=1)
for _ in range($index):
previous_100th_month_first_day = previous_100th_month_first_day.replace(day=1) - timedelta(days=1)
previous_100th_month_first_day = previous_100th_month_first_day.replace(day=1) # 将日期设置为月份的第一天
timestamp = int(previous_100th_month_first_day.timestamp())
print(timestamp)
")
fi
end_timestamp=$(python3 -c "
from datetime import datetime, timedelta, timezone
# 转换为日期对象
current_date = datetime.fromtimestamp($begin_timestamp)
if current_date.month == 12:
next_month = current_date.replace(year=current_date.year + 1, month=1, day=1)
else:
next_month = current_date.replace(month=current_date.month + 1, day=1)
next_month_timestamp = int(next_month.timestamp())
print(next_month_timestamp)
")
# 调用 process_timestamp 函数进行处理
process_timestamp $begin_timestamp $end_timestamp
done

被折叠的 条评论
为什么被折叠?



