- class QTime¶
The
QTime
class provides clock time functions. More…Synopsis¶
Methods¶
def
__init__()
def
__reduce__()
def
__repr__()
def
addMSecs()
def
addSecs()
def
hour()
def
isNull()
def
isValid()
def
minute()
def
msec()
def
msecsTo()
def
__ne__()
def
__lt__()
def
__le__()
def
__eq__()
def
__gt__()
def
__ge__()
def
second()
def
secsTo()
def
setHMS()
def
toPython()
def
toString()
Static functions¶
def
currentTime()
def
fromString()
def
isValid()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
A
QTime
object contains a clock time, which it can express as the numbers of hours, minutes, seconds, and milliseconds since midnight. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds.QTime
objects should be passed by value rather than by reference to const; they simply packageint
.QTime
uses the 24-hour clock format; it has no concept of AM/PM. UnlikeQDateTime
,QTime
knows nothing about time zones or daylight-saving time (DST).A
QTime
object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static functioncurrentTime()
, which creates aQTime
object that represents the system’s local time.The
hour()
,minute()
,second()
, andmsec()
functions provide access to the number of hours, minutes, seconds, and milliseconds of the time. The same information is provided in textual format by thetoString()
function.The
addSecs()
andaddMSecs()
functions provide the time a given number of seconds or milliseconds later than a given time. Correspondingly, the number of seconds or milliseconds between two times can be found usingsecsTo()
ormsecsTo()
.QTime
provides a full set of operators to compare twoQTime
objects; an earlier time is considered smaller than a later one; if A.msecsTo
(B) is positive, then A < B.QTime
objects can also be created from a text representation usingfromString()
and converted to a string representation usingtoString()
. All conversion to and from string formats is done using the C locale. For localized conversions, seeQLocale
.- class TimeFlag¶
- __init__()¶
Constructs a null time object. For a null time,
isNull()
returnstrue
andisValid()
returnsfalse
. If you need a zero time, useQTime
(0, 0). For the start of a day, seestartOfDay()
.- __init__(h, m[, s=0[, ms=0]])
- Parameters:
h – int
m – int
s – int
ms – int
Constructs a time with hour
h
, minutem
, secondss
and millisecondsms
.h
must be in the range 0 to 23,m
ands
must be in the range 0 to 59, andms
must be in the range 0 to 999.See also
- __reduce__()¶
- Return type:
str
- __repr__()¶
- Return type:
str
Returns a
QTime
object containing a timems
milliseconds later than the time of this object (or earlier ifms
is negative).Note that the time will wrap if it passes midnight. See
addSecs()
for an example.Returns a null time if this time is invalid.
See also
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns a
QTime
object containing a times
seconds later than the time of this object (or earlier ifs
is negative).Note that the time will wrap if it passes midnight.
Returns a null time if this time is invalid.
Example:
QTime n(14, 0, 0) # n == 14:00:00 t = QTime() t = n.addSecs(70) # t == 14:01:10 t = n.addSecs(-70) # t == 13:58:50 t = n.addSecs(10 * 60 * 60 + 5) # t == 00:00:05 t = n.addSecs(-15 * 60 * 60) # t == 23:00:00
See also
Returns the current time as reported by the system clock.
Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.
Furthermore, currentTime() only increases within each day; it shall drop by 24 hours each time midnight passes; and, beside this, changes in it may not correspond to elapsed time, if a daylight-saving transition intervenes.
See also
Returns a new
QTime
instance with the time set to the number ofmsecs
since the start of the day, i.e. since 00:00:00.If
msecs
falls outside the valid range an invalidQTime
will be returned.See also
This is an overloaded function.
- static fromString(string[, format=Qt.TextDate])
- Parameters:
string – str
format –
DateFormat
- Return type:
This is an overloaded function.
- static fromString(string, format)
- Parameters:
string – str
format – str
- Return type:
This is an overloaded function.
- static fromString(string[, format=Qt.TextDate])
- Parameters:
string – str
format –
DateFormat
- Return type:
Returns the time represented in the
string
as aQTime
using theformat
given, or an invalid time if this is not possible.See also
- static fromString(string, format)
- Parameters:
string – str
format – str
- Return type:
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the
QTime
represented by thestring
, using theformat
given, or an invalid time if the string cannot be parsed.These expressions may be used for the format:
Expression
Output
h
The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh
The hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H
The hour without a leading zero (0 to 23, even with AM/PM display)
HH
The hour with a leading zero (00 to 23, even with AM/PM display)
m
The minute without a leading zero (0 to 59)
mm
The minute with a leading zero (00 to 59)
s
The whole second, without any leading zero (0 to 59)
ss
The whole second, with a leading zero where applicable (00 to 59)
z or zz
The fractional part of the second, as would usually follow a decimal point, without requiring trailing zeroes (0 to 999). Thus
"s.z"
matches the seconds with up to three digits of fractional part supplying millisecond precision, without needing trailing zeroes. For example,"s.z"
would recognize either"00.250"
or"0.25"
as representing a time a quarter second into its minute.zzz
Three digit fractional part of the second, to millisecond precision, including trailing zeroes where applicable (000 to 999). For example,
"ss.zzz"
would reject"0.25"
but recognize"00.250"
as representing a time a quarter second into its minute.AP, A, ap, a, aP or Ap
Either ‘AM’ indicating a time before 12:00 or ‘PM’ for later times, matched case-insensitively.
All other input characters will be treated as text. Any non-empty sequence of characters enclosed in single quotes will also be treated (stripped of the quotes) as text and not be interpreted as expressions.
time = QTime.fromString("1mm12car00", "m'mm'hcarss") # time is 12:01.00
If the format is not satisfied, an invalid
QTime
is returned. Expressions that do not expect leading zeroes to be given (h, m, s and z) are greedy. This means that they will use two digits (or three, for z) even if this puts them outside the range of accepted values and leaves too few digits for other sections. For example, the following string could have meant 00:07:10, but the m will grab two digits, resulting in an invalid time:time = QTime.fromString("00:710", "hh:ms") # invalid()
Any field that is not represented in the format will be set to zero. For example:
time = QTime.fromString("1.30", "m.s") # time is 00:01:30.000
Note
If localized forms of am or pm (the AP, ap, Ap, aP, A or a formats) are to be recognized, use
system()
.toTime().Note
If a format character is repeated more times than the longest expression in the table above using it, this part of the format will be read as several expressions with no separator between them; the longest above, possibly repeated as many times as there are copies of it, ending with a residue that may be a shorter expression. Thus
'HHHHH'
would match"08088"
or"080808"
and set the hour to 8; if the time string contained “070809” it would “match” but produce an inconsistent result, leading to an invalid time.- hour()¶
- Return type:
int
Returns the hour part (0 to 23) of the time.
Returns -1 if the time is invalid.
- isNull()¶
- Return type:
bool
Returns
true
if the time is null (i.e., theQTime
object was constructed using the default constructor); otherwise returns false. A null time is also an invalid time.See also
- isValid()¶
- Return type:
bool
Returns
true
if the time is valid; otherwise returnsfalse
. For example, the time 23:30:55.746 is valid, but 24:12:30 is invalid.See also
- static isValid(h, m, s[, ms=0])
- Parameters:
h – int
m – int
s – int
ms – int
- Return type:
bool
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
This is an overloaded function.
Returns
true
if the specified time is valid; otherwise returns false.The time is valid if
h
is in the range 0 to 23,m
ands
are in the range 0 to 59, andms
is in the range 0 to 999.Example:
QTime.isValid(21, 10, 30) # returns true QTime.isValid(22, 5, 62) # returns false
- minute()¶
- Return type:
int
Returns the minute part (0 to 59) of the time.
Returns -1 if the time is invalid.
- msec()¶
- Return type:
int
Returns the millisecond part (0 to 999) of the time.
Returns -1 if the time is invalid.
- msecsSinceStartOfDay()¶
- Return type:
int
Returns the number of msecs since the start of the day, i.e. since 00:00:00.
See also
Returns the number of milliseconds from this time to
t
. Ift
is earlier than this time, the number of milliseconds returned is negative.Because
QTime
measures time within a day and there are 86400 seconds in a day, the result is always between -86400000 and 86400000 ms.Returns 0 if either time is invalid.
See also
Returns
true
iflhs
is different fromrhs
; otherwise returnsfalse
.Returns
true
iflhs
is earlier thanrhs
; otherwise returnsfalse
.Returns
true
iflhs
is earlier than or equal torhs
; otherwise returnsfalse
.Returns
true
iflhs
is equal torhs
; otherwise returnsfalse
.Returns
true
iflhs
is later thanrhs
; otherwise returnsfalse
.Returns
true
iflhs
is later than or equal torhs
; otherwise returnsfalse
.- second()¶
- Return type:
int
Returns the second part (0 to 59) of the time.
Returns -1 if the time is invalid.
Returns the number of seconds from this time to
t
. Ift
is earlier than this time, the number of seconds returned is negative.Because
QTime
measures time within a day and there are 86400 seconds in a day, the result is always between -86400 and 86400.secsTo() does not take into account any milliseconds.
Returns 0 if either time is invalid.
- setHMS(h, m, s[, ms=0])¶
- Parameters:
h – int
m – int
s – int
ms – int
- Return type:
bool
Sets the time to hour
h
, minutem
, secondss
and millisecondsms
.h
must be in the range 0 to 23,m
ands
must be in the range 0 to 59, andms
must be in the range 0 to 999. Returnstrue
if the set time is valid; otherwise returnsfalse
.See also
- toPython()¶
- Return type:
object
- toString(format)¶
- Parameters:
format – str
- Return type:
str
- toString([f=Qt.TextDate])
- Parameters:
f –
DateFormat
- Return type:
str
This is an overloaded function.
Returns the time as a string. The
format
parameter determines the format of the string.If
format
isTextDate
, the string format is HH:mm:ss; e.g. 1 second before midnight would be “23:59:59”.If
format
isISODate
, the string format corresponds to the ISO 8601 extended specification for representations of dates, represented by HH:mm:ss. To include milliseconds in the ISO 8601 date, use theformat
ISODateWithMs
, which corresponds to HH:mm:ss.zzz.If the
format
isRFC2822Date
, the string is formatted in an RFC 2822 compatible way. An example of this formatting is “23:59:20”.If the time is invalid, an empty string will be returned.
See also
- toString(format)
- Parameters:
format – str
- Return type:
str