Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

How to convert times in UTC to a corresponding Date and time in UTC using python?

I have an array of time in UTC as follows:

Array = [0,6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,126]

I want corresponding date and time in string format.

For example: If starting date(say) is 28-04-2019, the date and time for 0, 6, 12, 18 will be 28-04-2019:0, 28-04-2019:6,28-04-2019:12 and 28-04-2019:18. And similarly for 24, 30,36 and 42 should be 29-04-2019:00, 29-04-2019:06, 29-04-2019:12, 30-04-2019:18 and so on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given you are new to SO, I'm cutting you some slack. But To start I would like to point out a couple of issues with your question and what you have shown.

  1. First as someone already pointed out Array is not an Array it is a list. When presenting information in future try to be specific and accurate.
  2. While not explicitly stated, the list is expressed as hours offset from the start date. You should always carefully define your variables.
  3. Your start date seems to be a string expressing a date inn the form of mm-dd-yyyy. Again, always try to be explicit in defining variables.
  4. You don't specify the form of desired output. Options include a datetime compatible timestamp, a pandas timestamp, or a string. I have opted to demonstrate producing a string.

Okay now for a solution:

import datetime as dt
from dateutil import parser, relativedelta  
from dateutil.utils import default_tzinfo
def gen_date(stDate: str, ofst: int) -> str:
    std = parser.parse(stDate + ' 00:00 UTC') #Makes TZ aware timestamp
    newdt = std + relativedelta.relativedelta(hours=ofst)
    return newdt.strftime("%m-%d-%Y:%H %Z") #Returns TZ aware string
    
offset_lst = [0,6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,126]
std = '28-04-2019'
for ofst in offset_lst:
    print(f"Start Date {std} + Offset {ofst} hours -> {gen_date(std, ofst)}")  

The above yields:

Start Date 28-04-2019 + Offset 0 hours -> 04-28-2019:00 UTC
Start Date 28-04-2019 + Offset 6 hours -> 04-28-2019:06 UTC
Start Date 28-04-2019 + Offset 12 hours -> 04-28-2019:12 UTC
Start Date 28-04-2019 + Offset 18 hours -> 04-28-2019:18 UTC
Start Date 28-04-2019 + Offset 24 hours -> 04-29-2019:00 UTC
Start Date 28-04-2019 + Offset 30 hours -> 04-29-2019:06 UTC
Start Date 28-04-2019 + Offset 36 hours -> 04-29-2019:12 UTC
Start Date 28-04-2019 + Offset 42 hours -> 04-29-2019:18 UTC
Start Date 28-04-2019 + Offset 48 hours -> 04-30-2019:00 UTC
Start Date 28-04-2019 + Offset 54 hours -> 04-30-2019:06 UTC
Start Date 28-04-2019 + Offset 60 hours -> 04-30-2019:12 UTC
Start Date 28-04-2019 + Offset 66 hours -> 04-30-2019:18 UTC
Start Date 28-04-2019 + Offset 72 hours -> 05-01-2019:00 UTC
Start Date 28-04-2019 + Offset 78 hours -> 05-01-2019:06 UTC
Start Date 28-04-2019 + Offset 84 hours -> 05-01-2019:12 UTC
Start Date 28-04-2019 + Offset 90 hours -> 05-01-2019:18 UTC
Start Date 28-04-2019 + Offset 96 hours -> 05-02-2019:00 UTC
Start Date 28-04-2019 + Offset 102 hours -> 05-02-2019:06 UTC
Start Date 28-04-2019 + Offset 108 hours -> 05-02-2019:12 UTC
Start Date 28-04-2019 + Offset 114 hours -> 05-02-2019:18 UTC
Start Date 28-04-2019 + Offset 120 hours -> 05-03-2019:00 UTC
Start Date 28-04-2019 + Offset 126 hours -> 05-03-2019:06 UTC

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...