Storing time and date in postgre database based on local time in django project
I have a virtual server in Europe where a series of csv files are uploaded I am trying to use a script to read the information from csv and store it in the database(postgresql) My problem is when I read the date from the csv file, the date is based on Iran's time zone and I want it to be saved in the same way, but the time is saved based on utc this is a sample of date records in csv files
2024-09-12 08:00:00,....
and this is my command from read data from csv:
AnemometerData = apps.get_model("DataLoger", "AnemometerData")
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("from_date", type=str)
def handle(self, *args, **options):
self.read_csv(options["from_date"])
def validate_date(self, date):
try:
_date = datetime.strptime(date, "%Y-%m-%d").replace(minute=0, second=0, microsecond=0)
return _date
except Exception as e:
raise CommandError(e)
def read_csv(self, from_date):
from_date = self.validate_date(from_date)
base_path = settings.BASE_DIR
path = os.path.join(base_path, "DataLoger", "FTP")
iran_tz = pytz.timezone('Asia/Tehran')
current_time = datetime.now(iran_tz).replace(minute=0, second=0, microsecond=0, tzinfo=None)
while from_date <= current_time:
for id in ["G241156", "G241157"]:
try:
time_str = from_date.strftime("%Y%m%d_%H%M")
file_path = os.path.join(path, f'{id}_{time_str}.csv.gz')
df = pd.read_csv(file_path, compression='gzip',
header=0, sep=',', quotechar='"')
remove_columns = ['Anemometer 100m;wind_speed;Count', 'Anemometer 96m;wind_speed;Count',
'Anemometer 60m;wind_speed;Count', 'Anemometer 45m;wind_speed;Count',
'Wind Vane 94m;wind_direction;Count', 'Wind Vane 45m;wind_direction;Count',
'Hygro/Thermo 95m;humidity;Count', 'Hygro/Thermo 10m;humidity;Count',
'Hygro/Thermo 95m;temperature;Count', 'Hygro/Thermo 10m;temperature;Count',
'Barometer 2m;air_pressure;Count', 'Pyranometer 10m;solar_irradiance;Count',
'A1;Avg',
'A1;Max',
'A1;Min', 'A1;StdDev', 'A1;Count', 'A2;Avg', 'A2;Max', 'A2;Min', 'A2;StdDev',
'A2;Count',
'A3;Avg',
'A3;Max', 'A3;Min', 'A3;StdDev', 'A3;Count', 'A4;Avg', 'A4;Max', 'A4;Min',
'A4;StdDev',
'A4;Count',
'A5;Avg', 'A5;Max', 'A5;Min', 'A5;StdDev', 'A5;Count', 'A6;Avg', 'A6;Max',
'A6;Min',
'A6;StdDev',
'A6;Count', 'C1;Avg', 'C1;Max', 'C1;Min', 'C1;StdDev', 'C1;Count', 'C2;Avg',
'C2;Max',
'C2;Min',
'C2;StdDev', 'C2;Count', 'C3;Avg', 'C3;Max', 'C3;Min', 'C3;StdDev',
'C3;Count',
'C4;Avg',
'C4;Max',
'C4;Min', 'C4;StdDev', 'C4;Count', 'D1;Avg', 'D1;Max', 'D1;Min', 'D1;StdDev',
'D1;Count',
'D2;Avg',
'D2;Max', 'D2;Min', 'D2;StdDev', 'D2;Count', 'V;Avg', 'V;Max', 'V;Min',
'I;Avg',
'I;Max',
'I;Min',
'T;Avg', 'addr']
require_columns = ['time_stamp', 'Anemometer_100m_wind_speed_Avg',
'Anemometer_100m_wind_speed_Max',
'Anemometer_100m_wind_speed_Min', 'Anemometer_100m_wind_speed_StdDev',
'Anemometer_96m_wind_speed_Avg', 'Anemometer_96m_wind_speed_Max',
'Anemometer_96m_wind_speed_Min',
'Anemometer_96m_wind_speed_StdDev', 'Anemometer_60m_wind_speed_Avg',
'Anemometer_60m_wind_speed_Max', 'Anemometer_60m_wind_speed_Min',
'Anemometer_60m_wind_speed_StdDev', 'Anemometer_40m_wind_speed_Avg',
'Anemometer_40m_wind_speed_Max', 'Anemometer_40m_wind_speed_Min',
'Anemometer_40m_wind_speed_StdDev', 'wind_vane_94m_wind_direction_Avg',
'wind_vane_94m_wind_direction_StdDev', 'wind_vane_45m_wind_direction_Avg',
'wind_vane_45m_wind_direction_StdDev', 'Hygro_Thermo_95m_humidity_Avg',
'Hygro_Thermo_95m_humidity_Max', 'Hygro_Thermo_95m_humidity_Min',
'Hygro_Thermo_95m_humidity_StdDev', 'Hygro_Thermo_10m_humidity_Avg',
'Hygro_Thermo_10m_humidity_Max', 'Hygro_Thermo_10m_humidity_Min',
'Hygro_Thermo_10m_humidity_StdDev', 'Hygro_Thermo_95m_temperature_Avg',
'Hygro_Thermo_95m_temperature_Max', 'Hygro_Thermo_95m_temperature_Min',
'Hygro_Thermo_95m_temperature_StdDev', 'Hygro_Thermo_10m_temperature_Avg',
'Hygro_Thermo_10m_temperature_Max', 'Hygro_Thermo_10m_temperature_Min',
'Hygro_Thermo_10m_temperature_StdDev', 'barometer_2m_air_pressure_Avg',
'barometer_2m_air_pressure_Max', 'barometer_2m_air_pressure_Min',
'barometer_2m_air_pressure_StdDev', 'pyranometer_10m_solar_irradiance_Avg',
'pyranometer_10m_solar_irradiance_Max',
'pyranometer_10m_solar_irradiance_Min',
'pyranometer_10m_solar_irradiance_StdDev']
df = df.drop(remove_columns, axis=1)
df.columns = require_columns
if len(df) > 2:
df["device_id"] = df.iloc[7][0].split("=")[1]
df = df.loc[:5]
records_to_create = []
for idx, ser in df.iterrows():
new_record = ser.to_dict()
time_stamp_naive = datetime.strptime(new_record['time_stamp'], '%Y-%m-%d %H:%M:%S')
time_stamp_aware = iran_tz.localize(time_stamp_naive)
new_record['time_stamp'] = time_stamp_aware.astimezone()
for key, value in new_record.items():
if value == '---':
new_record[key] = None
exists = AnemometerData.objects.filter(
time_stamp=new_record['time_stamp'],
device_id=new_record['device_id']
).exists()
if not exists:
records_to_create.append(AnemometerData(**new_record))
if records_to_create:
AnemometerData.objects.bulk_create(records_to_create)
print(f"==========record create successfully for {id}_{time_str}.csv.gz==========")
except Exception as e:
print(e)
from_date = from_date + timedelta(hours=1)
and this is my setting file: """
LANGUAGE_CODE = 'en-us'
USE_TZ = False TIME_ZONE = 'Asia/Tehran'
USE_I18N = True
i set USE_TZ to False is settings file i try to use pytz to solve the problem