Django DateTime自动转换为IST字符串

2024-10-03 21:26:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在django面对这个奇怪的问题目标日期\时间当我传递Obj as函数时,对象正在转换为IST datetime字符串。而且,只有少数病例是根据日志发生的。你知道吗

#Actual Call
status_time_dict = {4: [orderObj.pickup_time,'dispatch_time'], 
                    5: [orderObj.delivered_time,'delivery_time']}

statusVar = status_time_dict.get(int(orderObj.status), [timezone.now(),"Undefined"])

statusDate = buildDateString(statusVar[0],orderObj, date_format_type = 0,convert_to_ist = 0 )
-----------------------------------------------------------------------

class Order(models.Model):
    name = models.CharField(max_length=100, blank=True)
    house_number = models.CharField(max_length=255)
    contact_number = models.CharField(max_length=13)
    order_date = models.DateField(db_index=True)
    order_time = models.DateTimeField(db_index=True) 
    pickup_time = models.DateTimeField(null=True)
    delivered_time = models.DateTimeField(null=True)

   def get_order_time(self):
       if self.order_time:
           return str(self.order_time.replace(tzinfo=None) + datetime.timedelta(minutes=330))
       else:
           return "not available"

    def get_pickup_time(self):
        if self.pickup_time:
            return str(self.pickup_time.replace(tzinfo=None) + datetime.timedelta(minutes=330))
        else:
            return "not available"

    def get_delivered_time(self):
        if self.delivered_time:
            return str(self.delivered_time.replace(tzinfo=None) + datetime.timedelta(minutes=330))
        else:
            return "not available"

def utc_to_local(utc_dt):
    local_tz = pytz.timezone('Asia/Kolkata')
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
    return local_tz.normalize(local_dt)


def buildDateString(statusDate,Obj, date_format_type = 1,convert_to_ist = 1):
    if statusDate is None:
        statusDate = timezone.now()

    elif type(statusDate) in [str, unicode]:
    # **This is where it is failing, it should not have ben str or      
    #   unicode as statusDate is being passed as OBJECT
    # OBJ.DATE_TIME should never been a string but is happeing for few 
    # cases only and that too in Prod server only.**
        failing_date = {"OID": Obj.id, "status_order": Obj.status, "failingDateString": statusDate,
            'pickup_time': Obj.pickup_time, 'allot_time': Obj.allot_time, 'delivered_time': Obj.delivered_time}

        print failing_date
        try:
            date_format = {True:"%Y-%m-%dT%H:%M:%S.%fZ", False:"%Y-%m-%d %H:%M:%S"}
            format = date_format.get('Z' in statusDate,"Y-%m-%d %H:%M:%S")
            statusDate = datetime.datetime.strptime(statusDate, format)
            if date_format_type == 1:
                return statusDate.strftime('%d-%m-%Y %H:%M:%S')
            else:
                return statusDate.strftime('%Y-%m-%d %H:%M:%S')
        except ValueError:
            statusDate = timezone.now()
    if convert_to_ist ==1:
        statusDate = utc_to_local(statusDate)
    if date_format_type == 1:
        statusDate = (statusDate).strftime('%d-%m-%Y %H:%M:%S')
    else:
        statusDate = (statusDate).strftime('%Y-%m-%d %H:%M:%S')
    return statusDate

有关正在发生的错误的更多信息,请参见elif中的评论。你知道吗

也在我的型号.py我定义了一个get\u COLUMN\u NAME函数,该函数转换为IST string,但在此上下文中没有调用它。我怀疑这是否是上述怪异行为的原因


Tags: selftrueobjformatgetdatetimedatereturn