Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sources/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ AC_PATH_PROG(RM, rm, /bin/rm)
AC_PATH_PROG(RMDIR, rmdir, /bin/rmdir)

# Checks for header files.
AC_CHECK_HEADERS([time.h limits.h stdlib.h string.h sys/time.h unistd.h])
AC_CHECK_HEADERS([time.h limits.h stdlib.h string.h sys/time.h unistd.h langinfo.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_SIZE_T
Expand Down
63 changes: 63 additions & 0 deletions sources/libjalali/jalali.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,69 @@
} \
}

/** Set locale from the environment.
*
* In case `HAVE_LANGINFO_H` (`langinfo.h` is defined), this will set the
* locale, in any other case, does nothing.
*/
void setlocale_from_env() {
#ifdef HAVE_LANGINFO_H
setlocale(LC_ALL, "");
#endif /* HAVE_LANGINFO_H */
}

char *abdow(int __item) {
#ifdef HAVE_LANGINFO_H
#define __CASE_NL(n, fallback) \
case n: \
return nl_langinfo(ABDAY_##n)
#else /* !HAVE_LANGINFO_H */
#define __CASE_NL(n, fallback) \
case n: \
return (char *)fallback
#endif /* HAVE_LANGINFO_H */

switch (__item) {
__CASE_NL(1, "Sun");
__CASE_NL(2, "Mon");
__CASE_NL(3, "Tue");
__CASE_NL(4, "Wed");
__CASE_NL(5, "Thu");
__CASE_NL(6, "Fri");
__CASE_NL(7, "Sat");
default:
return (char *)"";
}

#undef __CASE_NL
}

char *dow(int __item) {
#ifdef HAVE_LANGINFO_H
#define __CASE_NL(n, fallback) \
case n: \
return nl_langinfo(DAY_##n)
#else /* !HAVE_LANGINFO_H */
#define __CASE_NL(n, fallback) \
case n: \
return (char *)fallback
#endif /* HAVE_LANGINFO_H */

switch (__item) {
__CASE_NL(1, "Sunday");
__CASE_NL(2, "Monday");
__CASE_NL(3, "Tuesday");
__CASE_NL(4, "Wednesday");
__CASE_NL(5, "Thursday");
__CASE_NL(6, "Friday");
__CASE_NL(7, "Saturday");
default:
return (char *)"";
}

#undef __CASE_NL
}

const int cycle_patterns[] = {J_PT0, J_PT1, J_PT2, J_PT3, INT_MAX};
const int leaps[] = {J_L0, J_L1, J_L2, J_L3, INT_MAX};

Expand Down
42 changes: 42 additions & 0 deletions sources/libjalali/jalali.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,48 @@ extern "C" {

#define LIBJALALI_VERSION "0.5.1"

#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#include <locale.h>
#endif /* HAVE_LANGINFO_H */

/** Set locale from the environment.
*
* In case `HAVE_LANGINFO_H` (`langinfo.h` is defined), this will set the
* locale, in any other case, does nothing.
*/
void setlocale_from_env();

/** Return the day of week in a locale or English for 1 to 7 (Sun = 1).
*
* In case `HAVE_LANGINFO_H` (`langinfo.h` is defined), this will use `DAY_n`
* in localized context, otherwise, it's just hardcoded English days of week.
*
* @return the localized name of the day of the week or empty if not 1-7.
*/
char *dow(int __item);

/** Return the first 3 letters of `dow` in a locale for 1 to 7 (Sun = 1).
*
* This function is exactly as `dow`, see that for more information.
*
* There is no guarantee that this will return a string equal to 3, less or
* more since this might be set from the locales. The user must check the length
* and prepare proper paddings and else when using the abbrevations. Also, this
* string is ASCII hence slicing into it manually may lead to invalid
* characters.
*
* For this very reason, there is no "3 letters only" or "2 letters only"
* variations of this function as before.
*/
char *abdow(int __item);

/** Do as in `dow` but Saturday is 0 instead of 7. */
#define DOW06(n) (dow((n == 0) ? 7 : n))

/** Do as in `abdow` but Saturday is 0 instead of 7. */
#define ABDOW06(n) (abdow((n == 0) ? 7 : n))

struct jtm {
int tm_sec; /* Seconds. (0-59) */
int tm_min; /* Minutes. (0-59) */
Expand Down
Loading