CkDateTime Lua Reference Documentation

chilkat.DateTime

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

CkDateTime represents a date/time value and provides conversion methods for common date/time formats, including .NET DateTime, DateTime ticks, RFC 822, RFC 3339 / ISO 8601-style timestamps, Unix time, DOS date/time, OLE DATE, NTP time, ULID timestamps, and Chilkat DtObj. It also supports date arithmetic, age/expiration checks, serialization, and local/UTC conversion.

For an extended overview, see CkDateTime Class Overview

Object Creation

local chilkat = require("chilkat")

local dt = chilkat.DateTime.new()
local dt = chilkat.DateTime.new()

Creates the object (chilkat.DateTime() does the same). Lua's garbage collector frees it when it is no longer referenced; dt:dispose() frees it at once, and in Lua 5.4 and later local dt <close> = chilkat.DateTime.new() frees it when the block ends. Calling a method of a disposed object raises an error.

Errors

A method that fails returns nil followed by the error text, as io.open does: local body, err = dt:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.

Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.

Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (dt.verbose_logging = true) and methods use the colon (dt:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.

Properties

DebugLogFilePath
-- strVal is a string
local strVal = dt.debug_log_file_path
dt.debug_log_file_path = strVal

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
IsDst
-- intVal is an integer (read-only)
local intVal = dt.is_dst

This is the Daylight Saving Time flag. It can have one of three possible values: 1, 0, or -1. It has the value 1 if Daylight Saving Time is in effect, 0 if Daylight Saving Time is not in effect, and -1 if the information is not available.

Note: This is NOT the DST for the current system time. It is the DST that was in effect at the date value contained in this object.

top
LastErrorHtml
-- strVal is a string (read-only)
local strVal = dt.last_error_html

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
-- strVal is a string (read-only)
local strVal = dt.last_error_text

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
-- strVal is a string (read-only)
local strVal = dt.last_error_xml

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
-- boolVal is a boolean
local boolVal = dt.last_method_success
dt.last_method_success = boolVal

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
UtcOffset
-- intVal is an integer (read-only)
local intVal = dt.utc_offset

For the current system's timezone, returns the number of seconds offset from UTC for this date/time. The offset includes daylight savings adjustment. Local timezones west of UTC return a negative offset.

top
VerboseLogging
-- boolVal is a boolean
local boolVal = dt.verbose_logging
dt.verbose_logging = boolVal

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
-- strVal is a string (read-only)
local strVal = dt.version

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AddDays
-- num_days is an integer
-- ok is true, or nil and err on failure
local ok, err = dt:add_days(num_days)

Adds an integer number of days to the date/time. To subtract days, pass a negative integer.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AddSeconds
-- num_seconds is an integer
-- ok is true, or nil and err on failure
local ok, err = dt:add_seconds(num_seconds)
Introduced in version 9.5.0.65

Adds an integer number of seconds to the date/time. To subtract seconds, pass a negative integer.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
DeSerialize
-- serialized_date_time is a string
dt:de_serialize(serialized_date_time)

Loads the date/time with a string having the format as produced by the Serialize method, which is a string of SPACE separated integers containing (in this order) year, month, day, hour, minutes, seconds, and a UTC flag having the value of 1/0.

top
DiffSeconds
-- date_time_arg is a chilkat.DateTime
-- n is an integer
local n = dt:diff_seconds(date_time_arg)
Introduced in version 9.5.0.65

Returns the difference in seconds between the date_time_arg and this date/time. The value returned is this object's date/time - date_time_arg's date/time. For example, if the returned value is positive, then this object's date/time is more recent than date_time_arg's date/time. If the return value is negative, then this object's date/time is older than date_time_arg's date/time.

top
ExpiresWithin
-- n is an integer
-- units is a string
-- flag is true or false
local flag = dt:expires_within(n, units)
Introduced in version 9.5.0.67

Returns true if the date/time is within n seconds/minutes/hours/days of the current system date/time. Otherwise returns false. The units can be seconds, minutes, hours, or days (plural or singular).

Returns true or false: an answer, not a success flag.

top
GetAsDateTimeTicks
-- b_local is a boolean
-- n is an integer
local n = dt:get_as_date_time_ticks(b_local)

Returns the date/time as a 64-bit integer .NET DateTime value.

b_local indicates whether a local or UTC time is returned.

This is a date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.).

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds). For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.

top
GetAsDosDate
-- b_local is a boolean
-- n is an integer
local n = dt:get_as_dos_date(b_local)

Returns the date/time as a 32-bit DOS date/time bitmask.

b_local indicates whether a local or UTC time is returned.

