Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio Id3v2 Add Support For Release DateTime #325

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 61 additions & 2 deletions src/TaglibSharp.Tests/TaggingFormats/Id3V2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class Id3V2Test
static readonly string[] val_gnre = {"Rap",
"Jazz", "Non-Genre", "Blues"};

static readonly System.DateTime val_date = new System.DateTime (2022, 10, 20, 16, 45, 23, 0, 0);

[Test]
public void TestTitle ()
{
Expand Down Expand Up @@ -968,6 +970,35 @@ public void TestPublisher ()
}
}


[Test]
public void TestEncodedBy ()
{
Tag tag = new Tag ();
for (byte version = 2; version <= 4; version++) {
tag.Version = version;

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
Assert.IsNull (t.EncodedBy, "Initial (Null): " + m);
});

tag.EncodedBy = val_sing;

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsFalse (t.IsEmpty, "Value Set (!IsEmpty): " + m);
Assert.AreEqual (val_sing, t.EncodedBy, "Value Set (!Null): " + m);
});

tag.EncodedBy = string.Empty;

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Assert.IsNull (t.EncodedBy, "Value Cleared (Null): " + m);
});
}
}

[Test]
public void TestISRC ()
{
Expand Down Expand Up @@ -996,6 +1027,34 @@ public void TestISRC ()
}
}

[Test]
public void TestReleaseDate ()
{
Tag tag = new Tag ();
for (byte version = 4; version <= 4; version++) {
tag.Version = version;

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Initial (IsEmpty): " + m);
Assert.IsNull (t.ReleaseDate, "Initial (Null): " + m);
}, 4);

tag.ReleaseDate = val_date;

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsFalse (t.IsEmpty, "Value Set (!IsEmpty): " + m);
Assert.AreEqual (val_date, t.ReleaseDate.Value, "Value Set (!Null): " + m);
}, 4);

tag.ReleaseDate = null;

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Assert.IsNull (t.ReleaseDate, "Value Cleared (Null): " + m);
}, 4);
}
}

[Test]
public void TestLength ()
{
Expand Down Expand Up @@ -1634,10 +1693,10 @@ public void TestInvolvedPersonsFrame ()

delegate void TagTestFunc (Tag tag, string msg);

void TagTestWithSave (ref Tag tag, TagTestFunc testFunc)
void TagTestWithSave (ref Tag tag, TagTestFunc testFunc, byte minVersion = 2)
{
testFunc (tag, "Before Save");
for (byte version = 2; version <= 4; version++) {
for (byte version = minVersion; version <= 4; version++) {
tag.Version = version;
tag = new Tag (tag.Render ());
testFunc (tag, "After Save, Version: " + version);
Expand Down
2 changes: 2 additions & 0 deletions src/TaglibSharp/Id3v2/FrameTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ static class FrameType
public static readonly ReadOnlyByteVector WPUB = "WPUB";
public static readonly ReadOnlyByteVector WXXX = "WXXX";
public static readonly ReadOnlyByteVector ETCO = "ETCO";
public static readonly ReadOnlyByteVector TDRL = "TDRL"; // Release Time Frame
public static readonly ReadOnlyByteVector TENC = "TENC"; // Encoded By Frame.
}
}
56 changes: 56 additions & 0 deletions src/TaglibSharp/Id3v2/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,20 @@ IEnumerator IEnumerable.GetEnumerator ()
set { SetTextFrame (FrameType.TPUB, value); }
}

/// <summary>
/// Gets and sets the TENC (Encoded by) of the song.
/// </summary>
/// <value>
/// A <see cref="string" /> object containing the TENC of the song.
/// </value>
/// <remarks>
/// This property is implemented using the "TENC" field.
/// </remarks>
public string EncodedBy {
get { return GetTextAsString (FrameType.TENC); }
set { SetTextFrame (FrameType.TENC, value); }
}

/// <summary>
/// Gets and sets the ISRC (International Standard Recording Code) of the song.
/// </summary>
Expand All @@ -2279,6 +2293,48 @@ IEnumerator IEnumerable.GetEnumerator ()
set { SetTextFrame (FrameType.TSRC, value); }
}

/// <summary>
/// Gets and sets the date at which the song has been released.
/// </summary>
/// <value>
/// A nullable <see cref="DateTime" /> object containing the
/// date at which the song has been released, or <see
/// langword="null" /> if no value present.
/// </value>
/// <remarks>
/// <para>This property is implemented using the "TDRL" field.</para>
/// <para>This is a ID3v2.4 type tag.</para>
/// </remarks>
public DateTime? ReleaseDate {
get {
string value = GetTextAsString (FrameType.TDRL);

if (String.IsNullOrWhiteSpace(value)) {
return null;
} else if (DateTime.TryParseExact (value.Replace ('T', ' '), "yyyy-MM-dd HH:mm:ss", null, DateTimeStyles.None, out DateTime exactDate)) {
return exactDate;
} else if (DateTime.TryParse(value, out DateTime parsedDate)) {
return parsedDate;
}

return null;
}
set {
string date = null;

if (value != null) {
date = $"{value:yyyy-MM-dd HH:mm:ss}";
date = date.Replace (' ', 'T');
}

if (date == null) {
RemoveFrames(FrameType.TDRL);
} else {
SetTextFrame(FrameType.TDRL, date);
}
}
}

/// <summary>
/// Gets and sets the length of the media represented
/// by the current instance.
Expand Down