hi
I want to convert Jalali date to Gregorian and display it on lcd
But I don’t know how to do it
ESPHome
سلام خدمت همه فارسی زبانان
تبدیل تاریخ میلادی به جلالی و نمایش آن بر روی نمایشگر
esphome:
name: persian_calender
on_boot:
then:
- ds1307.read_time:
- script.execute: update_shamsi_date
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
logs:
component: none
# Enable Home Assistant API
api:
reboot_timeout: 0s
ota:
- platform: esphome
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
reboot_timeout: 0s
time:
- platform: sntp #homeassistant
id: my_time
on_time_sync:
then:
# ... and update the RTC when the synchronization was successful
- ds1307.write_time:
- script.execute: update_shamsi_date
on_time:
- seconds: 0
minutes: 0
hours: 0
then:
- script.execute: update_shamsi_date
- seconds: /1
then:
- lambda: |-
id(secbit) = ! id(secbit);
- platform: ds1307
id: ds1307_time
i2c_id: i2c_clock
update_interval: never
globals:
- id: secbit
type: bool
initial_value: 'true'
# Example configuration entry with 2 sensors and filter
sensor:
- platform: template
name: "ShamsiYear"
id: shamsi_year
- platform: template
name: "Shamsimonth"
id: shamsi_month
- platform: template
name: "Shamsi Day"
id: shamsi_day
script:
- id: update_shamsi_date
then:
- lambda: |-
time_t curr_time = id(my_time).now().timestamp;
struct tm *timeinfo = localtime(&curr_time);
int gy = timeinfo->tm_year + 1900;
int gm = timeinfo->tm_mon + 1;
int gd = timeinfo->tm_mday;
auto gregorian_to_jalali = [](int gy, int gm, int gd, int &jy, int &jm, int &jd) {
int g_d_m[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
jy = (gy <= 1600) ? 0 : 979;
gy -= (gy <= 1600) ? 621 : 1600;
int gy2 = (gm > 2) ? (gy + 1) : gy;
int days = (365 * gy) + ((gy2 + 3) / 4) - ((gy2 + 99) / 100) + ((gy2 + 399) / 400) - 80 + gd + g_d_m[gm - 1];
jy += 33 * (days / 12053);
days %= 12053;
jy += 4 * (days / 1461);
days %= 1461;
jy += (days - 1) / 365;
if (days > 365) days = (days - 1) % 365;
jm = (days < 186) ? 1 + (days / 31) : 7 + ((days - 186) / 30);
jd = 1 + ((days < 186) ? (days % 31) : ((days - 186) % 30));
};
int j_year, j_month, j_day;
gregorian_to_jalali(gy, gm, gd, j_year, j_month, j_day);
char shamsi_date[22];
snprintf(shamsi_date, sizeof(shamsi_date), "%04d/%02d/%02d", j_year, j_month, j_day);
id(shamsi_date_text).publish_state(shamsi_date);
id(shamsi_year).publish_state(j_year);
id(shamsi_month).publish_state(j_month);
id(shamsi_day).publish_state(j_day);
ESP_LOGD("custom", "Gregorian Date: %04d-%02d-%02d", gy, gm, gd);
ESP_LOGD("custom", "Shamsi Date: %s", shamsi_date);
text_sensor:
- platform: template
name: "Shamsi Date"
id: shamsi_date_text
icon: "mdi:calendar"
i2c:
- id: i2c_disp
sda: 18
scl: 5
# scan: false
- id: i2c_clock
sda: 22
scl: 23
# # scan9 false
display:
- platform: ssd1306_i2c
id: oled1
i2c_id: i2c_disp
update_interval: 0.5s
contrast: 1
model: "SH1106 128x64"
rotation: 0
auto_clear_enabled: true
#scan_interval: 1000
address: 0x3C
lambda: |-
it.printf(0, 6, id(my_font1), "%s", id(shamsi_date_text).state.c_str());
if (id(secbit)) {
it.strftime(it.get_width()/2, 26, id(my_font), TextAlign::TOP_CENTER, "%H %M", id(my_time).now());
}
else {
it.strftime(it.get_width()/2, 26, id(my_font), TextAlign::TOP_CENTER, "%H : %M", id(my_time).now());
}
font:
- file: "_fonts/BTitr-Bold.ttf" #"_fonts/TAHOMAB0.ttf"
id: my_font
size: 45
- file: "_fonts/BTitr-Bold.ttf"
id: my_font1
size: 20