-
Notifications
You must be signed in to change notification settings - Fork 8
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
Create current_time function #53
Conversation
parser/mpFuncCommon.cpp
Outdated
throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent())); | ||
|
||
std::time_t t = std::time(0); | ||
std::tm now = *std::localtime(&t); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we us gmtime here instead? i was curious, i'd guess it's better to save in the user current time instead of always utc to avoid problems
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thr ideal would be to receive the timezone as argument. And use UTC as default. Is it possible?
tests.sh
Outdated
@@ -184,6 +184,9 @@ test_eval 'timediff("02:00:00", "03:30:00")' '1.5' | |||
test_eval 'timediff("03:30:00", "02:00:00")' '22.5' | |||
test_eval 'timediff("02:00:00", "02:00:30")' '0.01' | |||
|
|||
current_time=$(echo `date +"%H:%M:%S"`) | |||
test_eval 'current_time()' "\"$current_time\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is flaky, although th likelihood of failing is very low. i thought about testing it by using timediff and checking if it's 0 or 1 but i don't think that'd be better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also be tested with a regex, using egrep
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see, to check if it fits the pattern, instead of checking if it displays the current_time. that is, in fact, a possibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i decided to leave this test and add one with the regex match
parser/mpFuncCommon.cpp
Outdated
throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent())); | ||
|
||
std::time_t t = std::time(0); | ||
std::tm now = *std::localtime(&t); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thr ideal would be to receive the timezone as argument. And use UTC as default. Is it possible?
tests.sh
Outdated
@@ -184,6 +184,9 @@ test_eval 'timediff("02:00:00", "03:30:00")' '1.5' | |||
test_eval 'timediff("03:30:00", "02:00:00")' '22.5' | |||
test_eval 'timediff("02:00:00", "02:00:30")' '0.01' | |||
|
|||
current_time=$(echo `date +"%H:%M:%S"`) | |||
test_eval 'current_time()' "\"$current_time\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also be tested with a regex, using egrep
@niltonvasques it is possible to set a timezone, but it'll mess with setting system environment variables and unsetting them afterwards (which is not a platform agnostic operation, there are different commands for linux and windows, for example). unless parsec is compiled with c++20, which would allow for a system agnostic approach (but would then require adding a timezone database to the build process). here would be the base implementation (with help from mr gippity) void set_timezone(string_type tz) {
if (tz) {
setenv("TZ", tz, 1); // On Windows, use _putenv_s("TZ", tz);
} else {
unsetenv("TZ"); // Reset to the system's default timezone
}
tzset(); // Apply the timezone change
}
void FunCurrentTime::Eval(ptr_val_type &ret, const ptr_val_type *a_pArg, int a_iArgc)
{
if (a_iArgc > 1)
throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent()));
string_type original_tz = getenv("TZ");
string_type saved_tz = original_tz ? original_tz : "";
string_type timezone = a_pArg[0]->GetString();
set_timezone(timezone);
std::time_t t = std::time(0);
std::tm now = *std::localtime(&t);
if (saved_tz.empty()) {
set_timezone(nullptr); // No TZ was set initially
} else {
set_timezone(saved_tz.c_str());
}
*ret = format_time(now);
} |
What about to establish UTC and just add or subtract depending on the arg? |
@niltonvasques and then we leave it to the user to properly determine their timezone difference to UTC? i think it's way better, and it can be offloaded to the systems using Parsec to genereate this offset, still leaving space for automation. i'll implement it and change the test to egrep and set the PR to review again! |
Yes. By doing that way we can during the setup determine the project timezone and set in the equation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is missing just the timezone +1, +2....
parser/mpFuncCommon.cpp
Outdated
////------------------------------------------------------------------------------ | ||
const char_type* FunCurrentTime::GetDesc() const | ||
{ | ||
return _T("current_time() - Returns the current time in the HH:MM:SS format."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return _T("current_time() - Returns the current time in the HH:MM:SS format."); | |
return _T("current_time("+1") - Returns the current time in the HH:MM:SS format."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, i thought it best to leave it as an integer param, is that a problem? so it would be current_time(1) not current_time("+1"). otherwise it would need a parser for the offset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh! Its fine as you did! Is missing tests for that, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah! i was going to ping you when the pr was complete hehe. it is now ready
Let's remember that macos would also be a problem here |
Great job @EduardoRSeifert 👍 |
No description provided.