def update_headers(self, resp):
headers = resp.headers
if 'expires' in headers:
return {}
if 'cache-control' in headers and headers['cache-control'] != 'public':
return {}
if resp.status not in self.cacheable_by_default_statuses:
return {}
if 'date' not in headers or 'last-modified' not in headers:
return {}
date = calendar.timegm(parsedate_tz(headers['date']))
last_modified = parsedate(headers['last-modified'])
if date is None or last_modified is None:
return {}
now = time.time()
current_age = max(0, now - date)
delta = date - calendar.timegm(last_modified)
freshness_lifetime = max(0, min(delta / 10, 24 * 3600))
if freshness_lifetime <= current_age:
return {}
expires = date + freshness_lifetime
return {'expires': time.strftime(TIME_FMT, time.gmtime(expires))}
python类gmtime()的实例源码
def format_duration(self, duration):
if (duration <= 0) and self.max is None or self.cur == self.min:
result = '??:??:??'
#elif duration < 1:
# result = '--:--:--'
else:
result = time.strftime('%H:%M:%S', time.gmtime(duration))
return result
def update_headers(self, resp):
headers = resp.headers
if 'expires' in headers:
return {}
if 'cache-control' in headers and headers['cache-control'] != 'public':
return {}
if resp.status not in self.cacheable_by_default_statuses:
return {}
if 'date' not in headers or 'last-modified' not in headers:
return {}
date = calendar.timegm(parsedate_tz(headers['date']))
last_modified = parsedate(headers['last-modified'])
if date is None or last_modified is None:
return {}
now = time.time()
current_age = max(0, now - date)
delta = date - calendar.timegm(last_modified)
freshness_lifetime = max(0, min(delta / 10, 24 * 3600))
if freshness_lifetime <= current_age:
return {}
expires = date + freshness_lifetime
return {'expires': time.strftime(TIME_FMT, time.gmtime(expires))}
def debug(output):
if _DEBUG:
if not "debuglog" in globals():
global debuglog
debuglog = open("debuglog","a")
timestamp = time.strftime("%Y-%m-%d %H:%M:%S : ", time.gmtime())
debuglog.write(timestamp + str(output)+"\n")
def test_UTCTimeJava(self):
prop = self._app.query([CF.DataType('simple_utctime', any.to_any(None))])
datetime = time.gmtime(prop[0].value.value().twsec)
self.assertEquals(datetime.tm_year,2017)
self.assertEquals(datetime.tm_mon,2)
self.assertEquals(datetime.tm_mday,1)
self.assertEquals(datetime.tm_hour,10)
self.assertEquals(datetime.tm_min,1)
self.assertEquals(datetime.tm_sec,0)
self.assertEquals(prop[0].value.value().tfsec,0.123)
self._app.configure([CF.DataType('reset_utctime', any.to_any(True))])
prop = self._app.query([CF.DataType('simple_utctime', any.to_any(None))])
now = time.time()
self.assertEquals(abs(now-(prop[0].value.value().twsec+prop[0].value.value().tfsec))<0.1,True)
def test_startofyear(self):
sdds_soy = Time.startOfYear()
# calculate start of year
soy = datetime.datetime(*(time.strptime(self.cur_year_str+"-01-01 00:00:00",
"%Y-%m-%d %H:%M:%S")[0:6]))
soy_time=calendar.timegm(soy.timetuple())
self.assertEqual( sdds_soy, soy_time )
sdds_time = Time()
sdds_time.setFromTime(soy_time)
# calculate start of year
self.assertEqual( sdds_time.gmtime(), time.gmtime(soy_time) )
def test_UTCTimePython(self):
prop = self._app.query([CF.DataType('simple_utctime', any.to_any(None))])
datetime = time.gmtime(prop[0].value.value().twsec)
self.assertEquals(datetime.tm_year,2017)
self.assertEquals(datetime.tm_mon,2)
self.assertEquals(datetime.tm_mday,1)
self.assertEquals(datetime.tm_hour,10)
self.assertEquals(datetime.tm_min,1)
self.assertEquals(datetime.tm_sec,0)
self.assertEquals(prop[0].value.value().tfsec,0.123)
self._app.configure([CF.DataType('reset_utctime', any.to_any(True))])
prop = self._app.query([CF.DataType('simple_utctime', any.to_any(None))])
now = time.time()
self.assertEquals(abs(now-(prop[0].value.value().twsec+prop[0].value.value().tfsec))<0.1,True)
def test_UTCTime(self):
prop = self._app.query([CF.DataType('simple_utctime', any.to_any(None))])
datetime = time.gmtime(prop[0].value.value().twsec)
self.assertEquals(datetime.tm_year,2017)
self.assertEquals(datetime.tm_mon,2)
self.assertEquals(datetime.tm_mday,1)
self.assertEquals(datetime.tm_hour,10)
self.assertEquals(datetime.tm_min,1)
self.assertEquals(datetime.tm_sec,0)
self.assertEquals(prop[0].value.value().tfsec,0.123)
self._app.configure([CF.DataType('reset_utctime', any.to_any(True))])
prop = self._app.query([CF.DataType('simple_utctime', any.to_any(None))])
now = time.time()
self.assertEquals(abs(now-(prop[0].value.value().twsec+prop[0].value.value().tfsec))<0.1,True)
def toString(t1, fmt=None):
gmt = t1.gmtime()
frac = int(t1.pf250_ * (Time.Tic/Time.Two32))
if not fmt:
fmt = Time.REDHAWK_FORMAT
xx=time.strftime(fmt,gmt)
return '%s.%06d' % (xx,frac)
else:
return time.strftime(fmt,gmt)
def toString(tstamp):
# Break out the whole seconds into a GMT time
gmt = time.gmtime(tstamp.twsec)
# Append the fractional seconds down to microsecond precision
fractional = int(round(tstamp.tfsec * 1e6))
return '%04d:%02d:%02d::%02d:%02d:%02d.%06d' % (gmt.tm_year, gmt.tm_mon, gmt.tm_mday, gmt.tm_hour,
gmt.tm_min, gmt.tm_sec, fractional)
def toString(tstamp):
# Break out the whole seconds into a GMT time
gmt = time.gmtime(tstamp.twsec)
# Append the fractional seconds down to microsecond precision
fractional = int(round(tstamp.tfsec * 1e6))
return '%04d:%02d:%02d::%02d:%02d:%02d.%06d' % (gmt.tm_year, gmt.tm_mon, gmt.tm_mday, gmt.tm_hour,
gmt.tm_min, gmt.tm_sec, fractional)
# Insert the arithmetic functions as operators on the PrecisionUTCTime class
def toString(tstamp):
# Break out the whole seconds into a GMT time
gmt = time.gmtime(tstamp.twsec)
# Append the fractional seconds down to microsecond precision
fractional = int(round(tstamp.tfsec * 1e6))
return '%04d:%02d:%02d::%02d:%02d:%02d.%06d' % (gmt.tm_year, gmt.tm_mon, gmt.tm_mday, gmt.tm_hour,
gmt.tm_min, gmt.tm_sec, fractional)
# Insert the arithmetic functions as operators on the PrecisionUTCTime class
def _iso_time(self, result_dict):
def _transform_item_iso_time(key, item):
if key.startswith('time') and isinstance(item, int):
return time.strftime(
'%Y-%m-%dT%H:%M:%S',
time.gmtime(item)
)
return item
self._walk_dict(result_dict, _transform_item_iso_time)
def date_time_string(self, timestamp=None):
"""Return the current date and time formatted for a message header."""
if timestamp is None:
timestamp = time.time()
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
self.weekdayname[wd],
day, self.monthname[month], year,
hh, mm, ss)
return s
def time2netscape(t=None):
"""Return a string representing time in seconds since epoch, t.
If the function is called without an argument, it will use the current
time.
The format of the returned string is like this:
Wed, DD-Mon-YYYY HH:MM:SS GMT
"""
if t is None: t = time.time()
year, mon, mday, hour, min, sec, wday = time.gmtime(t)[:7]
return "%s %02d-%s-%04d %02d:%02d:%02d GMT" % (
DAYS[wday], mday, MONTHS[mon-1], year, hour, min, sec)
def set_from(self, from_, time_=None):
"""Set "From " line, formatting and appending time_ if specified."""
if time_ is not None:
if time_ is True:
time_ = time.gmtime()
from_ += ' ' + time.asctime(time_)
self._from = from_