I would like to write log in a file with the correct timezone time (Europe/Paris) in a UpdateSubsStatus.log file ...
Here what i tried, the commented line are what i tired but it does not work.
import pytz
class Formatter(logging.Formatter):
"""override logging.Formatter to use an aware datetime object"""
def converter(self, timestamp):
dt = datetime.datetime.fromtimestamp(timestamp)
tzinfo = pytz.timezone('America/Denver')
return tzinfo.localize(dt)
def formatTime(self, record, datefmt=None):
dt = self.converter(record.created)
if datefmt:
s = dt.strftime(datefmt)
else:
try:
s = dt.isoformat(timespec='milliseconds')
except TypeError:
s = dt.isoformat()
return s
#console = logging.StreamHandler()
logging.basicConfig(filename='UpdateSubsStatus.log', filemode='a', format='%(asctime)s;%(name)s - %(levelname)s - %(message)s',datefmt='%Y-%m-%d %H:%M:%S')
#console.setFormatter(Formatter)
#logging.getLogger('').addHandler(console)
Does it exist a simple way with the basic config to use a timezone ?
Regards
I tried logging events using your code. the events are logged in the local timezone, not the specified one. I think the problem is with this code
def converter(self, timestamp):
dt = datetime.datetime.fromtimestamp(timestamp)
tzinfo = pytz.timezone('America/Denver')
return tzinfo.localize(dt)
This code converts timestamp to local datetime and changes tzinfo without a real datetime conversion
>>> timestamp
1619258964.3946261
>>> dt = datetime.datetime.fromtimestamp(timestamp)
>>> dt
datetime.datetime(2021, 4, 24, 13, 9, 24, 394626)
>>> tzinfo = pytz.timezone('America/Denver')
>>> res = tzinfo.localize(dt)
>>> res
datetime.datetime(2021, 4, 24, 13, 9, 24, 394626, tzinfo=<DstTzInfo 'America/Denver' MDT-1 day, 18:00:00 DST>)
# Hours left unchanged
So I change method converter
def converter(self, timestamp):
# Create datetime in UTC
dt = datetime.datetime.fromtimestamp(timestamp, tz=pytz.UTC)
# Change datetime's timezone
return dt.astimezone(pytz.timezone('America/Denver'))
Here the final code
import pytz
import logging
import datetime
class Formatter(logging.Formatter):
"""override logging.Formatter to use an aware datetime object"""
def converter(self, timestamp):
# Create datetime in UTC
dt = datetime.datetime.fromtimestamp(timestamp, tz=pytz.UTC)
# Change datetime's timezone
return dt.astimezone(pytz.timezone('America/Denver'))
def formatTime(self, record, datefmt=None):
dt = self.converter(record.created)
if datefmt:
s = dt.strftime(datefmt)
else:
try:
s = dt.isoformat(timespec='milliseconds')
except TypeError:
s = dt.isoformat()
return s
console = logging.FileHandler('UpdateSubsStatus.log')
console.setFormatter(Formatter(
'%(asctime)s;%(name)s - %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S'))
logging.getLogger('').addHandler(console)
logging.critical("Critical event")
Log file UpdateSubsStatus.log (still America/Denver timezone)
2021-04-24 04:19:16;root - CRITICAL - Critical event
2021-04-24 04:19:17;root - CRITICAL - Critical event
Also, don't forget to change the timezone to Europe/Paris
Hope This helps, just using simple logic and passing in the required Timezone as an argument. Also, the return value is created with dateTimenow format.
from datetime import datetime
import pytz
def getTime(timeZoneVal):
# get the standard UTC time
UTC = pytz.utc
logTime = pytz.timezone(timeZoneVal)
#print("US Time in Default Format : ", datetime.now(logTime))
return datetime.now(logTime)
logTime = getTime('Europe/Paris')
print(logTime)
import time
import datetime
import socket
import ssl
all_ip = ["cert-name1", "cert-name2","cert-name3","cert-name4"]
def ssl_expiry_datetime(hostname):
ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z'
context = ssl.create_default_context()
conn = context.wrap_socket(
socket.socket(socket.AF_INET),
server_hostname=hostname,
)
conn.connect((hostname, 443))
ssl_info = conn.getpeercert()
# parse the string from the certificate into a Python datetime object
today_date = time.strftime(r'%b %d %H:%M:%S %Y %Z')
b = datetime.datetime.strptime(ssl_info['notAfter'], ssl_date_fmt)
print b - datetime.datetime.utcnow()
def lambda_handler(event,context):
for hostname in all_ip:
ssl_expiry_datetime(hostname)
My code was this ,if i pass 3 certs it was working but when i add my 4th cert in that list i was getting time out error from 4th cert!!1 amy i doing anything wrong in my code?
Please help, thanks.
This is what I have:
pat = '%Y-%m-%d %H:%M:%S +0000'
my_time = time.strptime(task_time, pattern)
But, how can I change the timezone from:
my_time:
2016-06-15 23:27:52 +0000
to a different timezone:
PDT
or
-0700
So the result is:
result = 2016-06-15 16:27:52 -0700
Using the python package arrow this simple script can be used:
import arrow
fmt = "YYYY-MM-DD HH:mm:ss Z"
time = arrow.get("2016-06-15 23:27:52 +0000", fmt)
time = time.to("US/Pacific")
print(time.format(fmt))
2016-06-15 16:27:52 -0700
Install arrow with pip install arrow
Edit: If you do not want to use the arrow package:
import time
import calendar
fmt = "%Y-%m-%d %H:%M:%S "
t = calendar.timegm(time.strptime("2016-06-15 23:27:52 +0000", fmt + "+0000"))
t -= 8 * 60 * 60
s = time.strftime(fmt + "-0700", time.gmtime(t))
print(s)
Note, that this is horrible code and if you use this in production you will definitely get fired from your job, so just install the arrow package!
I am trying to build a function as follows:
Input
UTC timestamp in miliseconds, for example:
1456865863633
UTC offset, or miliseconds to add/substract to the UTC timestamp to obtain the time in the corresponding timezone, for example:
-14400
OutPut
Time zone name in the following format:
US/Alaska
US/Aleutian
US/Arizona
US/Central
US/East-Indiana
US/Eastern
US/Hawaii
.
.
.
I've been trying to find the rught combination to do it using datetime and pytz libraries, but I was not successful so far.
Any ideas?
You could loop through all the timezones
for name in pytz.all_timezones:
and compare the date's utcoffset to the given offset:
if date.utcoffset().total_seconds() == utcoffset:
result.append(name)
import datetime as DT
import pytz
utc = pytz.utc
def tzones(timestamp, utcoffset):
result = []
date = utc.localize(DT.datetime.utcfromtimestamp(timestamp/float(1000)))
for name in pytz.all_timezones:
timezone = pytz.timezone(name)
date = date.astimezone(timezone)
if date.utcoffset().total_seconds() == utcoffset:
result.append(name)
return result
print(tzones(1456865863633, -14400))
prints
['America/Anguilla', 'America/Antigua', 'America/Aruba', 'America/Barbados', 'America/Blanc-Sablon', 'America/Boa_Vista', 'America/Campo_Grande', 'America/Cuiaba', 'America/Curacao', 'America/Dominica', 'America/Glace_Bay', 'America/Goose_Bay', 'America/Grand_Turk', 'America/Grenada', 'America/Guadeloupe', 'America/Guyana', 'America/Halifax', 'America/Kralendijk', 'America/La_Paz', 'America/Lower_Princes', 'America/Manaus', 'America/Marigot', 'America/Martinique', 'America/Moncton', 'America/Montserrat', 'America/Port_of_Spain', 'America/Porto_Velho', 'America/Puerto_Rico', 'America/Santo_Domingo', 'America/St_Barthelemy', 'America/St_Kitts', 'America/St_Lucia', 'America/St_Thomas', 'America/St_Vincent', 'America/Thule', 'America/Tortola', 'America/Virgin', 'Atlantic/Bermuda', 'Brazil/West', 'Canada/Atlantic', 'Etc/GMT+4']
Here's a different implementation of the approach from #unutbu's answer:
from datetime import datetime
import pytz
def timezones_from_utc_offset(offset, now=None):
if now is None:
now = datetime.now(pytz.utc)
return {tz.zone for tz in map(pytz.timezone, pytz.all_timezones_set)
if now.astimezone(tz).utcoffset() == offset}
If the input is the POSIX time in milliseconds:
>>> from datetime import timedelta
>>> dt = datetime(1970,1,1,tzinfo=pytz.utc) + timedelta(milliseconds=1456865863633)
>>> timezones_from_utc_offset(offset=timedelta(seconds=-14400), now=dt)
{'America/Anguilla',
'America/Antigua',
'America/Aruba',
'America/Barbados',
'America/Blanc-Sablon',
'America/Boa_Vista',
'America/Campo_Grande',
'America/Cuiaba',
'America/Curacao',
'America/Dominica',
'America/Glace_Bay',
'America/Goose_Bay',
'America/Grand_Turk',
'America/Grenada',
'America/Guadeloupe',
'America/Guyana',
'America/Halifax',
'America/Kralendijk',
'America/La_Paz',
'America/Lower_Princes',
'America/Manaus',
'America/Marigot',
'America/Martinique',
'America/Moncton',
'America/Montserrat',
'America/Port_of_Spain',
'America/Porto_Velho',
'America/Puerto_Rico',
'America/Santo_Domingo',
'America/St_Barthelemy',
'America/St_Kitts',
'America/St_Lucia',
'America/St_Thomas',
'America/St_Vincent',
'America/Thule',
'America/Tortola',
'America/Virgin',
'Atlantic/Bermuda',
'Brazil/West',
'Canada/Atlantic',
'Etc/GMT+4'}
How I can change this code from localtime UTC to UTC+2. Now hours() function print 13 but I need to write 15.
import time;
def hours():
localtime = time.localtime(time.time())
return localtime.tm_hour
def minutes():
localtime = time.localtime(time.time())
return localtime.tm_min
def seconds():
localtime = time.localtime(time.time())
return localtime.tm_sec
print(hours())
#minutes()
#seconds()
How about using the datetime module:
import datetime;
today = datetime.datetime.now()
todayPlus2Hours = today + datetime.timedelta(hours=2)
print(todayPlus2Hours)
print(todayPlus2Hours.hour)
print(todayPlus2Hours.minute)
print(todayPlus2Hours.second)
You can use pytz along with datetime modules.
for a timezone reference i'd look here.
I'd do something of this sort:
import datetime
import pytz
utc_dt = datetime.datetime.now(tz=pytz.utc)
amsterdam_tz = pytz.timezone("Europe/Amsterdam")
local_amsterdam_time = amsterdam_tz.normalize(utc_dt)
print local_amsterdam_time.hour
print local_amsterdam_time.minute
print local_amsterdam_time.second