Hardware Reference
In-Depth Information
Formatting the Current Time
he function getFormattedTime() is used to get the current time and format it. Looking
inside the function, you'll see that the variable now gets a datetime.datetime object con-
taining the current date and time:
now = datetime.datetime.now()
he method strftime is called with an argument that speciies how the current time is
formatted:
return now.strftime(“%d %b %Y %H:%M:%S. “) + ;
str(int(round(now.microsecond/1000.0)))
he format argument provides a template of codes in a string. When the program runs, the
codes in Table 17-1 are replaced by diferent parts of the date and time stored by the object.
Table 17-1 Date-Formatting Codes
Code
Meaning
English Example
Shortened weekday name
Mon
%a
%A
Full weekday name
Monday
%b
Shortened month name
Mar
Full month name
March
%B
%d
Day of the month
14
%H
Hour (24-hour clock) as a decimal number
18
Hour (12-hour clock) as a decimal number
6
%I
%m
Month as decimal number
03
%M
Minute
15
AM or PM (or local equivalent description)
PM
%p
%S
Second
12
%y
Two-digit year
13
Four-digit year
2013
%Y
%%
Use to display a single % sign
%
Different places around the world have different conventions; for example, the 14th of March in the
U.K. would be written as 14/03, but in the U.S. as 03/14. Linux locale sets the various settings like
these that are unique to a particular place in the world. For example, it also deines the currency
used, default paper size and keyboard layout. strftime uses your locale setting to determine
what %p displays. It also uses it to make %c format the date and time appropriately for you.
Search WWH ::




Custom Search