Class
class DateTimeFormat
public class DateTimeFormat {
public static prop RFC1123: DateTimeFormat
public static prop RFC3339: DateTimeFormat
public prop format: String
}
Description: Provides time formats for parsing and generating DateTime.
static prop RFC1123
public static prop RFC1123: DateTimeFormat
Description: Provides the RFC1123 time format. The time string format is www, dd MMM yyyy HH:mm:ss z
.
Type: DateTimeFormat
static prop RFC3339
public static prop RFC3339: DateTimeFormat
Description: Provides the RFC3339 time format. The time string format is yyyy-MM-ddTHH:mm:ssOOOO
.
Type: DateTimeFormat
prop format: String
public prop format: String
Description: Specifies the string format of the DateTimeFormat instance.
Type: String
static func of(String)
public static func of(format: String): DateTimeFormat
Description: Creates a specific DateTimeFormat instance based on a string.
For details about the string formats, see Time String Formats.
Parameters:
- format: String: string format
Returns:
- DateTimeFormat: time format
Throws:
- IllegalArgumentException: If the input parameter format does not meet the requirement on the number of letters in Time String Formats, this exception is thrown.
class TimeZone
public class TimeZone <: ToString & Equatable<TimeZone> {
public init(id: String, offset: Duration)
}
Description: Indicates the time zone. It records the time offsets of a region relative to the zero time zone at different times. It provides functionality such as loading a time zone from the system and customizing a time zone.
Parent Type:
static let Local
public static let Local: TimeZone
Description: Obtains the local time zone.
Local
obtains a time zone ID from the system environment variable TZ and loads the time zone from the system time zone file based on the time zone ID. The behavior is the same as that of the load function.
The value of the environment variable TZ is in a standard time zone ID format, for example, Asia/Shanghai. All operating systems adopt the same time zone ID format.
If the environment variable TZ is not set or is empty, the rules for loading the local time zone are as follows:
- In the Linux or Unix like system, the system path /etc/localtime is loaded. The time zone name is the same as the name of the relative path to which /etc/localtime points, for example, Asia/Shanghai.
- If the loading fails or the system is Windows, the time zone whose ID is UTC&offset is returned, for example, UTC+08:00 corresponding to Asia/Shanghai.
Type: TimeZone
static let UTC
public static let UTC: TimeZone
Description: Obtains the UTC time zone.
Type: TimeZone
prop id
public prop id: String
Description: Obtains the time zone ID associated with the current TimeZone instance.
Type: String
init(String, Duration)
public init(id: String, offset: Duration)
Description: Constructs a custom TimeZone instance using the specified time zone ID and offset.
Parameters:
- id: String: time zone ID A slash (/) is used as the delimiter, for example, Asia/Shanghai. All operating systems adopt the same rule.
- offset: Duration: offset relative to the UTC time zone, accurate to seconds. The offset is positive for time zones east of UTC and negative for time zones west of UTC. The value range is (-25 * Duration.hour, 26 * Duration.hour).
Throws:
- IllegalArgumentException: This exception is thrown if the input
id
is a null string oroffset
exceeds the valid range.
static func load(String)
public static func load(id: String): TimeZone
Description: Loads the time zone specified by the parameter id
from the system.
- In Linux, macOS, and HarmonyOS systems, if the environment variable CJ_TZPATH exists, the time zone file is loaded from the path specified by the environment variable. If there are multiple values for the environment variable separated by colons (:), the time zone files are searched based on the order of paths, and the time zone file first found is loaded. Otherwise, the time zone is loaded from the system time zone file at /usr/share/zoneinfo for Linux and macOS and /system/etc/zoneinfo for HarmonyOS.
- In Windows, you need to download and compile a Time Zone File, and set the environment variable CJ_TZPATH to point to the zoneinfo directory. If there are multiple values for the environment variable separated by semicolons (;), the time zone files are searched based on the order of paths, and the time zone file first found is loaded. Otherwise, an exception occurs.
Parameters:
- id: String: time zone ID
Returns:
- TimeZone: time zone
Throws:
- IllegalArgumentException: This exception is thrown if the parameter
id
is empty, its length exceeds 4096 bytes, or the parameter does not comply with the standard time zone ID format. - InvalidDataException: This exception is thrown if loading the time zone file fails (for example, the file cannot be found or parsing the file fails).
static func loadFromPaths(String, Array<String>)
public static func loadFromPaths(id: String, tzpaths: Array<String>): TimeZone
Description: Loads the time zone specified by the parameter id
from the time zone file directory specified by the parameter tzpaths
.
During time zone loading, the time zone is loaded from the path of the first time zone file that is successfully read. The time zone file format must comply with the Time Zone Information Format.
Parameters:
Returns:
- TimeZone: time zone to be loaded
Throws:
- IllegalArgumentException: This exception is thrown if the parameter
id
is empty, its length exceeds 4096 bytes, or the parameter does not comply with the standard time zone ID format. - InvalidDataException: This exception is thrown if loading the time zone file fails (for example, the file cannot be found or parsing the file fails).
static func loadFromTZData(String, Array<UInt8>)
public static func loadFromTZData(id: String, data: Array<UInt8>): TimeZone
Description: Constructs a custom TimeZone instance using the specified time zone ID and time zone data. id
can be any valid string, and data
must comply with the IANA time zone file format. If the loading is successful, the TimeZone instance is created. Otherwise, an exception is thrown.
Parameters:
- id: String: time zone ID
- data: Array<UInt8>: data complying with the Time Zone Information Format
Returns:
- TimeZone: time zone to be loaded
Throws:
- IllegalArgumentException: If
id
is empty, this exception is thrown. - InvalidDataException: If
data
parsing fails, this exception is thrown.
func toString()
public func toString(): String
Description: Obtains the string representation of the time zone ID in the current TimeZone instance.
Returns:
- String: string representation of the time zone ID
operator func !=(TimeZone)
public operator func !=(r: TimeZone): Bool
Description: Checks whether the reference of the current TimeZone instance is not equal to the reference of r
.
Parameters:
Returns:
- Bool:
true
orfalse
. If the reference of the current TimeZone instance is not equal to the reference ofr
,true
is returned; otherwise,false
is returned.
operator func ==(TimeZone)
public operator func ==(r: TimeZone): Bool
Description: Checks whether the reference of the current TimeZone instance is equal to the reference of r
.
Parameters:
Returns: