3
3
using System ;
4
4
using System . Collections . Generic ;
5
5
using System . Globalization ;
6
+ using System . Linq ;
6
7
using System . Text ;
7
8
using static ATL . TagData ;
8
9
@@ -84,7 +85,7 @@ public ushort TrackNumber
84
85
{
85
86
if ( tagData [ Field . TRACK_NUMBER_TOTAL ] != null )
86
87
return TrackUtils . ExtractTrackNumber ( tagData [ Field . TRACK_NUMBER_TOTAL ] ) ;
87
- else return TrackUtils . ExtractTrackNumber ( tagData [ Field . TRACK_NUMBER ] ) ;
88
+ return TrackUtils . ExtractTrackNumber ( tagData [ Field . TRACK_NUMBER ] ) ;
88
89
}
89
90
set => tagData . IntegrateValue ( Field . TRACK_NUMBER , value . ToString ( ) ) ;
90
91
}
@@ -95,9 +96,9 @@ public ushort TrackTotal
95
96
{
96
97
if ( tagData [ Field . TRACK_NUMBER_TOTAL ] != null )
97
98
return TrackUtils . ExtractTrackTotal ( tagData [ Field . TRACK_NUMBER_TOTAL ] ) ;
98
- else if ( Utils . IsNumeric ( tagData [ Field . TRACK_TOTAL ] ) )
99
+ if ( Utils . IsNumeric ( tagData [ Field . TRACK_TOTAL ] ) )
99
100
return ushort . Parse ( tagData [ Field . TRACK_TOTAL ] ) ;
100
- else return TrackUtils . ExtractTrackTotal ( tagData [ Field . TRACK_NUMBER ] ) ;
101
+ return TrackUtils . ExtractTrackTotal ( tagData [ Field . TRACK_NUMBER ] ) ;
101
102
}
102
103
set => tagData . IntegrateValue ( Field . TRACK_TOTAL , value . ToString ( ) ) ;
103
104
}
@@ -108,7 +109,7 @@ public ushort DiscNumber
108
109
{
109
110
if ( tagData [ Field . DISC_NUMBER_TOTAL ] != null )
110
111
return TrackUtils . ExtractTrackNumber ( tagData [ Field . DISC_NUMBER_TOTAL ] ) ;
111
- else return TrackUtils . ExtractTrackNumber ( tagData [ Field . DISC_NUMBER ] ) ;
112
+ return TrackUtils . ExtractTrackNumber ( tagData [ Field . DISC_NUMBER ] ) ;
112
113
}
113
114
set => tagData . IntegrateValue ( Field . DISC_NUMBER , value . ToString ( ) ) ;
114
115
}
@@ -119,9 +120,9 @@ public ushort DiscTotal
119
120
{
120
121
if ( tagData [ Field . DISC_NUMBER_TOTAL ] != null )
121
122
return TrackUtils . ExtractTrackTotal ( tagData [ Field . DISC_NUMBER_TOTAL ] ) ;
122
- else if ( Utils . IsNumeric ( tagData [ Field . DISC_TOTAL ] ) )
123
+ if ( Utils . IsNumeric ( tagData [ Field . DISC_TOTAL ] ) )
123
124
return ushort . Parse ( tagData [ Field . DISC_TOTAL ] ) ;
124
- else return TrackUtils . ExtractTrackTotal ( tagData [ Field . DISC_NUMBER ] ) ;
125
+ return TrackUtils . ExtractTrackTotal ( tagData [ Field . DISC_NUMBER ] ) ;
125
126
}
126
127
set => tagData . IntegrateValue ( Field . DISC_TOTAL , value . ToString ( ) ) ;
127
128
}
@@ -130,11 +131,30 @@ public DateTime Date
130
131
{
131
132
get
132
133
{
133
- DateTime result ;
134
- if ( ! DateTime . TryParse ( Utils . ProtectValue ( tagData [ Field . RECORDING_DATE ] ) , out result ) ) // First try with a proper Recording date field
134
+ DateTime result = DateTime . MinValue ;
135
+ bool success = false ;
136
+
137
+ // The field may be holding multiple values => split it and order it by size desc
138
+ string [ ] dateValues = Array . Empty < string > ( ) ;
139
+ if ( tagData . hasKey ( Field . RECORDING_DATE ) )
135
140
{
136
- bool success = false ;
137
- string dayMonth = Utils . ProtectValue ( tagData [ Field . RECORDING_DAYMONTH ] ) ; // If not, try to assemble year and dateMonth (e.g. ID3v2)
141
+ dateValues = tagData [ Field . RECORDING_DATE ]
142
+ . Split ( Settings . InternalValueSeparator )
143
+ . OrderByDescending ( s => s . Length )
144
+ . ToArray ( ) ;
145
+
146
+ // First try with a proper Recording date field
147
+ foreach ( var dateValue in dateValues )
148
+ {
149
+ success = DateTime . TryParse ( Utils . ProtectValue ( dateValue ) , out result ) ;
150
+ if ( success ) break ;
151
+ }
152
+ }
153
+
154
+ // If not, try to assemble year and dateMonth (e.g. ID3v2)
155
+ if ( ! success )
156
+ {
157
+ string dayMonth = Utils . ProtectValue ( tagData [ Field . RECORDING_DAYMONTH ] ) ;
138
158
string year = Utils . ProtectValue ( tagData [ Field . RECORDING_YEAR ] ) ;
139
159
if ( 4 == dayMonth . Length && 4 == year . Length )
140
160
{
@@ -152,10 +172,19 @@ public DateTime Date
152
172
}
153
173
success = DateTime . TryParse ( dateTimeBuilder . ToString ( ) , out result ) ;
154
174
}
155
- if ( ! success ) // Year only
175
+
176
+ // Year only
177
+ if ( ! success )
156
178
{
157
- if ( year . Length != 4 ) year = Utils . ProtectValue ( tagData [ Field . RECORDING_DATE ] ) ; // ...then with RecordingDate
158
- if ( 4 == year . Length ) // We have a year !
179
+ // ...then try with RecordingDate
180
+ foreach ( var dateValue in dateValues )
181
+ {
182
+ if ( 4 == year . Length ) break ;
183
+ year = Utils . ProtectValue ( dateValue ) ;
184
+ }
185
+
186
+ // We have a year !
187
+ if ( 4 == year . Length )
159
188
{
160
189
StringBuilder dateTimeBuilder = new StringBuilder ( ) ;
161
190
dateTimeBuilder . Append ( year ) . Append ( "-01-01" ) ;
@@ -165,12 +194,11 @@ public DateTime Date
165
194
dateTimeBuilder . Append ( 'T' ) ;
166
195
dateTimeBuilder . Append ( time [ ..2 ] ) . Append ( ':' ) ;
167
196
dateTimeBuilder . Append ( time . Substring ( 2 , 2 ) ) . Append ( ':' ) ;
168
- dateTimeBuilder . Append ( ( 6 == time . Length ) ? time . Substring ( 4 , 2 ) : "00" ) ;
197
+ dateTimeBuilder . Append ( 6 == time . Length ? time . Substring ( 4 , 2 ) : "00" ) ;
169
198
}
170
- success = DateTime . TryParse ( dateTimeBuilder . ToString ( ) , out result ) ;
199
+ DateTime . TryParse ( dateTimeBuilder . ToString ( ) , out result ) ;
171
200
}
172
201
}
173
- if ( ! success ) result = DateTime . MinValue ;
174
202
}
175
203
return result ;
176
204
}
0 commit comments