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

Create current_time function #53

Merged
merged 9 commits into from
Nov 23, 2024
Merged

Conversation

EduardoRSeifert
Copy link
Member

No description provided.

@EduardoRSeifert EduardoRSeifert added the enhancement New feature or request label Nov 16, 2024
@EduardoRSeifert EduardoRSeifert self-assigned this Nov 16, 2024
@EduardoRSeifert EduardoRSeifert changed the title Create current time function Create current_time function Nov 16, 2024
throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent()));

std::time_t t = std::time(0);
std::tm now = *std::localtime(&t);
Copy link
Member Author

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

Copy link
Collaborator

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\""
Copy link
Member Author

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

Copy link
Collaborator

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

Copy link
Member Author

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

Copy link
Member Author

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

throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent()));

std::time_t t = std::time(0);
std::tm now = *std::localtime(&t);
Copy link
Collaborator

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\""
Copy link
Collaborator

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

@EduardoRSeifert
Copy link
Member Author

@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);
  }

@niltonvasques
Copy link
Collaborator

What about to establish UTC and just add or subtract depending on the arg? current_time(+3)

@EduardoRSeifert
Copy link
Member Author

What about to establish UTC and just add or subtract depending on the arg? current_time(+3)

@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!

@niltonvasques
Copy link
Collaborator

Yes. By doing that way we can during the setup determine the project timezone and set in the equation.

Copy link
Collaborator

@niltonvasques niltonvasques left a 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....

////------------------------------------------------------------------------------
const char_type* FunCurrentTime::GetDesc() const
{
return _T("current_time() - Returns the current time in the HH:MM:SS format.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.");

Copy link
Member Author

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

Copy link
Collaborator

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?

Copy link
Member Author

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

@EduardoRSeifert EduardoRSeifert marked this pull request as draft November 22, 2024 19:03
@EduardoRSeifert EduardoRSeifert marked this pull request as ready for review November 22, 2024 19:05
@niltonvasques niltonvasques merged commit 39ba4c4 into master Nov 23, 2024
1 check passed
@niltonvasques niltonvasques deleted the create_current_time_function branch November 23, 2024 05:07
@Victorcorcos
Copy link
Contributor

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)

Let's remember that macos would also be a problem here

@Victorcorcos
Copy link
Contributor

Great job @EduardoRSeifert 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants