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 weekday(date) function #50

Merged
merged 9 commits into from
Sep 18, 2024

Conversation

EduardoRSeifert
Copy link
Member

@EduardoRSeifert EduardoRSeifert commented Sep 2, 2024

Create the weekday(date/datetime) function that takes in a date and returns the number of days since sunday(returns the day of the week in int form, 0 = sunday, 6 = saturday).

Make support these languages as option argument, otherwise return the numbers [0-6]:

  • en
  • nb (norwegean)
  • pt-BR
  • es-ES
  • fr-FR
  • de-DE
  • zh-CN (Chinese)
  • th-TH (Thai)

Tests Passed !

image

Comment on lines +306 to +308
# weekday with date_time
test_eval 'weekday("2014-03-21T21:03")' '5'
test_eval 'weekday("2015-03-21T21:03")' '6'
Copy link
Member

Choose a reason for hiding this comment

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

Cool! it also works!

@EduardoRSeifert
Copy link
Member Author

Should i add the changes to the Makefile? it doesn't seem to make sense for me since that it's version controlled since it's generated by cmake

Copy link
Member

@Marinofull Marinofull left a comment

Choose a reason for hiding this comment

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

Great job!

@Marinofull
Copy link
Member

Marinofull commented Sep 5, 2024

$ cmake CMakeLists.txt
$ make
$ ./example
parsec> weekday("2024-09-05", "pt-BR")
Result (type: 's'):
ans = "Quinta-feira"
parsec> weekday("2024-09-06", "en")
Result (type: 's'):
ans = "Friday"
parsec> weekday("2024-09-04")
Result (type: 'i'):
ans = 3

Comment on lines +474 to +480
string_type locales[8] = {"en", "nb", "pt-BR", "es-ES", "fr-FR", "de-DE", "zh-CN", "th-TH"};

