Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AF SDK Reference

AFTime Structure

  • Last UpdatedNov 18, 2025
  • 13 minute read
AFTime Structure
AFTime is used to represent time and convert between various time formats.

Namespace:  OSIsoft.AF.Time
Assembly:  OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.1.1.1182

Syntax

[SerializableAttribute]
public struct AFTime : IComparable, IComparable<AFTime>, 
	IEquatable<AFTime>, IFormattable
<SerializableAttribute>
Public Structure AFTime
	Implements IComparable, IComparable(Of AFTime), 
	IEquatable(Of AFTime), IFormattable

Dim instance As AFTime
[SerializableAttribute]
public value class AFTime : IComparable, 
	IComparable<AFTime>, IEquatable<AFTime>, IFormattable
[<SealedAttribute>]
[<SerializableAttribute>]
type AFTime =  
    struct
        interface IComparable
        interface IComparable<AFTime>
        interface IEquatable<AFTime>
        interface IFormattable
    end

The AFTime type exposes the following members.

Constructors

  NameDescription
Public method
AFTime(DateTime)
Initializes a new instance of the AFTime structure to the specified DateTime.
Public method
AFTime(Double)
Initializes a new instance of the AFTime structure to the specified number of seconds since January 1, 1970.
Public method
AFTime(Int64)
Initializes a new instance of the AFTime structure to the specified number of ticks.
Public method
AFTime(Object)
Initializes a new instance of the AFTime structure to the specified value.
Public methodCode example
AFTime(String)
Initializes a new instance of the AFTime structure to the specified string representation of time.
Public method
AFTime(Object, AFTime)
Initializes a new instance of the AFTime structure to the specified value representation of time using the CurrentCulture information and formatting style, providing a default if necessary.
Public method
AFTime(Object, IFormatProvider)
Initializes a new instance of the AFTime structure to the specified value representation of time using the specified culture-specific information and formatting style.
Public methodCode example
AFTime(String, IFormatProvider)
Initializes a new instance of the AFTime structure to the specified string representation of time using the specified culture-specific formatting information.
Public method
AFTime(Object, AFTime, IFormatProvider)
Initializes a new instance of the AFTime structure to the specified value representation of time using the specified culture-specific information and formatting style, providing a default if necessary.

Properties

  NameDescription
Public property
IsEmpty
Tests whether this time has not been initialized or is DateTime.MinValue.
Public property
LocalTime
Gets a DateTime object converted to local time.
Public propertyStatic member
Now
Gets a AFTime object that is set to the current date and time on this computer.
Public propertyStatic member
NowInWholeSeconds
Gets a AFTime object that is set to the current date and time on this computer with the fractional seconds truncated.
Public property
PITime Obsolete.
Returns the time as a PI SDK PITime.
Public property
UtcSeconds
Return the time as the number of seconds since January 1, 1970, UTC.
Public property
UtcTime
Gets a DateTime object that is expressed in Coordinated Universal Time (UTC).

Methods

  NameDescription
