python converted timestamp to UTC date

def datetime_switch_to_timestamp13(dt:str) -> str:
    """
        dt = '2020-11-01T00:00:00.000Z'
        converted result: 1604188800000
        
        or
        
        dt = '2021-12-31T23:59:59.999Z'
        converted result: 1606780799999

    """
	#   convert str type to <class 'datetime.datetime'> = Your time zone + (GMT+08:00)
    date = datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S.%fZ") + relativedelta(hours=8)
    #  <class 'datetime.datetime'> have the property timetuple(), then convert datetime to 10 TimestampInMillis
    date_stamp = str(int(time.mktime(date.timetuple())))
    #  <class 'datetime.datetime'> have the property microsecond, then get 3 microsecond
    date_microsecond = str("%06d" % date.microsecond)[0:3]
    # <class 'str'> 13 TimestampInMillis = 10 TimestampInMillis + 3 microsecond 
    date_stamp = date_stamp + data_microsecond
    return date_stamp


if __name__ == '__main__':
    dt = '2020-11-01T00:00:00.000Z'
    print(datetime_switch_to_timestamp13(dt))
    # result : 1604188800000

def datetime_switch_to_timestamp10(dt):
    """
        dt = 2022-01-25T10:34:48.029Z
        converted result: 1642125489
        """
    s = time.mktime(time.strptime(dt, "%Y-%m-%dT%H:%M:%S.%fZ"))
    return int(s)


if __name__ == '__main__':
    dt = '2022-01-25T10:34:48.029Z'
    print(datetime_switch_to_timestamp13(dt))
    # result : 1642125489
def convert_date_to_UTCdate(sdate):
    """
    date = 2022-01-16
    converted result: 2021-11-12T00:00:00.000Z
    """
    # sdate = datetime.strptime(sdate, "%Y-%m-%d").date()
    tdate = sdate.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'
    return tdate
def convert_Vuedate_to_UTCtdate(Vuedate):
    """
    Vuedate= 2021-05-01 00:00:00+00:00
    converted result: 2021-11-12T00:00:00.000Z
    """
    date_list = re.split("\+", Vuedate, maxsplit=1)
    date = datetime.strptime(date_list[0], "%Y-%m-%d %H:%M:%S")
    UTCtdate = date.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'
    return UTCtdate
def increase_start_bucket_months(date, months):
    """
    date = 2021-05-01 00:00:00+00:00
    converted result: 2021-06-01T00:00:00.000Z
    """
    date_list = re.split("\+", date, maxsplit=1)
    date = datetime.strptime(date_list[0], "%Y-%m-%d %H:%M:%S")
    date = date + relativedelta(months=months)
    tdate = date.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'
    return tdate

if __name__ == '__main__':
    date = '2021-05-01 00:00:00+00:00'
    print(increase_start_bucket_months(date, 1))
    # result : 2021-06-01T00:00:00.000Z
def increase_end_bucket_months(pdate, month):
    """
    pdate = 2020-01-31 23:59:59.999000+00:00
    converted result: 2021-11-12T00:00:00.000Z
    """
    pdate_list = re.split("\+", pdate, maxsplit=1)
    pdate = datetime.strptime(pdate_list[0], "%Y-%m-%d %H:%M:%S.%f")
    pdate = pdate + relativedelta(months=month)
    tdate = pdate.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + 'Z'
    return tdate

if __name__ == '__main__':
    date = '2020-01-31 23:59:59.999000+00:00'
    print(increase_start_bucket_months(date, 1))
    # result : 2020-02-29T00:00:00.000Z

def convert_ldate_to_date(ldate):
    """
    ldate = 2021-11-12 09:17:15.819000+00:00
    converted result: 2021-11-12
    """
    long_date_list = re.split(" ", long_date)
    CreationTime = datetime.strptime(long_date_list[0], '%Y-%m-%d').date()
    return CreationTime
def convert_ldate_to_sdate(long_date):
    """
    ldate = 2021-11-12 09:17:15.819000+00:00
    converted result: Nov 12, 2021
    """
    long_date_list = re.split(" ", long_date)
    CreationTime = datetime.strptime(long_date_list[0], "%Y-%m-%d").date()
    # https://stackoverflow.com/questions/9525944/python-datetime-formatting-without-zero-padding
    short_date = CreationTime.strftime('%b %d, %Y').replace(" 0", " ")
    return short_date
def convert_epoch_to_human_date(timeStamp):
    """
       timeStamp = 1640995199999
       converted result: Dec 31, 2021
       """
    date = time.gmtime(timeStamp / 1000)
    human_date = time.strftime("%b %d, %Y", date)
    return human_date
def timestamps_deduction(startTimestampInMillis, endTimestampInMillis):
    """
    one month between startTimestampInMillis  and endTimestampInMillis 
       startTimestampInMillis = 1643685415000
       endTimestampInMillis = 1646018215999
       result: boolean

    """
    date1 = time.gmtime(startTimestampInMillis / 1000)
    date2 = time.gmtime(endTimestampInMillis / 1000)
    date1 = datetime(date1[0], date1[1], date1[2])
    date2 = datetime(date2[0], date2[1], date2[2])
    date_result = date1 + relativedelta(months=1, days=-1)
    if date2 == date_result:
        return True
def increase_TimestampInMillis(TimestampInMillis, month):
    """
    add months base on startTimestampInMillis, endTimestampInMillis
    startTimestampInMillis = 1643685415000
    endTimestampInMillis = 1606780799999

   """
    date = datetime.fromtimestamp(TimestampInMillis/1000)
    date_result = date + relativedelta(months=month)
    # 10 TimestampInMillis
    date_stamp = str(int(time.mktime(date_result.timetuple())))
    #  3 microsecond
    data_microsecond = str("%06d" % date.microsecond)[0:3]
    #  13 TimestampInMillis
    date_stamp = date_stamp + data_microsecond
    return int(date_stamp)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值