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
285 views
in Technique[技术] by (71.8m points)

UTC a JTS Time zone change in python

I want to change UTC time to JTS when I csv the information I get in salesforce.

Is there a way to change the timezone? put an argument of data ['CreatedDate'] = d.astimezone but output the CreatedDate in UTC time. This way I want to change.

UTC            JTS
CreatedDate    CreatedDate
-------------- ---------------
2020/1/1 0:25  2020/1/1 9:25

code

from simple_salesforce import Salesforce
from datetime import datetime
import csv
import os
import json
import account

SALESFORCE_USERNAME = '123'
PASSWORD = '123'
SECURITY_TOKEN = ''

def main():
    # Authentication settings
    sf = Salesforce(username=SALESFORCE_USERNAME,
                    password=PASSWORD,
                    security_token=SECURITY_TOKEN)

    # Lead Column setting to be acquired
    columns = [
        "CreatedDate"
    ]
    sosl = 'SELECT {0[0]} FROM Lead'.format(
        columns)

    # Data acquisition with SOSL
    data = sf.query_all(sosl)

    # Delete CSV file if it exists
    output_csv = 'output.csv'
    if os.path.exists(output_csv):
        os.remove(output_csv)

    # Write to CSV file
    for k, v in data.items():
        if type(v) is list:
            with open(output_csv, 'w', newline="") as f:
                writer = csv.DictWriter(f, fieldnames=columns)
                writer.writeheader()
                for d in v:
                    data = json.loads(json.dumps(d))
                    del data['attributes']
                    data['CreatedDate'], '%Y-%m-%dT%H:%M:%S.%f%z')
                    data['CreatedDate'] = d.astimezone
                    data['CreatedDate'] = d.strftime('%Y-%m-%d %H:%M:%S')
                    writer.writerow(data)
if __name__ == '__main__':
    main()

If anyone knows, please let me know.

question from:https://stackoverflow.com/questions/65842231/utc-a-jts-time-zone-change-in-python

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...