1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# -*- coding: utf-8 -*- ## Copyright 2019 Trevor van Hoof and Jan Pijpers. ## Licensed under the Apache License, Version 2.0 ## Downloaded from https://janpijpers.com or https://gumroad.com/janpijpers ## See the license file attached or on https://www.janpijpers.com/script-licenses/ ''' Name: timeInIsoStandardFormat Description: For whatever reason I found it hard to get python to output a timestamp of a continuous string with all info in it. Hence making this function. Im sure there should be a better way to do this buuuuttttt this works XD ''' from datetime import datetime def get_now_time( ): ''' I think this is called the iso format timestamp. I think in python 3 you can do this a lot easier but I keep this as a usefull time manipulation example Just prints a timestamp in a neat string with timezone information. 2019-12-08T09:32:55.423000+09:00 :return: ''' ## Diff local now and utcnow localnow = datetime.now() utcnow = datetime.utcnow() # compute the time difference in seconds tzd = localnow - utcnow secs = tzd.total_seconds() # get a positive or negative prefix for the timezone prefix = '+' if secs < 0: prefix = '-' secs = abs(secs) ## fix the remaining seconds. # print the local time with the difference, correctly formatted suffix = "%s%02d:%02d" % (prefix, secs/3600, secs/60%60) return "%s%s" % (localnow.isoformat('T'), suffix) print get_now_time() |