Public method
CompareTo(Object)
Compares this instance with a specified Object.
Public method
CompareTo(AFTime)
Compares this instance with a specified AFTime.
Public methodStatic member
ConvertString
Converts an AFTime or AFTimeSpan input string to a different culture-specific normalized string.
Public method
Equals(Object)
Determines whether the specified Object is equal to the current object.
(Overrides ValueTypeEquals(Object).)
Public method
Equals(AFTime)
Indicates whether the current object is equal to another object of the same type.
Public method
Equals(AFTime, Double)
Indicates whether the current object is equal to another object of the same type.
Public methodStatic member
GetAbbreviatedName
Gets the culture-specific abbreviated time name of the specified invariant time name based on the specified format provider.
Public method
GetHashCode
Gets the hash code for this instance of the object which is suitable for use in hashing algorithms and data structures like a hash table.
(Overrides ValueTypeGetHashCode.)
Public methodStatic member
GetName
Gets the culture-specific full time name of the specified invariant time name based on the specified format provider.
Public method
GetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodStatic member
Parse(String, IFormatProvider)
Converts the specified string representation of a local date and time to its AFTime equivalent by using the specified culture-specific formatting information.
Public methodStatic member
Parse(String, AFTime, IFormatProvider)
Converts the specified string representation of a local date and time to its AFTime equivalent by using the specified reference time and culture-specific formatting information.
Public method
ToPIPrecision
Rounds the AFTime object to the precision supported by the PIServer.
Public method
ToString
Returns a String that represents the current object.
(Overrides ValueTypeToString.)
Public method
ToString(IFormatProvider)
Returns a String that represents the current object.
Public method
ToString(String, IFormatProvider)
Returns a String that represents the current object.
Public method
TruncateToWholeSeconds
Truncates the fractional seconds from the AFTime object.
Public methodStatic memberCode example
TryParse(String, AFTime)
Converts the specified string representation of a local date and time to its AFTime equivalent by using the CurrentCulture and returns a value that indicates whether the conversion succeeded.
Public methodStatic memberCode example
TryParse(String, AFTime, AFTime)
Converts the specified string representation of a local date and time to its AFTime equivalent by using the specified reference time and returns a value that indicates whether the conversion succeeded.
Public methodStatic memberCode example
TryParse(String, IFormatProvider, AFTime)
Converts the specified string representation of a local date and time to its AFTime equivalent by using the specified culture-specific formatting information and returns a value that indicates whether the conversion succeeded.
Public methodStatic memberCode example
TryParse(String, AFTime, IFormatProvider, AFTime)
Converts the specified string representation of a local date and time to its AFTime equivalent by using the specified reference time and culture-specific formatting information, and returns a value that indicates whether the conversion succeeded.

Operators

  NameDescription
Public operatorStatic member
Addition(AFTime, AFTimeSpan)
Adds a specified AFTimeSpan time interval to a specified AFTime, yielding a new AFTime.
Public operatorStatic member
Addition(AFTime, TimeSpan)
Adds a specified TimeSpan time interval to a specified AFTime, yielding a new AFTime.
Public operatorStatic member
Equality
The equality operator (==) compares its operands to determine if they are equal.
Public operatorStatic member
GreaterThan
The greater than relation operator (>) compares its operands to determine which one is greater than the other.
Public operatorStatic member
GreaterThanOrEqual
The greater than or equal relation operator (>=) compares its operands to determine which one is greater than or equal to the other.
Public operatorStatic member
(DateTime to AFTime)
Implicit cast operator to convert a DateTime to an AFTime.
Public operatorStatic member
(AFTime to DateTime)
Implicit cast operator to convert an AFTime to a DateTime.
Public operatorStatic member
Inequality
The inequality operator (!=) compares its operands to determine if they are not equal.
Public operatorStatic member
LessThan
The less than relation operator (<) compares its operands to determine which one is less than the other.
Public operatorStatic member
LessThanOrEqual
The less than or equal relation operator (<=) compares its operands to determine which one is less than or equal to the other.
Public operatorStatic member
Subtraction(AFTime, AFTime)
Subtracts a specified AFTime from another specified AFTime, yielding a time interval.
Public operatorStatic member
Subtraction(AFTime, AFTimeSpan)
Subtracts a specified AFTimeSpan time interval from a specified AFTime, yielding a new AFTime.
Public operatorStatic member
Subtraction(AFTime, TimeSpan)
Subtracts a specified TimeSpan time interval from a specified AFTime, yielding a new AFTime.

Fields

  NameDescription
Public fieldStatic member
MaxValue
Represents the largest possible value of AFTime.
Public fieldStatic member
MinValue
Represents the smallest possible value of AFTime.

Remarks

Represents the date and time data ranging in value from January 1, 1970 to December 31, 9999. Internally, the time is represented as a System.DateTime in Coordinated Universal Time (UTC). Actual storage of an AFTime value may cause a loss of accuracy depending on the target storage. The PI AF Server will store timestamps to an accuracy of 100 nanoseconds which is the same as the .NET DateTime precision. A PI 3.x Server stores timestamps to an accuracy of 15 microseconds.

When converting to local time (see LocalTime), time zones containing multiple daylight saving time adjustment rules are taken into account. This allows accurate representation of times outside the daylight saving time adjustment rule for the current year.