The DOS date/time format is a bitmask:


			   24                16                 8                 0
	    +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
	    |Y|Y|Y|Y|Y|Y|Y|M| |M|M|M|D|D|D|D|D| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s|
	    +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
	     \___________/\________/\_________/ \________/\____________/\_________/
		 year        month       day      hour       minute        second

The year is stored as an offset from 1980. Seconds are stored in two-second increments. (So if the second value is 15, it actually represents 30 seconds.)

top
GetAsIso8601
-- format_str is a string
-- b_local is a boolean
-- str is a string, or nil and err on failure
local str, err = dt:get_as_iso8601(format_str, b_local)
Introduced in version 9.5.0.65

Returns the date/time in a compatible ISO 8601 format according to the format specified in format_str.. Examples of ISO 8601 formats include the following:

    YYYY-MM-DD

    YYYY-MM-DDThh:mmTZD

    YYYY-MM-DDThh:mm:ssTZD

    YYYY-MM-DDThh:mm:ss:sssTZD
For the date portion of these formats, YYYY is a four-digit year representation, MM is a two-digit month, and DD is a two-digit day. For the time portion, hh is the hour in 24-hour notation, mm is the two-digit minutes, ss is the two-digit seconds, and sss is the 3-digit milliseconds. A time designator T separates the date and time portions of the string, while a time zone designator TZD specifies a time zone (UTC).

b_local indicates whether a local or UTC time is returned.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
GetAsOleDate
-- b_local is a boolean
-- num is a number
local num = dt:get_as_ole_date(b_local)

Returns the date/time in a Windows OLE DATE format.

b_local indicates whether a local or UTC time is returned.

The OLE automation date format is a floating point value, counting days since midnight 30 December 1899. Hours and minutes are represented as fractional days.

top
GetAsRfc822
-- b_local is a boolean
-- str is a string, or nil and err on failure
local str, err = dt:get_as_rfc822(b_local)

Returns the date/time as an RFC822 formatted string. (An RFC822 format string is what is found in the Date header field of an email, such as Wed, 18 Oct 2017 09:08:21 GMT.)

b_local indicates whether a local or UTC time is returned.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
GetAsTimestamp
-- b_local is a boolean
-- str is a string, or nil and err on failure
local str, err = dt:get_as_timestamp(b_local)
Introduced in version 9.5.0.58

Returns the date/time as an RFC 3339 formatted string, such as 1990-12-31T23:59:60Z. (This is an ISO 8061 format like the following: YYYY-MM-DDThh:mm:ssTZD)

b_local indicates whether a local or UTC time is returned.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
GetAsUnixTime
-- b_local is a boolean
-- n is an integer
local n = dt:get_as_unix_time(b_local)

Returns the date/time as a Unix time value, which is the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.

b_local indicates whether the date/time returned is local or UTC.

top
GetAsUnixTime64
-- b_local is a boolean
-- n is an integer
local n = dt:get_as_unix_time64(b_local)

The same as GetUnixTime, except returns the date/time as a 64-bit integer.

b_local indicates whether a local or UTC time is returned.

Note: 64-bit integers are needed after Jan 19, 2038. The Y2038 problem happens because some systems store Unix time as a signed 32-bit number of seconds since Jan 1, 1970. This number runs out on Jan 19, 2038, causing the time to wrap around to a date in 1901.

top
GetAsUnixTimeDbl
-- b_local is a boolean
-- num is a number
local num = dt:get_as_unix_time_dbl(b_local)

The same as GetUnixTime, except returns the date/time as a double.

b_local indicates whether a local or UTC time is returned.

top
GetAsUnixTimeStr
-- b_local is a boolean
-- str is a string, or nil and err on failure
local str, err = dt:get_as_unix_time_str(b_local)
Introduced in version 9.5.0.65

Returns the time in Unix format (in seconds since the epoch: 00:00:00 UTC on 1 January 1970).

b_local indicates whether the date/time returned is local or UTC.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
OlderThan
-- n is an integer
-- units is a string
-- flag is true or false
local flag = dt:older_than(n, units)
Introduced in version 9.5.0.67

Returns true if the date/time is older than the current system date/time by n seconds/minutes/hours/days. Otherwise returns false. The units can be seconds, minutes, hours, or days (plural or singular).

Returns true or false: an answer, not a success flag.

top
Serialize
-- str is a string, or nil and err on failure
local str, err = dt:serialize()

Serializes the date/time to a us-ascii string that can be imported at a later time via the DeSerialize method. The format of the string returned by this method is not intended to match any published standard. It is formatted to a string with SPACE separated integers containing (in this order) year, month, day, hour, minutes, seconds, and a UTC flag having the value of 1 or 0.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromCurrentSystemTime
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_current_system_time()

Sets the date/time from the current system time.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromDateTimeTicks
-- b_local is a boolean
-- ticks is an integer
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_date_time_ticks(b_local, ticks)

Sets the date/time from a .NET DateTime value represented in ticks. See GetAsDateTimeTicks for more information.

b_local indicates whether the passed in date/time is local or UTC.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromDosDate
-- b_local is a boolean
-- t is an integer
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_dos_date(b_local, t)

Sets the date/time from a 32-bit DOS date/time bitmask. See GetAsDosDate for more information.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromDtObj
-- dt is a chilkat.DtObj
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_dt_obj(dt)
Introduced in version 9.5.0.47

Sets the date/time from a Chilkat Dt object.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromNtpServer
-- json_str is a string
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_ntp_server(json_str)
Introduced in version 9.5.0.96

Sets the date/time by sending a query to an NTP server.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
SetFromNtpTime
-- ntp_seconds is an integer
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_ntp_time(ntp_seconds)
Introduced in version 9.5.0.50

Sets the date/time from a 32-bit NTP time value. ntp_seconds is the number of seconds since 00:00 (midnight) 1 January 1900 GMT.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromOleDate
-- b_local is a boolean
-- dt is a number
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_ole_date(b_local, dt)

Sets the date/time from a Windows OLE DATE value.

b_local indicates whether the passed in date/time is local or UTC.

Note: This method was not working correctly. The problem was discovered just after releasing v9.5.0.65. It will be fixed in the next version update.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromRfc822
-- rfc822_str is a string
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_rfc822(rfc822_str)

Sets the date/time from an RFC822 date/time formatted string. Here are some examples of RFC822 formatted date/times:

Tue, 15 Nov 2022 22:00:58 +0000
Tue, 15 Nov 2022 20:21:50 +0100
Tue, 01 Nov 2022 18:09:41 -0600

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromTimestamp
-- timestamp is a string
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_timestamp(timestamp)
Introduced in version 9.5.0.58

Sets the date/time from an RFC 3339 timestamp format. (such as 1990-12-31T23:59:60Z:)

(This is an ISO 8061 format like the following: YYYY-MM-DDThh:mm:ssTZD)

Note: Starting in v9.5.0.77, strings formatted as YYMMDDhhmmssZ, such as 181221132225Z, can also be passed to this method.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromUlid
-- b_local is a boolean
-- ulid is a string
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_ulid(b_local, ulid)
Introduced in version 9.5.0.94

Sets this object's date/time using the timestamp embedded in the ulid. If the ulid was not a valid ULID, returns false and the date/time is not set. b_local indicates whether the ulid uses a local time or UTC time.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
SetFromUnixTime
-- b_local is a boolean
-- t is an integer
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_unix_time(b_local, t)

Sets the date/time from a UNIX time value.

b_local indicates whether the passed in date/time is local or UTC.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromUnixTime64
-- b_local is a boolean
-- t is an integer
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_unix_time64(b_local, t)

The same as SetFromUnixTime, except that it uses a 64-bit integer to solve the Y2038 problem.

b_local indicates whether the passed in date/time is local or UTC.

Note: 64-bit integers are needed after Jan 19, 2038. The Y2038 problem happens because some systems store Unix time as a signed 32-bit number of seconds since Jan 1, 1970. This number runs out on Jan 19, 2038, causing the time to wrap around to a date in 1901.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
SetFromUnixTimeDbl
-- b_local is a boolean
-- d is a number
-- ok is true, or nil and err on failure
local ok, err = dt:set_from_unix_time_dbl(b_local, d)

The same as SetFromUnixTime, except that it uses a double to solve the Y2038 problem. (See GetAsUnixTime for more information about Y2038).

b_local indicates whether the passed in date/time is local or UTC.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
ToDtObj
-- b_local is a boolean
-- dt_obj is a chilkat.DtObj
dt:to_dt_obj(b_local, dt_obj)
Introduced in version 11.0.0

Copies the date/time to the dt_obj. Gets the date/time in the local time if b_local is true, or GMT if b_local is false.

top
UlidGenerate
-- b_local is a boolean
-- str is a string, or nil and err on failure
local str, err = dt:ulid_generate(b_local)
Introduced in version 9.5.0.94

Generates and returns a new ULID using this object's date/time value. b_local indicates whether to use local time or UTC time.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
UlidIncrement
-- ulid is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = dt:ulid_increment(ulid)
Introduced in version 9.5.0.94

Increments the ULID that is passed in ulid.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
UlidValidate
-- ulid is a string
-- ok is true, or nil and err on failure
local ok, err = dt:ulid_validate(ulid)
Introduced in version 9.5.0.94

Validates the ulid. Returns true if the ulid is valid, otherwise returns false.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top