Altova XMLSpy 2024 Enterprise Edition

此类可用于处理具有xs:duration类型的XML特性或元素。

 

构造函数


Name

描述

ic_method

Duration(Duration obj)

Duration类的新实例初始化为作为参数提供的Duration对象。

ic_method

Duration(System.TimeSpan newvalue)

Duration类的新实例初始化为作为参数提供的System.TimeSpan对象。

ic_method

Duration(long ticks)

Duration类的新实例初始化为作为参数提供的刻度数。

ic_method

Duration(int newyears, int newmonths, int days, int hours, int minutes, int seconds, double partseconds, bool bnegative)

Duration类的新实例初始化为由作为参数提供的部分构建的持续时间。

 

属性


Name

描述

ic_property

int Months

获取或设置Duration的当前实例的月数。

ic_property

System.TimeSpan Value

获取或设置Duration的当前实例的值(作为System.TimeSpan)。

ic_property

int Years

获取或设置Duration的当前实例的年数。

 

方法


Name

描述

ic_method

override bool Equals(object other)

如果指定的对象等于当前对象,则返回true;否则返回false

ic_method

override int GetHashCode()

返回当前实例的哈希代码。

ic_method

bool IsNegative()

如果Duration的当前实例表示一个负持续时间,则返回true

ic_method

static Duration Parse( string s, ParseType pt )

使用作为参数提供的解析类型返回一个由作为参数提供的字符串解析的Altova.Types.Duration对象。有效的解析类型值:

DURATION

假设存在年、月、日以及持续时间部分,则解析持续时间,。

YEARMONTH

假设仅存在年和月部分,则解析持续时间。

DAYTIME

假设仅存在日和时间部分,则解析持续时间。

 

请注意,此方法是静态的,只能在类本身上调用,而不能在该类的实例上调用。

ic_method

override string ToString()

将当前Duration实例转换为字符串。例如,3小时4分钟5秒这一时间范围会被转换为“PT3H4M5S”。

ic_method

string ToYearMonthString()

使用“Year and Month”解析类型将当前Duration实例转换为字符串。

 

运算符

Name

描述

!=

确定Duration a是否不等于Duration b。

==

确定Duration是否等于Duration b。

 

示例

在您的程序中使用以下代码片段之前,请确保已导入Altova类型:

 

using Altova.Types;

 

以下代码片段展示了创建Duration对象的各种方法:

 

protected static void DurationExample1()
{
  // Create a new time span of 3 hours, 4 minutes, and 5 seconds
  System.TimeSpan ts = new TimeSpan(3, 4, 5);
  // Create a Duration from the time span
  Duration dr = new Duration(ts);
  // The output is: PT3H4M5S
  Console.WriteLine("Duration created from TimeSpan: " + dr.ToString());
 
  // Create a negative Altova.Types.Duration from 6 years, 5 months, 4 days, 3 hours,
      // 2 minutes, 1 second, and .33 of a second
  Duration dr1 = new Duration(6, 5, 4, 3, 2, 1, .33, true);
  // The output is: -P6Y5M4DT3H2M1.33S
  Console.WriteLine("Duration created from parts: " + dr1.ToString());
 
  // Create a Duration from a string using the DAYTIME parse type
  Duration dr2 = Altova.Types.Duration.Parse("-P4DT3H2M1S", Duration.ParseType.DAYTIME);
  // The output is -P4DT3H2M1S
  Console.WriteLine("Duration created from string: " + dr2.ToString());
 
  // Create a duration from ticks            
  Duration dr3 = new Duration(System.DateTime.UtcNow.Ticks);
  // Output the result
  Console.WriteLine("Duration created from ticks: " + dr3.ToString());
}

 

以下代码片段展示了如何从Duration对象获取值:

 

protected static void DurationExample2()
{
  // Create a negative Altova.Types.Duration from 6 years, 5 months, 4 days, 3 hours,
      // 2 minutes, 1 second, and .33 of a second
  Duration dr = new Duration(6, 5, 4, 3, 2, 1, .33, true);
  // The output is: -P6Y5M4DT3H2M1.33S
  Console.WriteLine("The complete duration is: " + dr.ToString());
 
  // Get only the year and month part as string
  string dr1 = dr.ToYearMonthString();
  Console.WriteLine("The YEARMONTH part is: " + dr1);
 
  // Get the number of years in duration
  Console.WriteLine("Years: " + dr.Years);
 
  // Get the number of months in duration
  Console.WriteLine("Months: " + dr.Months);
}

© 2017-2023 Altova GmbH