Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date related macros based on UTC time #4187

Closed
Wernfried opened this issue Nov 1, 2022 · 6 comments
Closed

Date related macros based on UTC time #4187

Wernfried opened this issue Nov 1, 2022 · 6 comments

Comments

@Wernfried
Copy link

Is there any way to have date related macros (i.e. S_HOUR, S_MIN, S_SEC, S_DAY etc.) based on UTC times?

In my syslog-ng I have systems which provide local times and others provide UTC times. Thus I have different date-parsers:

options { 
   time-zone("Europe/Zurich");
};

parser p_date_time_local {
   date-parser(
      format("%Y-%m-%d %H:%M:%S"),
      template("${.FN.date} ${.FN.time}"),
      time-zone("Europe/Zurich")
   );
};

parser p_date_time_utc {
   date-parser(
      format("%Y-%m-%d %H:%M:%S"),
      template("${.HW.date} ${.HW.time}"),
      time-zone("UTC")
   );
};

In order to avoid clashes of filenames while transition of daylight-saving times I like to get hour as UTC time. I managed to develop these rewrites, but certainly these are just workarounds:

rewrite r_set_hour_local {
   set("${S_HOUR}", value("HOUR_UTC"));
   subst("^0", "", value("HOUR_UTC"));
   set("$(- ${HOUR_UTC} 1)", value("HOUR_UTC"), condition("${S_TZOFFSET}" eq "+01:00"));
   set("$(- ${HOUR_UTC} 2)", value("HOUR_UTC"), condition("${S_TZOFFSET}" eq "+02:00"));
   set("23", value("HOUR_UTC"), condition("${HOUR_UTC}" eq "-1"));
   set("22", value("HOUR_UTC"), condition("${HOUR_UTC}" eq "-2"));
};

rewrite r_set_hour_utc {
   fix-time-zone("Europe/Zurich");
   set-time-zone("UTC");
   set("${S_HOUR}", value("HOUR_UTC"));
   subst("^0", "", value("HOUR_UTC"));
};

Note, in general I like to get local time when I use S_HOUR macro. But for some parts, UTC based hour is required.
Would you have any other proposal?

Kind Regards
Wernfried

@bazsi
Copy link
Collaborator

bazsi commented Nov 2, 2022 via email

@Wernfried
Copy link
Author

I would need local times and UTC times within one message. I do some more stuff with the hour value:

 rewrite r_set_hour {
   set("${S_HOUR}", value("HOUR_UTC"));
   subst("^0", "", value("HOUR_UTC"));
   set("$(- ${HOUR_UTC} 1)", value("HOUR_UTC"), condition("${S_TZOFFSET}" eq "+01:00"));
   set("$(- ${HOUR_UTC} 2)", value("HOUR_UTC"), condition("${S_TZOFFSET}" eq "+02:00"));
   set("23", value("HOUR_UTC"), condition("${HOUR_UTC}" eq "-1"));
   set("22", value("HOUR_UTC"), condition("${HOUR_UTC}" eq "-2"));
   set("$(% ${HOUR_UTC} 2)", value("HOUR_UTC"));
};

Thus, setting time zone on source or destination level (or even global) is not sufficient.

@bazsi
Copy link
Collaborator

bazsi commented Nov 3, 2022

What about this:

#4202

This would let you do stuff like this $(format-time --time-zone UTC %H $S_UNIXTIME)

If you omit $S_UNIXTIME it takes the timestamp of the message by default (which is slightly faster, as we don't have to parse a string value). The function takes any strftime format string to format the timestamp.

@Wernfried
Copy link
Author

Wernfried commented Nov 3, 2022

What about this:

#4202

This would let you do stuff like this $(format-time --time-zone UTC %H $S_UNIXTIME)

If you omit $S_UNIXTIME it takes the timestamp of the message by default (which is slightly faster, as we don't have to parse a string value). The function takes any strftime format string to format the timestamp.

Looks very promising. Would provide full flexibility for every use case, you could even manipulate the time value, if needed.
Unix timestamps are always UTC, so I would consider --time-zone UTC as default.

@bazsi
Copy link
Collaborator

bazsi commented Feb 10, 2023

Note this PR improves this area further: #4319

@bazsi
Copy link
Collaborator

bazsi commented Feb 10, 2023

I think the previous PR should already solve this use-case, so closing this issue. Let me know if something is missing.

@bazsi bazsi closed this as completed Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants