Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/isodate/isostrf.py: 51%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

83 statements  

1""" 

2This module provides an alternative strftime method. 

3 

4The strftime method in this module allows only a subset of Python's strftime 

5format codes, plus a few additional. It supports the full range of date values 

6possible with standard Python date/time objects. Furthermore there are several 

7pr-defined format strings in this module to make ease producing of ISO 8601 

8conforming strings. 

9""" 

10 

11import re 

12from datetime import date, timedelta 

13 

14from isodate.duration import Duration 

15from isodate.isotzinfo import tz_isoformat 

16 

17# Date specific format strings 

18DATE_BAS_COMPLETE = "%Y%m%d" 

19DATE_EXT_COMPLETE = "%Y-%m-%d" 

20DATE_BAS_WEEK_COMPLETE = "%YW%W%w" 

21DATE_EXT_WEEK_COMPLETE = "%Y-W%W-%w" 

22DATE_BAS_ORD_COMPLETE = "%Y%j" 

23DATE_EXT_ORD_COMPLETE = "%Y-%j" 

24DATE_BAS_WEEK = "%YW%W" 

25DATE_EXT_WEEK = "%Y-W%W" 

26DATE_BAS_MONTH = "%Y%m" 

27DATE_EXT_MONTH = "%Y-%m" 

28DATE_YEAR = "%Y" 

29DATE_CENTURY = "%C" 

30 

31# Time specific format strings 

32TIME_BAS_COMPLETE = "%H%M%S" 

33TIME_EXT_COMPLETE = "%H:%M:%S" 

34TIME_BAS_MINUTE = "%H%M" 

35TIME_EXT_MINUTE = "%H:%M" 

36TIME_HOUR = "%H" 

37 

38# Time zone formats 

39TZ_BAS = "%z" 

40TZ_EXT = "%Z" 

41TZ_HOUR = "%h" 

42 

43# DateTime formats 

44DT_EXT_COMPLETE = DATE_EXT_COMPLETE + "T" + TIME_EXT_COMPLETE + TZ_EXT 

45DT_BAS_COMPLETE = DATE_BAS_COMPLETE + "T" + TIME_BAS_COMPLETE + TZ_BAS 

46DT_EXT_ORD_COMPLETE = DATE_EXT_ORD_COMPLETE + "T" + TIME_EXT_COMPLETE + TZ_EXT 

47DT_BAS_ORD_COMPLETE = DATE_BAS_ORD_COMPLETE + "T" + TIME_BAS_COMPLETE + TZ_BAS 

48DT_EXT_WEEK_COMPLETE = DATE_EXT_WEEK_COMPLETE + "T" + TIME_EXT_COMPLETE + TZ_EXT 

49DT_BAS_WEEK_COMPLETE = DATE_BAS_WEEK_COMPLETE + "T" + TIME_BAS_COMPLETE + TZ_BAS 

50 

51# Duration formts 

52D_DEFAULT = "P%P" 

53D_WEEK = "P%p" 

54D_ALT_EXT = "P" + DATE_EXT_COMPLETE + "T" + TIME_EXT_COMPLETE 

55D_ALT_BAS = "P" + DATE_BAS_COMPLETE + "T" + TIME_BAS_COMPLETE 

56D_ALT_EXT_ORD = "P" + DATE_EXT_ORD_COMPLETE + "T" + TIME_EXT_COMPLETE 

57D_ALT_BAS_ORD = "P" + DATE_BAS_ORD_COMPLETE + "T" + TIME_BAS_COMPLETE 

58 

59STRF_DT_MAP = { 

60 "%d": lambda tdt, yds: "%02d" % tdt.day, 

61 "%f": lambda tdt, yds: "%06d" % tdt.microsecond, 

62 "%H": lambda tdt, yds: "%02d" % tdt.hour, 

63 "%j": lambda tdt, yds: "%03d" 

64 % (tdt.toordinal() - date(tdt.year, 1, 1).toordinal() + 1), 

65 "%m": lambda tdt, yds: "%02d" % tdt.month, 

66 "%M": lambda tdt, yds: "%02d" % tdt.minute, 

67 "%S": lambda tdt, yds: "%02d" % tdt.second, 

68 "%w": lambda tdt, yds: "%1d" % tdt.isoweekday(), 

69 "%W": lambda tdt, yds: "%02d" % tdt.isocalendar()[1], 

70 "%Y": lambda tdt, yds: (((yds != 4) and "+") or "") + (("%%0%dd" % yds) % tdt.year), 

71 "%C": lambda tdt, yds: (((yds != 4) and "+") or "") 

72 + (("%%0%dd" % (yds - 2)) % (tdt.year / 100)), 

73 "%h": lambda tdt, yds: tz_isoformat(tdt, "%h"), 

74 "%Z": lambda tdt, yds: tz_isoformat(tdt, "%Z"), 

75 "%z": lambda tdt, yds: tz_isoformat(tdt, "%z"), 

76 "%%": lambda tdt, yds: "%", 

77} 

78 