for (int i = 0; i < 8; i++) {
if(locale == locales[i]) {
ret = localized_weekdays[i][week_day];
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

you could use a map here to achieve O(1). int index = locales[locale]

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 know, but map lookup is only amortized O(1). with n=8, you can see the difference is infinitesimal (in this print array method wins by 4ms but it's basically the same, i ran it a lot of times to check
image

therefore, i don't think the inclusion of the lib is warrented.

scripts used:

string_method

#include <iostream>
#include <string>

using namespace std;

int main(){
  string localized_weekdays[8][7] = {
      {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
      {"Søndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag"},
      {"Domingo", "Segunda-Feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"},
      {"Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sabado"},
      {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"},
      {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"},
      {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"},
      {"วันอาทิตย์", "วันจันทร์", "วันอังคาร", "วันพุธ", "วันพฤหัสบดี", "วันศุกร์", "วันเสาร์"}
    };
  string locales[8] = {"en", "nb", "pt-BR", "es-ES", "fr-FR", "de-DE", "zh-CN", "th-TH"};

  string locale, ret;
  int week_day;
  while(cin >> locale >> week_day){
    for (int i = 0; i < 8; i++) {
      if(locale == locales[i]) {
        ret = localized_weekdays[i][week_day];
      }
    }
    cout << ret << endl;
  }
}

map method

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  map<string, vector<string>> localized_weekdays = {
      {"en", {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}},
      {"nb", {"Søndag", "Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag", "Lørdag"}},
      {"pt-BR", {"Domingo", "Segunda-Feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado"}},
      {"es-ES", {"Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sabado"}},
      {"fr-FR", {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"}},
      {"de-DE", {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}},
      {"zh-CN", {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}},
      {"th-TH", {"วันอาทิตย์", "วันจันทร์", "วันอังคาร", "วันพุธ", "วันพฤหัสบดี", "วันศุกร์", "วันเสาร์"}}
  };

  string locale, ret;
  int week_day;

  while(cin >> locale >> week_day){
    ret = localized_weekdays[locale][week_day];
    cout << ret << endl;
  }
}

timer.sh

#!/bin/bash

# Run the array method 1 million times
echo "Running the array method 1 million times"
start_time1=$(date +%s%N) # Record start time
./array_method < input.txt > /dev/null
end_time1=$(date +%s%N) # Record end time
total_time1=$(((end_time1 - start_time1)/1000000))
echo "Time taken for array_method: $total_time1 miliseconds"

# Run the map method 1 million times
echo "Running the map method 1 million times"
start_time2=$(date +%s%N) # Record start time
./map_method < input.txt > /dev/null
end_time2=$(date +%s%N) # Record end time
total_time2=$(((end_time2 - start_time2)/1000000))
echo "Time taken for map method: $total_time2 miliseconds"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice bench! Lets keep it simple than!

Comment on lines +1002 to +1006
if (a_iArgc < 1) {
throw ParserError(ErrorContext(ecTOO_FEW_PARAMS, GetExprPos(), GetIdent()));
} else if (a_iArgc > 2) {
throw ParserError(ErrorContext(ecTOO_MANY_PARAMS, GetExprPos(), GetIdent()));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmmm! We can write variable arity functions! This will make our case strategy simpler!

test_eval 'weekday("2016-03-21", "zh-CN")' '"星期一"'
test_eval 'weekday("2017-03-21", "fr-FR")' '"Mardi"'
test_eval 'weekday("2018-03-21", "th-TH")' '"วันพุธ"'
test_eval 'weekday("2019-03-21", "pt-BR")' '"Quinta-feira"'
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing tests with unknow locale and invalid date

Copy link
Member Author

Choose a reason for hiding this comment

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

will add!

Copy link
Member Author

@EduardoRSeifert EduardoRSeifert Sep 13, 2024

Choose a reason for hiding this comment

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

okay, it seems like this tests.sh does not test for thrown errors. i think tests like these are added on the parsec gem, like this

this test.sh assumes that errors won't be thrown

Copy link
Member

Choose a reason for hiding this comment

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

@niltonvasques tests for thrown errors are done in parsec layer, can we merge this and proceed to parsec PR?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is possible guys @EduardoRSeifert and @Marinofull, to catch crashs on bash: See

https://medium.com/@dirk.avery/the-bash-trap-trap-ce6083f36700

But looking parsec code, seems that it is also possible to get the syntax error:

https://github.com/oxeanbits/parsec/blob/3701864b947448754e3b7e3978cdd798fb319936/lib/parsec.rb#L81-L86

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we have tests in the parsec gem that check for syntax error, as i said in my previous comment

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thats not the reason that I posted the parsec link here. In my link you can see that is possible to collect the syntax error from the libnativemath output, thus you can also write tests here on the bash.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In bash you can also catch crashs/fatals as you can see in the first link.

Comment on lines +325 to +327
test_eval 'weekday("2019-03-21", "invalidlocale")' 'The chosen locale is not supported'
test_eval 'weekday("2024-32-21")' 'Invalid format on the parameter(s). Please use two "yyyy-mm-dd" for dates OR two "yyyy-mm-ddTHH:MM" for date_times'
test_eval 'weekday("2024-09-17", "en", "one too many params")' 'Too many parameters passed to function'
Copy link
Member Author

Choose a reason for hiding this comment

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

@@ -111,6 +111,8 @@ MUP_NAMESPACE_START
m_vErrMsg[ecADD_HOURS_DATE] = _T("The first parameter could not be converted to a date. Please use the format: \"yyyy-mm-dd\"");
m_vErrMsg[ecADD_HOURS_DATETIME] = _T("The first parameter could not be converted to a date time. Please use the format: \"yyyy-mm-ddTHH:MM\"");
m_vErrMsg[ecINVALID_TYPES_MATCH] = _T("Both values of the default(x, y) function should have the same type");
m_vErrMsg[ecUKNOWN_LOCALE] = _T("The chosen locale is not supported");
m_vErrMsg[ecINVALID_TIME_FORMAT] = _T("Invalid time format on parameter(s). Please use 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.

without this error message the program was filing because of it missing, which would stop the tests script from getting the error message. it was not added in the PR that used it

Copy link
Collaborator

Choose a reason for hiding this comment

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

I love tests because their incredible ability to highlight these corner cases. 👏

@niltonvasques niltonvasques merged commit 3e0e34f into master Sep 18, 2024
1 check passed
@niltonvasques niltonvasques deleted the dpmsm-18471-create_weekday_function branch September 18, 2024 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants