EdgeScript provides the following time functions for use in CDN EdgeScript.
| Function | Description |
|---|---|
today() | Returns the current date as a string (yyyy-mm-dd) |
time() | Returns the current UNIX timestamp in whole seconds |
now() | Returns the current UNIX timestamp with millisecond precision |
localtime() | Returns the current local date and time as a string (yyyy-mm-dd hh:mm:ss) |
utctime() | Returns the current UTC date and time as a string (yyyy-mm-dd hh:mm:ss) |
cookie_time(sec) | Converts a UNIX timestamp to a GMT time string in cookie format |
http_time(sec) | Converts a UNIX timestamp to a GMT time string in HTTP header format |
parse_http_time(str) | Parses an HTTP-format time string and returns the corresponding UNIX timestamp |
unixtime(year, month, day, hour, min, sec) | Converts a date and time to a UNIX timestamp |
today
| Field | Detail |
|---|---|
| Syntax | today() |
| Description | Returns the current date (local time) in the format yyyy-mm-dd. |
| Parameters | None |
| Return value | String — current local date in yyyy-mm-dd format |
Example
say(concat('today:', today()))
# Output: today:2019-05-23time
| Field | Detail |
|---|---|
| Syntax | time() |
| Description | Returns the current UNIX timestamp as a whole-second integer. Use now() if you need millisecond precision. |
| Parameters | None |
| Return value | Integer — current UNIX timestamp in seconds |
Note
The UNIX timestamp represents the number of seconds elapsed since 00:00:00 UTC on January 1, 1970 (Unix epoch). It is independent of time zones.
Example
say(concat('time:', time()))
# Output: time:1559109666now
| Field | Detail |
|---|---|
| Syntax | now() |
| Description | Queries the current UNIX timestamp, excluding the fractional part of milliseconds. Unit: seconds. |
| Parameters | None |
| Return value | Current UNIX timestamp, excluding the fractional part of milliseconds. Unit: seconds. |
Note
The UNIX timestamp represents the number of seconds elapsed since 00:00:00 UTC on January 1, 1970 (Unix epoch). It is independent of time zones.
Example
say(concat('now:', now()))
# Output: now:1559109666.644localtime
| Field | Detail |
|---|---|
| Syntax | localtime() |
| Description | Returns the current local date and time in the format yyyy-mm-dd hh:mm:ss. |
| Parameters | None |
| Return value | String — current local date and time in yyyy-mm-dd hh:mm:ss format |
Example
say(concat('localtime:', localtime()))
# Output: localtime:2019-05-29 14:02:41utctime
| Field | Detail |
|---|---|
| Syntax | utctime() |
| Description | Returns the current UTC date and time in the format yyyy-mm-dd hh:mm:ss. |
| Parameters | None |
| Return value | String — current UTC date and time in yyyy-mm-dd hh:mm:ss format |
Example
say(concat('utctime:', utctime()))
# Output: utctime:2019-05-29 06:02:41cookie_time
| Field | Detail |
|---|---|
| Syntax | cookie_time(sec) |
| Description | Converts a UNIX timestamp to a GMT time string in cookie format. |
| Parameters | sec — a UNIX timestamp (integer). Call time() to get the current timestamp. |
| Return value | String — GMT time string in cookie format: Wed, 29-May-19 06:02:41 GMT |
Example
say(concat('cookie_time:', cookie_time(time())))
# Output: cookie_time:Wed, 29-May-19 06:02:41 GMThttp_time
| Field | Detail |
|---|---|
| Syntax | http_time(sec) |
| Description | Converts a UNIX timestamp to a GMT time string for use in HTTP headers, such as Last-Modified. |
| Parameters | sec — a UNIX timestamp (integer). Call time() to get the current timestamp. |
| Return value | String — GMT time string in HTTP-date format: Wed, 29 May 2019 06:02:41 GMT |
Important
The time string is always in GMT.
Example
say(concat('http_time:', http_time(time())))
# Output: http_time:Wed, 29 May 2019 06:02:41 GMTparse_http_time
| Field | Detail |
|---|---|
| Syntax | parse_http_time(str) |
| Description | Parses an HTTP-format time string and returns the corresponding UNIX timestamp. Returns false if parsing fails. |
| Parameters | str — a time string in the format Wed, 29 May 2019 06:02:41 GMT. Call http_time() to generate a valid input string. |
| Return value | Integer — UNIX timestamp if parsing succeeds; false if parsing fails |
Important
This function does not handle time zone conversion. Convert the time to GMT before passing it to this function.
Examples
# Successful parse
say(concat('parse_http_time:', parse_http_time(http_time(time()))))
# Output: parse_http_time:1559109761
# Failed parse — invalid input returns false
say(concat('parse_http_time:', parse_http_time('not-a-valid-time')))
# Output: parse_http_time:falseunixtime
| Field | Detail |
|---|---|
| Syntax | unixtime(year, month, day, hour, min, sec) |
| Description | Converts a date and time to a UNIX timestamp. |
| Parameters | year, month, day, hour, min, sec — integer values specifying the date and time components |
| Return value | Integer — UNIX timestamp corresponding to the specified date and time |
Note
The UNIX timestamp represents the number of seconds elapsed since 00:00:00 UTC on January 1, 1970 (Unix epoch). It is independent of time zones.
Examples
t = unixtime(1970, 1, 1, 8, 0, 0)
say(concat('unixtime()=', t))
# Output: unixtime()=0
# 2021-12-23 00:00:00
t = unixtime(2021, 12, 23, 0, 0, 0)
say(concat('unixtime()=', t))
# Output: unixtime()=1640188800