Skip to content

Commit

Permalink
Merge branch 'bugfix/21-wrong-name-for-datetime'
Browse files Browse the repository at this point in the history
  • Loading branch information
warappa committed Jun 27, 2024
2 parents 8446be2 + 84c4774 commit b3ba957
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
97 changes: 91 additions & 6 deletions src/BMEcatSharp/Types/BMEcatDatetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,113 @@ public BMEcatDatetime(BMEcatDatetimeType type)
/// <br/>
/// XML-namespace: BMECAT
/// </summary>
[XmlIgnore]
public DateTimeOffset Value { get; set; }

[BMEXmlElement("DATE")]
public DateTime Date { get; set; }
[EditorBrowsable(EditorBrowsableState.Never)]
public string Date
{
get => Value.ToString("yyyy-MM-dd");
set
{
var strs = value.Split(['-'], StringSplitOptions.RemoveEmptyEntries);
if (strs.Length == 3)
{
Value = new DateTimeOffset(
int.Parse(strs[0]),
int.Parse(strs[1]),
int.Parse(strs[2]),
Value.Hour,
Value.Minute,
Value.Second,
Value.Offset);
}
else
{
throw new InvalidOperationException("Wrong format for DATE");
}
}
}

/// <summary>
/// (required - choice Date/Time/Timezone - deprecated) Time<br/>
/// (optional - choice Date/Time/Timezone - deprecated) Time<br/>
/// <br/>
/// Element for time.<br/>
/// <br/>
/// XML-namespace: BMECAT
/// </summary>
[BMEXmlElement("TIME")]
public DateTime? Time { get; set; }
[EditorBrowsable(EditorBrowsableState.Never)]
public bool TimeSpecified => Time.HasValue;
public string? Time
{
get => Value.ToString("HH:mm:ss");
set
{
var strs = value?.Split([':'], StringSplitOptions.RemoveEmptyEntries);
var hour = 0;
var minute = 0;
var second = 0;
if (strs?.Length == 3)
{
hour = int.Parse(strs[0]);
minute = int.Parse(strs[1]);
second = int.Parse(strs[2]);
}
else if (strs is not null)
{
throw new InvalidOperationException("Wrong format for TIME");
}

Value = new DateTimeOffset(
Value.Year,
Value.Month,
Value.Day,
hour,
minute,
second,
Value.Offset);
}
}

/// <summary>
/// (required - choice Date/Time/Timezone - deprecated) Time zone<br/>
/// (optional - choice Date/Time/Timezone - deprecated) Time zone<br/>
/// <br/>
/// Element for timezone.<br/>
/// <br/>
/// XML-namespace: BMECAT
/// </summary>
[BMEXmlElement("TIMEZONE")]
public string? Timezone { get; set; }
[EditorBrowsable(EditorBrowsableState.Never)]
public string? Timezone
{
get => $"{(Value.Offset.Hours < 0 ? "-" : "+")}{Value.Offset:hh\\:mm}";
set
{
var strs = value?.Split([':'], StringSplitOptions.RemoveEmptyEntries);
var offset = TimeSpan.Zero;
if (strs?.Length == 2)
{
offset = new TimeSpan(int.Parse(strs[0]), int.Parse(strs[1]), 0);
}
else if (strs?.Length == 1 &&
value == "Z")
{
offset = TimeSpan.Zero;
}
else if (strs is not null)
{
throw new InvalidOperationException("Wrong format for TIMEZONE");
}

Value = new DateTimeOffset(
Value.Year,
Value.Month,
Value.Day,
Value.Hour,
Value.Minute,
Value.Second,
offset);
}
}
}
6 changes: 3 additions & 3 deletions src/BMEcatSharp/Types/Catalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ public string? VersionForSerializer
/// XML-namespace: BMECAT
/// </summary>
//[Obsolete("The element DATETIME in the context of CATALOG with the attribute 'generation_date' will be replaced by the element GENERATION_DATE in future versions and will be omitted then.")]
[BMEXmlElement("DATE")]
public BMEcatDatetime? Date { get; set; }
[BMEXmlElement("DATETIME")]
public BMEcatDatetime? DateTime { get; set; }
[EditorBrowsable(EditorBrowsableState.Never)]
public bool DateSpecified => Date is not null;
public bool DateTimeSpecified => DateTime is not null;

/// <summary>
/// (optional) Territory<br/>
Expand Down

0 comments on commit b3ba957

Please sign in to comment.