For a description of the supported string representations that can be parsed as an AFTime, see the TryParse(String, AFTime) method.

Examples

// This example shows how to use the AFTime class
// to convert between AFTime, PI Time strings, and common time strings.

// Display current time in UtcSeconds
Console.WriteLine("Now (in UtcSeconds) = {0}", AFTime.Now.UtcSeconds);

// Create an AFTime from a PITime formatted string
AFTime oneHourAgo = new AFTime("*-1h", CultureInfo.CurrentCulture);
Console.WriteLine("One Hour Ago = {0}", oneHourAgo.LocalTime);

// Create an AFTime without throwing an exception for an invalid string
string input = "Y+4dd";
AFTime result;
if (!AFTime.TryParse(input, out result))
    Console.WriteLine("Input '{0}' was not valid.", input);
else
    Console.WriteLine("Input '{0}' = {1}", input, result.LocalTime);

// Create an AFTime from a date string
AFTime y2k = new AFTime("1/1/2000 12:00:00 AM", CultureInfo.InvariantCulture);
Console.WriteLine("Y2K, Locally = {0}", y2k.LocalTime);
' This example shows how to use the AFTime class
' to convert between AFTime, PI time strings, and common time strings.

' Display current time in UtcSeconds
Console.WriteLine("Now (in UtcSeconds) = {0}", AFTime.Now.UtcSeconds)

' Create an AFTime from a PITime formatted string
Dim oneHourAgo As AFTime = New OSIsoft.AF.Time.AFTime("*-1h", CultureInfo.CurrentCulture)
Console.WriteLine("One Hour Ago = {0}", oneHourAgo.LocalTime)

' Create an AFTime without throwing an exception for an invalid string
Dim input As String = "Y+4dd"
Dim result As AFTime
If Not AFTime.TryParse(input, result) Then
    Console.WriteLine("Input '{0}' was not valid.", input)
Else
    Console.WriteLine("Input '{0}' = {1}", input, result.LocalTime)
End If

' Create an AFTime from a date string
Dim y2k As AFTime = New OSIsoft.AF.Time.AFTime("1/1/2000 12:00:00 AM", CultureInfo.InvariantCulture)
Console.WriteLine("Y2K, Locally = {0}", y2k.LocalTime)

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

// This example shows how to serialize an AFTime
AFTime time = AFTime.Now;
Console.WriteLine("Time = {0}", time.LocalTime);

// Serialize using Json
string parametersJson = JsonConvert.SerializeObject(time);
AFTime restoredJsonTime = JsonConvert.DeserializeObject<AFTime>(parametersJson);
Console.WriteLine("Restored Time from Json = {0}", restoredJsonTime.LocalTime);

// Serialize using XmlSerializer
AFTime restoredXmlTime;
using (var stream = new MemoryStream())
{
    XmlSerializer serializer = new XmlSerializer(typeof(AFTime));
    serializer.Serialize(stream, time);
    stream.Seek(0, SeekOrigin.Begin);
    restoredXmlTime = (AFTime)serializer.Deserialize(stream);
}
Console.WriteLine("Restored Time from XmlSerializer = {0}", restoredXmlTime.LocalTime);
' This example shows how to serialize an AFTime
Dim time As AFTime = AFTime.Now
Console.WriteLine("Time = {0}", time.LocalTime)

' Serialize using Json
Dim parametersJson As String = JsonConvert.SerializeObject(time)
Dim restoredJsonTime As AFTime = JsonConvert.DeserializeObject(Of AFTime)(parametersJson)
Console.WriteLine("Restored Time from Json = {0}", restoredJsonTime.LocalTime)

' Serialize using XmlSerializer
Dim restoredXmlTime As AFTime
Using stream As MemoryStream = New MemoryStream()
    Dim serializer As New XmlSerializer(GetType(AFTime))
    serializer.Serialize(stream, time)
    stream.Seek(0, SeekOrigin.Begin)
    restoredXmlTime = DirectCast(serializer.Deserialize(stream), AFTime)
End Using
Console.WriteLine("Restored Time from XmlSerializer = {0}", restoredXmlTime.LocalTime)

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

Version Information

AFSDK


See Also

TitleResults for “How to create a CRG?”Also Available in