79STRF_D_MAP = { 

80 "%d": lambda tdt, yds: "%02d" % tdt.days, 

81 "%f": lambda tdt, yds: "%06d" % tdt.microseconds, 

82 "%H": lambda tdt, yds: "%02d" % (tdt.seconds / 60 / 60), 

83 "%m": lambda tdt, yds: "%02d" % tdt.months, 

84 "%M": lambda tdt, yds: "%02d" % ((tdt.seconds / 60) % 60), 

85 "%S": lambda tdt, yds: "%02d" % (tdt.seconds % 60), 

86 "%W": lambda tdt, yds: "%02d" % (abs(tdt.days / 7)), 

87 "%Y": lambda tdt, yds: (((yds != 4) and "+") or "") 

88 + (("%%0%dd" % yds) % tdt.years), 

89 "%C": lambda tdt, yds: (((yds != 4) and "+") or "") 

90 + (("%%0%dd" % (yds - 2)) % (tdt.years / 100)), 

91 "%%": lambda tdt, yds: "%", 

92} 

93 

94 

95def _strfduration(tdt, format, yeardigits=4): 

96 """ 

97 this is the work method for timedelta and Duration instances. 

98 

99 see strftime for more details. 

100 """ 

101 

102 def repl(match): 

103 """ 

104 lookup format command and return corresponding replacement. 

105 """ 

106 if match.group(0) in STRF_D_MAP: 

107 return STRF_D_MAP[match.group(0)](tdt, yeardigits) 

108 elif match.group(0) == "%P": 

109 ret = [] 

110 if isinstance(tdt, Duration): 

111 if tdt.years: 

112 ret.append("%sY" % abs(tdt.years)) 

113 if tdt.months: 

114 ret.append("%sM" % abs(tdt.months)) 

115 usecs = abs( 

116 (tdt.days * 24 * 60 * 60 + tdt.seconds) * 1000000 + tdt.microseconds 

117 ) 

118 seconds, usecs = divmod(usecs, 1000000) 

119 minutes, seconds = divmod(seconds, 60) 

120 hours, minutes = divmod(minutes, 60) 

121 days, hours = divmod(hours, 24) 

122 if days: 

123 ret.append("%sD" % days) 

124 if hours or minutes or seconds or usecs: 

125 ret.append("T") 

126 if hours: 

127 ret.append("%sH" % hours) 

128 if minutes: 

129 ret.append("%sM" % minutes) 

130 if seconds or usecs: 

131 if usecs: 

132 ret.append(("%d.%06d" % (seconds, usecs)).rstrip("0")) 

133 else: 

134 ret.append("%d" % seconds) 

135 ret.append("S") 

136 # at least one component has to be there. 

137 return ret and "".join(ret) or "0D" 

138 elif match.group(0) == "%p": 

139 return str(abs(tdt.days // 7)) + "W" 

140 return match.group(0) 

141 

142 return re.sub("%d|%f|%H|%m|%M|%S|%W|%Y|%C|%%|%P|%p", repl, format) 

143 

144 

145def _strfdt(tdt, format, yeardigits=4): 

146 """ 

147 this is the work method for time and date instances. 

148 

149 see strftime for more details. 

150 """ 

151 

152 def repl(match): 

153 """ 

154 lookup format command and return corresponding replacement. 

155 """ 

156 if match.group(0) in STRF_DT_MAP: 

157 return STRF_DT_MAP[match.group(0)](tdt, yeardigits) 

158 return match.group(0) 

159 

160 return re.sub("%d|%f|%H|%j|%m|%M|%S|%w|%W|%Y|%C|%z|%Z|%h|%%", repl, format) 

161 

162 

163def strftime(tdt, format, yeardigits=4): 

164 """Directive Meaning Notes 

165 %d Day of the month as a decimal number [01,31]. 

166 %f Microsecond as a decimal number [0,999999], zero-padded 

167 on the left (1) 

168 %H Hour (24-hour clock) as a decimal number [00,23]. 

169 %j Day of the year as a decimal number [001,366]. 

170 %m Month as a decimal number [01,12]. 

171 %M Minute as a decimal number [00,59]. 

172 %S Second as a decimal number [00,61]. (3) 

173 %w Weekday as a decimal number [0(Monday),6]. 

174 %W Week number of the year (Monday as the first day of the week) 

175 as a decimal number [00,53]. All days in a new year preceding the 

176 first Monday are considered to be in week 0. (4) 

177 %Y Year with century as a decimal number. [0000,9999] 

178 %C Century as a decimal number. [00,99] 

179 %z UTC offset in the form +HHMM or -HHMM (empty string if the 

180 object is naive). (5) 

181 %Z Time zone name (empty string if the object is naive). 

182 %P ISO8601 duration format. 

183 %p ISO8601 duration format in weeks. 

184 %% A literal '%' character. 

185 

186 """ 

187 if isinstance(tdt, (timedelta, Duration)): 

188 return _strfduration(tdt, format, yeardigits) 

189 return _strfdt(tdt, format, yeardigits)