How to convert a Greenwich string time format to local time

Source: Internet
Author: User

The Python time function learns a whole lot, so how do you convert a Greenwich time string to local time?


Here is an example of how to convert a GMT string to Beijing time.


GMT String: 2015-08-31t11:20:48


First, convert the string to a tuple format that represents time:

>>> t=time.strptime (' 2015-08-31t11:20:48 ', '%y-%m-%dt%h:%m:%s ') >>> ttime.struct_time (tm_year= Tm_mon=8, tm_mday=31, tm_hour=11, tm_min=20, tm_sec=48, tm_wday=0, tm_yday=243, Tm_isdst=-1)


At this point, the string can be represented in the touple format. That's OK if we add 8 to the hour number and then we can read the log format. But in Python, a tuple is not able to be changed (the concept is a little blurry, it should be).


That's the only way to do it.

>>> i=list (t) >>> i[2015, 8, 0, 243, -1]>>> i[3] = i[3] + 8>>> i[2015, 8, 31, 19, 20, 48, 0, 243,-1]


At this point, the difference is how to express to everyone can read the format.

>>> time.strftime ('%y-%m-%d%h:%m:%s ', tuple (i)) ' 2015-08-31 19:20:48 '


In this way, it should be in line with our expectations. Oh! It's so nerve-racking!

This article is from the "RIP Notes" blog, please make sure to keep this source http://unixman.blog.51cto.com/10163040/1694035

How to convert a Greenwich string time format to local time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.