Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -355,31 +355,50 @@ public static double MSNumber(IList<XPathItem> value)
// string ms:format-time(string datetime[, string format[, string language]])
//
// Format xsd:dateTime as a date/time string for a given language using a given format string.
// * Datetime contains a lexical representation of xsd:dateTime. If datetime is not valid, the
// * dateTime contains a lexical representation of xsd:dateTime. If datetime is not valid, the
// empty string is returned.
// * Format specifies a format string in the same way as for GetDateFormat/GetTimeFormat system
// * format specifies a format string in the same way as for GetDateFormat/GetTimeFormat system
// functions. If format is the empty string or not passed, the default date/time format for the
// given culture is used.
// * Language specifies a culture used for formatting. If language is the empty string or not
// * lang specifies a culture used for formatting. If language is the empty string or not
// passed, the current culture is used. If language is not recognized, a runtime error happens.
// * isDate - true to output just the date; false to output just the time.
public static string MSFormatDateTime(string dateTime, string format, string lang, bool isDate)
{
try
{
string locale = GetCultureInfo(lang).Name;

XsdDateTime xdt;
if (!XsdDateTime.TryParse(dateTime, XsdDateTimeFlags.AllXsd | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrTimeNoTz, out xdt))
if (!XsdDateTime.TryParse(dateTime, XsdDateTimeFlags.AllXsd | XsdDateTimeFlags.XdrDateTime | XsdDateTimeFlags.XdrTimeNoTz, out XsdDateTime xdt))
{
return string.Empty;
}

DateTime dt = xdt.ToZulu();
CultureInfo ci = string.IsNullOrEmpty(lang) ?
CultureInfo.CurrentCulture :
GetCultureInfo(lang);

// If format is the empty string or not specified, use the default format for the given locale
return dt.ToString(format.Length != 0 ? format : null, new CultureInfo(locale));
if (isDate)
{
DateOnly dateOnly = DateOnly.FromDateTime(dt);
return dateOnly.ToString(!string.IsNullOrEmpty(format) ? format : null, ci);
}
else
{
TimeOnly timeOnly = TimeOnly.FromDateTime(dt);
if (!string.IsNullOrEmpty(format))
{
return timeOnly.ToString(format, ci);
}
else
{
// Duplicate what the Win32 GetTimeFormat API returns
// with an empty format - the long time string.
return timeOnly.ToLongTimeString();
}
}
}
catch (ArgumentException)
{ // Operations with DateTime can throw this exception eventualy
catch (Exception ex) when (ex is ArgumentException or FormatException)
{
return string.Empty;
}
}
Expand Down