I want to convert a UNIX time like 1522922431 (Unix time) into RFC 3339 format used by Google API 2018-04-05T10:00:31+00:00 (RFC 3339) using python. Is it possible using any python packages or python modules?
            Asked
            
        
        
            Active
            
        
            Viewed 281 times
        
    -4
            
            
         
    
    
        Markandeya
        
- 507
- 4
- 13
- 
                    4Possible duplicate of [Converting unix timestamp string to readable date](https://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date) – Patrick Artner Apr 05 '18 at 10:08
- 
                    1(Re-) **Search** before asking questions. Use the answers provided in the dupe to create a `datetime` and then format it as you like using [Format Specification Mini-Language](https://docs.python.org/2/library/string.html#format-specification-mini-language) or use the answers provided in [generate-rfc-3339-timestamp-in-python](https://stackoverflow.com/questions/8556398/generate-rfc-3339-timestamp-in-python) – Patrick Artner Apr 05 '18 at 10:09
- 
                    It was supposed to be more specific question than the generalized answer, to be used for Google API, does it change the relevance? – Markandeya Apr 05 '18 at 10:20
- 
                    Markandeya - then code one. SO is not a "coding service for what I want" - it is a forum where you give your code that you have problems with and we help you fix it. Use the links provided, code your solution and if you _then_ have got problems, ask a question providing your [mvce](https://stackoverflow.com/help/mcve) – Patrick Artner Apr 05 '18 at 10:31
- 
                    @PatrickArtner True, but why code when there are inbuilt functions that can be used? – Markandeya Apr 05 '18 at 10:33
- 
                    Coding is the way of chaining inbuild things - did you even read the questions I linked you too and viewed the answers on them? They solve your problem ... thats kindof the definition of duplicate... – Patrick Artner Apr 05 '18 at 10:36
1 Answers
1
            Use the datetime and pytz library
import pytz
from datetime import datetime
utc = pytz.utc
a_date = datetime.utcfromtimestamp(1522922431)
utc_date = utc.localize(a_date).isoformat()
'2018-04-05T10:00:31+00:00'
 
    
    
        ScottMcC
        
- 4,094
- 1
- 27
- 35
- 
                    
- 
                    To allow conversion of `datetime` objects between different timezones. See http://pytz.sourceforge.net/ – ScottMcC Apr 05 '18 at 10:29
- 
                    @Markandeya Just made a slight change, I was using the `fromtimestamp` method, it should have been the `utcfromtimestamp` method – ScottMcC Apr 05 '18 at 10:32
- 
                    valid solution for a question that is a dupe :) I mostly do not answer dupes but thats preference. +1 – Patrick Artner Apr 05 '18 at 10:33
- 
                    All good, always happy to help out. Even though the question is a duplicate I do find that I get personal benefit from putting together my own solution. In this instance for this question I discovered the differences between the `.utcfromtimestamp` and `.fromtimestamp` methods – ScottMcC Apr 05 '18 at 13:18