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

Use system time for holidays #36628

Closed
Pgj1997 opened this issue Jan 2, 2020 · 9 comments · Fixed by #39897
Closed

Use system time for holidays #36628

Pgj1997 opened this issue Jan 2, 2020 · 9 comments · Fixed by #39897
Labels
[C++] Changes (can be) made in C++. Previously named `Code` <Enhancement / Feature> New features, or enhancements on existing Good First Issue This is a good first issue for a new contributor (P5 - Long-term) Long-term WIP, may stay on the list for a while.

Comments

@Pgj1997
Copy link

Pgj1997 commented Jan 2, 2020

I've noticed that we've been manually changing the title screens for the holiday variants. I don't think it's really necessary to make an entirely new patch update just for a small Easter egg. I have a better solution: system time. The game will pull from the system's internal clock, and will display whatever title screen based on that. I did a bit of digging to see if C++ supports pulling from system time, and, with my very little knowledge on programming code from scratch, here's what I got.

#include <ctime>

int main() {

time_t currentTime;
struct tm *local time;

time( &currentTime );
 localTime = localtime( &currentTime );

int month = localTime->tm_mon; 
init day = localTime->tm_mday;
init wday = localTime->tm_wday;


if((month = 0) && (day = 1))
	static const holiday current_holiday holiday::new_year;

if((month = 2) && (day ≥ 21) && (wday = 0))
	static const holiday current_holiday holiday::easter;

if((month = 3) && (day ≤ 7) && (wday = 0))
	static const holiday current_holiday holiday::easter;

if((month = 6) && (day = 4))
	static const holiday current_holiday holiday::independence_day;

if(month = 9)
	static const holiday current_holiday holiday::halloween;

if((month = 10) && (day = 25) && (wday = 4))
	static const holiday current_holiday holiday::thanksgiving;

if((month = 10) && (day = 26) && (wday = 4))
	static const holiday current_holiday holiday::thanksgiving;

if((month = 11) && (day ≤ 25))
	static const holiday current_holiday holiday::christmas;

ifElse
	static const holiday current_holiday holiday::none;
}
@KorGgenT KorGgenT added the <Suggestion / Discussion> Talk it out before implementing label Jan 2, 2020
@Pgj1997 Pgj1997 changed the title Using system time for holidays Use system time for holidays Jan 2, 2020
@kevingranade
Copy link
Member

Thanks a ton, I somehow missed this part of the language standard the last time I was looking for a solution.

@kevingranade
Copy link
Member

References
https://en.cppreference.com/w/cpp/chrono/c/localtime
https://en.cppreference.com/w/cpp/chrono/c/tm

Ideally we'd have a json calendar of future holidays we update yearly or something, though there's no real reason it can't be updated for the next decade or so.

@Pgj1997
Copy link
Author

Pgj1997 commented Jan 3, 2020

Ideally we'd have a json calendar of future holidays we update yearly or something, though there's no real reason it can't be updated for the next decade or so.

For holidays like Easter and Thanksgiving? Yeah, that'd probably be handy.

I trust that my code for Thanksgiving will work (found out that Thanksgiving is always on either the 25th or 26th), but not really my Easter code. Tried doing the math, and I just assumed it isn't mathematically possible for Easter to occur on anything past April 7th.

@Pgj1997
Copy link
Author

Pgj1997 commented Feb 1, 2020

So what exactly is stopping us from adding this?

@anothersimulacrum
Copy link
Member

Someone doing it.

@stale
Copy link

stale bot commented Mar 2, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Please do not 'bump' or comment on this issue unless you are actively working on it. Stale issues, and stale issues that are closed are still considered.

@stale stale bot added the stale Closed for lack of activity, but still valid. label Mar 2, 2020
@kevingranade kevingranade added (P5 - Long-term) Long-term WIP, may stay on the list for a while. <Enhancement / Feature> New features, or enhancements on existing [C++] Changes (can be) made in C++. Previously named `Code` Good First Issue This is a good first issue for a new contributor and removed <Suggestion / Discussion> Talk it out before implementing labels Mar 3, 2020
@stale stale bot removed the stale Closed for lack of activity, but still valid. label Mar 3, 2020
@Pgj1997
Copy link
Author

Pgj1997 commented Mar 11, 2020

It seriously can't be that hard to implement, right?

Would the code I gave not work?

@anothersimulacrum
Copy link
Member

The code you gave would not work, but could be very easily turned into actual C++ instead of psuedocode and work (though there are some things that are wrong there regardless of pseudocode/actual C++.

@anothersimulacrum
Copy link
Member

anothersimulacrum commented Mar 12, 2020

e.g.

holiday get_holiday_from_time()
{
    std::tm local_time;

    std::time_t current_time = std::time( nullptr );
    local_time = std::localtime( &currentTime );

    int month = local_time.tm_mon; 
    int day = local_time.tm_mday;
    int wday = local_time.tm_wday;

    if( month == 0 && day == 1 ) {
        return holiday::new_year;
    } else if( ( month == 2 && day >= 21 ) ||
               ( month == 3 && day <= 7 ) && wday == 0 ) {
        return holiday::easter;
    } else if( month == 6 && day == 4 ) {
        return holiday::independence_day;
    } else if( month == 9 ) {
        return holiday::halloween;
    } else if( month == 10 && ( day == 25 || day == 26 ) && wday == 4 ) {
        return holiday::thanksgiving;
    } else if( month == 11 && day <= 25 ) {
        return holiday::christmas;
    } else {
        return holiday::none;
    }
}

This probably works, no guarantees.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` <Enhancement / Feature> New features, or enhancements on existing Good First Issue This is a good first issue for a new contributor (P5 - Long-term) Long-term WIP, may stay on the list for a while.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants