Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/aniso8601/date.py: 9%

75 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:03 +0000

1# -*- coding: utf-8 -*- 

2 

3# Copyright (c) 2021, Brandon Nielsen 

4# All rights reserved. 

5# 

6# This software may be modified and distributed under the terms 

7# of the BSD license. See the LICENSE file for details. 

8 

9from aniso8601.builders import TupleBuilder 

10from aniso8601.builders.python import PythonTimeBuilder 

11from aniso8601.compat import is_string 

12from aniso8601.exceptions import ISOFormatError 

13from aniso8601.resolution import DateResolution 

14 

15 

16def get_date_resolution(isodatestr): 

17 # Valid string formats are: 

18 # 

19 # Y[YYY] 

20 # YYYY-MM-DD 

21 # YYYYMMDD 

22 # YYYY-MM 

23 # YYYY-Www 

24 # YYYYWww 

25 # YYYY-Www-D 

26 # YYYYWwwD 

27 # YYYY-DDD 

28 # YYYYDDD 

29 isodatetuple = parse_date(isodatestr, builder=TupleBuilder) 

30 

31 if isodatetuple.DDD is not None: 

32 # YYYY-DDD 

33 # YYYYDDD 

34 return DateResolution.Ordinal 

35 

36 if isodatetuple.D is not None: 

37 # YYYY-Www-D 

38 # YYYYWwwD 

39 return DateResolution.Weekday 

40 

41 if isodatetuple.Www is not None: 

42 # YYYY-Www 

43 # YYYYWww 

44 return DateResolution.Week 

45 

46 if isodatetuple.DD is not None: 

47 # YYYY-MM-DD 

48 # YYYYMMDD 

49 return DateResolution.Day 

50 

51 if isodatetuple.MM is not None: 

52 # YYYY-MM 

53 return DateResolution.Month 

54 

55 # Y[YYY] 

56 return DateResolution.Year 

57 

58 

59def parse_date(isodatestr, builder=PythonTimeBuilder): 

60 # Given a string in any ISO 8601 date format, return a datetime.date 

61 # object that corresponds to the given date. Valid string formats are: 

62 # 

63 # Y[YYY] 

64 # YYYY-MM-DD 

65 # YYYYMMDD 

66 # YYYY-MM 

67 # YYYY-Www 

68 # YYYYWww 

69 # YYYY-Www-D 

70 # YYYYWwwD 

71 # YYYY-DDD 

72 # YYYYDDD 

73 if is_string(isodatestr) is False: 

74 raise ValueError("Date must be string.") 

75 

76 if isodatestr.startswith("+") or isodatestr.startswith("-"): 

77 raise NotImplementedError( 

78 "ISO 8601 extended year representation " "not supported." 

79 ) 

80 

81 if len(isodatestr) == 0 or isodatestr.count("-") > 2: 

82 raise ISOFormatError('"{0}" is not a valid ISO 8601 date.'.format(isodatestr)) 

83 yearstr = None 

84 monthstr = None 

85 daystr = None 

86 weekstr = None 

87 weekdaystr = None 

88 ordinaldaystr = None 

89 

90 if len(isodatestr) <= 4: 

91 # Y[YYY] 

92 yearstr = isodatestr 

93 elif "W" in isodatestr: 

94 if len(isodatestr) == 10: 

95 # YYYY-Www-D 

96 yearstr = isodatestr[0:4] 

97 weekstr = isodatestr[6:8] 

98 weekdaystr = isodatestr[9] 

99 elif len(isodatestr) == 8: 

100 if "-" in isodatestr: 

101 # YYYY-Www 

102 yearstr = isodatestr[0:4] 

103 weekstr = isodatestr[6:] 

104 else: 

105 # YYYYWwwD 

106 yearstr = isodatestr[0:4] 

107 weekstr = isodatestr[5:7] 

108 weekdaystr = isodatestr[7] 

109 elif len(isodatestr) == 7: 

110 # YYYYWww 

111 yearstr = isodatestr[0:4] 

112 weekstr = isodatestr[5:] 

113 elif len(isodatestr) == 7: 

114 if "-" in isodatestr: 

115 # YYYY-MM 

116 yearstr = isodatestr[0:4] 

117 monthstr = isodatestr[5:] 

118 else: 

119 # YYYYDDD 

120 yearstr = isodatestr[0:4] 

121 ordinaldaystr = isodatestr[4:] 

122 elif len(isodatestr) == 8: 

123 if "-" in isodatestr: 

124 # YYYY-DDD 

125 yearstr = isodatestr[0:4] 

126 ordinaldaystr = isodatestr[5:] 

127 else: 

128 # YYYYMMDD 

129 yearstr = isodatestr[0:4] 

130 monthstr = isodatestr[4:6] 

131 daystr = isodatestr[6:] 

132 elif len(isodatestr) == 10: 

133 # YYYY-MM-DD 

134 yearstr = isodatestr[0:4] 

135 monthstr = isodatestr[5:7] 

136 daystr = isodatestr[8:] 

137 else: 

138 raise ISOFormatError('"{0}" is not a valid ISO 8601 date.'.format(isodatestr)) 

139 

140 hascomponent = False 

141 

142 for componentstr in [yearstr, monthstr, daystr, weekstr, weekdaystr, ordinaldaystr]: 

143 if componentstr is not None: 

144 hascomponent = True 

145 

146 if componentstr.isdigit() is False: 

147 raise ISOFormatError( 

148 '"{0}" is not a valid ISO 8601 date.'.format(isodatestr) 

149 ) 

150 

151 if hascomponent is False: 

152 raise ISOFormatError('"{0}" is not a valid ISO 8601 date.'.format(isodatestr)) 

153 

154 return builder.build_date( 

155 YYYY=yearstr, 

156 MM=monthstr, 

157 DD=daystr, 

158 Www=weekstr, 

159 D=weekdaystr, 

160 DDD=ordinaldaystr, 

161 )