Awk time functions: systime, mktime, strftime

Today, I would like to share with you how to use the awk time function:

  • systime
    Get the timestamp and return the whole seconds from January 1, 1970 to the current time (excluding leap years)
  • mktime
    Create the specified time and convert it to timestamp
  • strftime
    Format the time output and convert the timestamp to a time string

Syntax

awk 'pattern {  action }'

Example

In the following example, we use the awk systime function to get the current timestamp.

awk 'BEGIN{nowTimestamp=systime();print nowTimestamp;}'

In the following example, we use the awk mktime function to create a timestamp at the specified time.

awk 'BEGIN{print "Number of seconds since the Epoch = " mktime("2022 10 08 20 20 10")}'

In the following example, we use the awk strftime function to output the timestamp formatted time string.

awk 'BEGIN {print strftime("Time = %m/%d/%Y %H:%M:%S", 1665140876)}'

Add a Comment

Your email address will not be published. Required fields are marked *