def local_time(self):
localtime = timezone(str(TIME_ZONE))
time = datetime.now(localtime)
format_time = time.strftime('%H:%M:%S')
if TIME_ZONE == 'US/Eastern':
output = "Local time: {} EST".format(format_time)
if TIME_ZONE == 'Europe/London':
output = "Local time: {} GMT".format(format_time)
if TIME_ZONE == 'Europe/Oslo':
output = "Local time: {} CEST".format(format_time)
return output
python类strftime()的实例源码
def local_time(self):
from modules.bot import bot_msg
localtime = timezone(str(TIME_ZONE))
time = datetime.now(localtime)
format_time = time.strftime('%H:%M:%S')
if TIME_ZONE == 'US/Eastern':
output = "Local time: {} EST".format(format_time)
if TIME_ZONE == 'Europe/London':
output = "Local time: {} GMT".format(format_time)
if TIME_ZONE == 'Europe/Oslo':
output = "Local time: {} CEST".format(format_time)
bot_msg(output)
def set_time(self, time):
"""Set si station internal time.
@param time: time as a python datetime object.
"""
bintime = (SIReader._to_str(int(time.strftime('%y')), 1)
+ SIReader._to_str(time.month, 1)
+ SIReader._to_str(time.day, 1)
+ SIReader._to_str(((time.isoweekday() % 7) << 1) + time.hour // 12, 1)
+ SIReader._to_str((time.hour % 12) * 3600 + time.minute * 60 + time.second, 2)
+ SIReader._to_str(int(round(time.microsecond / 1000000.0 * 256)), 1)
)
self._send_command(SIReader.C_SET_TIME, bintime)
self.beep()
def weekdayStr(date):
return date.strftime("%A")
# Returns the weekday as a string in its abbreviated form
def weekdayStrAbv(date):
return date.strftime("%a")
# Returns the day of the month
def monthStr(date):
return date.strftime("%B")
# Returns the month as a string in its abbreviated form
def monthStrAbv(date):
return date.strftime("%b")
# Returns the quarter in the year
def year(date):
return date.strftime("%Y")
# Returns the year and month as a concatenated string
def time_label_12(min_num):
hours, minutes = divmod(min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%I:%M %p')
# Given a minute number, return the 24-hour time label
def time_label_24(min_num):
hours, minutes = divmod(min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%H:%M')
# Given a minute number, return the 15 minute interval it occures in
def label_hh(min_num):
hours, minutes = divmod(min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%I %p')
# Given a minute number, return the 24-hour time label with just hours
def label_hh24(min_num):
hours, minutes = divmod(min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%H')
# Given a minute number, return the 15 minute interval label for a 24-hour clock
def label_15_min_24(min_num):
interval_num = time_interval_15_min(min_num)
int_min_num = interval_num * 15
hours, minutes = divmod(int_min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%H:%M')
# Given a minute number, return the 30 minute interval label for a 24-hour clock
def label_60_min_24(min_num):
interval_num = time_interval_60_min(min_num)
int_min_num = interval_num * 60
hours, minutes = divmod(int_min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%H:%M')
# Given a minute number, return the 15 minute interval label for a 12-hour clock
def label_15_min_12(min_num):
interval_num = time_interval_15_min(min_num)
int_min_num = interval_num * 15
hours, minutes = divmod(int_min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%I:%M %p')
# Given a minute number, return the 30 minute interval label for a 12-hour clock
def label_30_min_12(min_num):
interval_num = time_interval_30_min(min_num)
int_min_num = interval_num * 30
hours, minutes = divmod(int_min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%I:%M %p')
# Given a miute number, return the 60 minute interval label for a 12-hour clock
def label_60_min_12(min_num):
interval_num = time_interval_60_min(min_num)
int_min_num = interval_num * 60
hours, minutes = divmod(int_min_num, 60)
timestamp = time(hour=hours, minute=minutes)
return time.strftime(timestamp, '%I:%M %p')