6
6
7
7
"""
8
8
from __future__ import annotations
9
+
10
+ import re
9
11
import warnings
10
12
from xml .sax .saxutils import escape
11
13
from itertools import zip_longest , repeat , chain , groupby
64
66
H = TypeVar ("H" , bound = Hashable ) # pylint: disable=invalid-name
65
67
66
68
MAX_HINTS = 1000
67
-
69
+ CUSTOM_TOOLTIP = """
70
+ %a Weekday abbreviated name
71
+ %A Weekday full name
72
+ %w Weekday as a number (0=Sunday, 6=Saturday)
73
+ %d Day of the month (01-31)
74
+ %b Month abbreviated name
75
+ %B Month full name
76
+ %m Month as a number (01-12)
77
+ %y Year without century (00-99)
78
+ %Y Year with century
79
+ %H Hour (00-23)
80
+ %I Hour (01-12)
81
+ %p AM or PM
82
+ %M Minute (00-59)
83
+ %S Second (00-59)
84
+ %f Microsecond (000000-999999)
85
+ %z UTC offset in the form +HHMM or -HHMM
86
+ %Z Time zone name
87
+ %j Day of the year (001-366)
88
+ %U Week number of the year (Sunday as the first day of the week)
89
+ %W Week number of the year (Monday as the first day of the week)
90
+ %c Locale's appropriate date and time representation
91
+ %x Locale's appropriate date representation
92
+ %X Locale's appropriate time representation
93
+ """
68
94
69
95
class _DataType :
70
96
def __eq__ (self , other ):
@@ -1523,6 +1549,7 @@ class ContinuousVariableEditor(VariableEditor):
1523
1549
1524
1550
1525
1551
class TimeVariableEditor (VariableEditor ):
1552
+ CUSTOM_FORMAT_LABEL = "Custom format"
1526
1553
def __init__ (self , parent = None , ** kwargs ):
1527
1554
super ().__init__ (parent , ** kwargs )
1528
1555
form = self .layout ().itemAt (0 )
@@ -1532,8 +1559,14 @@ def __init__(self, parent=None, **kwargs):
1532
1559
Orange .data .TimeVariable .ADDITIONAL_FORMATS .items ()
1533
1560
):
1534
1561
self .format_cb .addItem (item , StrpTime (item , * data ))
1562
+ self .format_cb .addItem (self .CUSTOM_FORMAT_LABEL )
1535
1563
self .format_cb .currentIndexChanged .connect (self .variable_changed )
1564
+ self .custom_edit = QLineEdit ()
1565
+ self .custom_edit .setPlaceholderText ("%Y-%m-%d %H:%M:%S" )
1566
+ self .custom_edit .setToolTip (CUSTOM_TOOLTIP )
1567
+ self .custom_edit .textChanged .connect (self ._on_custom_change )
1536
1568
form .insertRow (2 , "Format:" , self .format_cb )
1569
+ form .insertRow (3 , "Custom format:" , self .custom_edit )
1537
1570
1538
1571
def set_data (self , var , transform = ()):
1539
1572
super ().set_data (var , transform )
@@ -1552,9 +1585,30 @@ def get_data(self):
1552
1585
var , tr = super ().get_data ()
1553
1586
if var is not None and (self .parent () is None or not isinstance (self .parent ().var , Time )):
1554
1587
# do not add StrpTime when transforming from time to time
1555
- tr .insert (0 , self .format_cb .currentData ())
1588
+ if self .format_cb .currentText () == self .CUSTOM_FORMAT_LABEL :
1589
+ custom_text = self .custom_edit .text ()
1590
+ date_pat = r"%(-?)d|%(b|B)|%(-?)m|%(y|Y)|%(-?)j|%(-?)U|%(-?)W|%(a|A)|%w"
1591
+ time_pat = r"%(-?)H|%(-?)I|%p|%(-?)M|%(-?)S|%f"
1592
+ have_date = int (bool (re .search (date_pat , custom_text )))
1593
+ have_time = int (bool (re .search (time_pat , custom_text )))
1594
+ # this is done to ensure that the custom format is correct
1595
+ if not have_date and not have_time :
1596
+ trf = StrpTime (self .CUSTOM_FORMAT_LABEL , (None ,),
1597
+ have_date , have_time )
1598
+ else :
1599
+ trf = StrpTime (self .CUSTOM_FORMAT_LABEL , (custom_text ,),
1600
+ have_date , have_time )
1601
+ else :
1602
+ trf = self .format_cb .currentData ()
1603
+ tr .insert (0 , trf )
1556
1604
return var , tr
1557
1605
1606
+ def _on_custom_change (self ):
1607
+ if self .format_cb .currentText () != self .CUSTOM_FORMAT_LABEL :
1608
+ self .format_cb .setCurrentIndex (self .format_cb .count () - 1 )
1609
+ else :
1610
+ self .variable_changed .emit ()
1611
+
1558
1612
1559
1613
def variable_icon (var ):
1560
1614
# type: (Union[Variable, Type[Variable], ReinterpretTransform]) -> QIcon
@@ -3080,4 +3134,5 @@ def column_str_repr_string(
3080
3134
3081
3135
3082
3136
if __name__ == "__main__" : # pragma: no cover
3083
- WidgetPreview (OWEditDomain ).run (Orange .data .Table ("iris" ))
3137
+ WidgetPreview (OWEditDomain ).run (Orange .data .Table (
3138
+ "/Users/ajda/Desktop/test.csv" ))
0 commit comments