The problem of getting the pod creation date instead of the current date on the kubernetes pod

4/30/2020

I have a webservice that I published using kubernetes. And I am doing the current transaction by looking at the date If I have traded for today, I will not do it again. My problem is that the system I deployed yesterday or 2, 3 days ago gives datetime.now() output's pod created date. And it acts as there is no current transaction.

def timestamp_converter(timestamp=datetime.timestamp(datetime.now(pytz.UTC)), t_delta=0):
    '''
        Fix timestamp to date
    '''
    a = datetime.fromtimestamp(timestamp, pytz.UTC) + timedelta(t_delta)
    res = int(datetime.timestamp(datetime(a.year, a.month, a.day, tzinfo=pytz.UTC)) * 1000)
    return res

ts = timestamp_converter()

Output:

1588107600000 # '2020/04/29'

But today is 1588234224371 (2020/04/30)

-- sumeyye
datetime
kubernetes
pod
python
web-services

1 Answer

4/30/2020

In Stackoverflow, I am writing a solution for my own problem again. :) Yes, if we come to the solution. I was very enlightened. Frankly, I wouldn't have found the error.

I learned today that. When defining the function, the default value was working once. In this case, it defaults to the date the pod was installed. And the next day, the next day, and the day after that, accept the "timestamp" parameter of the function below.

def timestamp_converter(timestamp=datetime.timestamp(datetime.now(pytz.UTC)), t_delta=0):
    '''
        Fix timestamp to date
    '''
    a = datetime.fromtimestamp(timestamp, pytz.UTC) + timedelta(t_delta)
    res = int(datetime.timestamp(datetime(a.year, a.month, a.day, tzinfo=pytz.UTC)) * 1000)
    return res

I replaced the code above with

def timestamp_converter(timestamp=None, t_delta=0):
    '''
        Fix timestamp to date
    '''
    timestamp = timestamp or datetime.timestamp(datetime.now(pytz.UTC))
    a = datetime.fromtimestamp(timestamp, pytz.UTC) + timedelta(t_delta)
    res = int(datetime.timestamp(datetime(a.year, a.month, a.day, tzinfo=pytz.UTC)) * 1000)
    return res
-- sumeyye
Source: StackOverflow