0

I need to obtain hour and weekday into variables to be used in config file.

Hour can be obtain from $time_iso8601 using a regexp map, but it's overkill (like I said: based on regexp).

For weekday I cannot find any solution (not even a bad one).

Is there a better solution to implement those in nginx ?

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
Alex
  • 133
  • 1
  • 9

1 Answers1

0

I won't call that one an overkill, especially if you have a PCRE JIT enabled nginx build (requires support from the system PCRE library). To get the weekday (as well as an hour too) you can use the $date_gmt/$date_local variables from the ngx_http_ssi_module in conjunction with the map block. I don't find a way to return the time string using custom strftime() format, it seems that the mentioned timefmt parameter is supported only in SSI commands used within the <!-- ... --> blocks of post-processed response body. You should be aware that the strftime() result for the %a/%A/%b/%B/%c format modifiers can be locale-dependent on some systems/nginx builds. You can also check this SO thread, depending on how are you going to use those values it may be useful for you too.

I doubt there is a better solution with the vanilla nginx, but if you are not limited to built-in modules only, there is a set-misc-nginx-module which has the set_formatted_gmt_time / set_formatted_local_time directives where you can specify an exact strftime() format (%u/%w/%H). This module is also a part of the OpenResty bundle.

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37