JCTIME(3)

πŸ“¦
version 0.5.1 Β· 2 versions
, in debian trixie, nixos 26.05, nixpkgs unstable, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– JCTIME(3) πŸ’–

okay bestie, welcome to JCTIME(3) β€” straight out of the libjalali Manual, and it's a whole journey. This is part 1, so grab a boba and lock in. πŸ§‹βœ¨


🌈 NAME 🌈

jasctime, jctime, jgmtime, jlocaltime, jmktime, jasctime_r, jctime_r, jgmtime_r, jlocaltime_r β€” the girlies that transform a jalali date and time into either a broken-down jalali time or straight-up ASCII. πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <jtime.h>

char *jasctime(const struct jtm *jtm);
char *jasctime_r(const struct jtm *jtm, char *buf);

char *jctime(const time_t *timep);
char *jctime_r(const time_t *timep, char *buf);

struct jtm *jgmtime(const time_t *timep);
struct jtm *jgmtime_r(const time_t *timep, struct jtm *result);

struct jtm *jlocaltime(const time_t *timep);
struct jtm *jlocaltime_r(const time_t *timep, struct jtm *result);

time_t jmktime(struct jtm *jtm);

#include <jalali.h>

int jalali_is_jleap(int year);

void jalali_create_time_from_secs(time_t time, struct ab_jtm* ab_jtm);
time_t jalali_create_secs_from_time(const struct ab_jtm* ab_jtm);

int jalali_create_date_from_days(struct jtm* jtm);
int jalali_create_days_from_date(struct jtm* jtm);

void jalali_get_jyear_info(struct jyinfo* jyinfo);

void jalali_get_date(int p, struct jtm* jtm);
int jalali_get_diff(const struct jtm* jtm);

void jalali_update(struct jtm* jtm);

and when you compile, link with -ljalali or nothing works, no cap. πŸ”—


✨ DESCRIPTION ✨

So jctime(), jgmtime() and jlocaltime() all take one lil argument of type time_t that represents calendar time. And when you read it as an absolute time value? It's literally the number of seconds that have elapsed since the Epoch β€” that's 1970-01-01 (1348-10-11 in Jalali), 00:00:00 +0000 (UTC). ⏳

Meanwhile jasctime() and jmktime() both take an argument that's a broken-down jalali time β€” meaning it's been separated into year, month, day, etc. πŸ—“οΈ

Broken-down jalali time lives in the structure jtm, which is defined in <jtime.h> like this:

struct jtm {
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};

And here's the rundown of every jtm member, one by one, no skips:

  • tm_sec β€” the number of seconds after the minute, range 0 to 59. ⏱️
  • tm_min β€” the number of minutes after the hour, range 0 to 59. ⏱️
  • tm_hour β€” the number of hours past midnight, range 0 to 23. ⏱️
  • tm_mday β€” the day of the month, range 1 to 31. πŸ“…
  • tm_mon β€” the number of months since Farvadin, range 0 to 11. πŸŒ™
  • tm_year β€” absolute year number, century included. πŸ‘‘
  • tm_wday β€” the number of days since Saturday, range 0 to 6. πŸ—“οΈ
  • tm_yday β€” the number of days since Farvadin 1, range 0 to 365. πŸ—“οΈ
  • tm_isdst β€” a flag saying whether daylight saving time is in effect at the described time. Positive value = DST is in effect, zero = it isn't. β˜€οΈ

Now, info about a certain year in the jalali system is stored in the structure jyinfo, formatted like this:

struct jyinfo {
    int lf;                /* leap indicator flag */
    int y;                 /* year */
    int r;                 /* reamining years in grand cycle */
    int p;                 /* passed years from grand cycle*/
    int rl;                /* remaining leap years in grand cycle */
    int pl;                /* passed leap years in grand cycle */
    int apl;               /* absolute passed leaps */
};

And the members of jyinfo, decoded for you:

  • lf β€” a flag that says whether a year is leap or not. 🐸
  • y β€” absolute year number, century included. πŸ‘‘
  • r β€” the number of remaining years in the grand cycle. (That cycle is 2820 years long, fyi.) πŸ”
  • p β€” the number of passed years in the grand cycle. ⏭️
  • rl β€” the number of remaining leap years in the grand cycle. 🐸
  • pl β€” the number of passed leap years in the grand cycle. ⏭️
  • apl β€” the absolute number of passed leaps since Epoch. 🌌

The internal jalali date functions use passed days since the UTC Epoch to work out date and time. And to store all that, this structure is also defined:

struct ab_jtm {
    int ab_sec;
    int ab_min;
    int ab_hour;
    int ab_days;
};

And the members of ab_jtm go like this:

  • ab_sec β€” the number of seconds. ⏱️
  • ab_min β€” the number of minutes. ⏱️
  • ab_hour β€” the number of hours. ⏱️
  • ab_days β€” the absolute number of days since UTC Epoch. (1348-10-11) 🌍

So the call jctime(t) is equivalent to jasctime(jlocaltime(t)). It converts the calendar time t into a null-terminated string shaped like:

"Jom Kho 06 22:59:17 1390\n"

The abbreviations for the days of the week are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", and "Sat". And the Farsi transliteration for the days of the week are "Sha", "Yek", "Dos", "Ses", "Cha", "Pan", and "Jom". The abbreviations for the months are "Far", "Ord", "Kho", "Tir", "Mor", "Sha", "Meh", "Aba", "Aza", "Dey", "Bah", and "Esf". πŸ’–

⚠️ The return value points to a statically allocated string, so it might get overwritten by later calls to any of the date and time functions. Not a forever girl, she's reusable. 😭

The reentrant version jctime_r() does the same thing, except it stores the string in a user-supplied buffer, which needs room for at least 26 bytes. πŸ“¦


jgmtime() converts the calendar time timep into a broken-down jalali time representation, expressed in Coordinated Universal Time (UTC). The return value points to a statically allocated struct that might be overwritten by subsequent calls to any of the jalali date and time functions. jgmtime_r() does the same, but stores the data in a user-supplied struct. The function acts as if it called tzset(3). 🌍

jlocaltime() converts the calendar time timep into a broken-down jalali time representation, expressed relative to the user's specified timezone. The function acts as if it called tzset(3) and sets the external variables tzname with info about the current timezone, timezone with the difference between Coordinated Universal Time (UTC) and local standard time in seconds, and daylight to a nonzero value if daylight savings time rules apply during some part of the year. The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the jalali date and time functions. jlocaltime_r() does the same, but stores the data in a user-supplied struct. πŸ•°οΈ

jasctime() converts the broken-down jalali time value jtm into a null-terminated string with the same format as jctime(). The return value points to a statically allocated string which might be overwritten by subsequent calls to any of the date and time functions. jasctime_r() does the same, but stores the string in a user-supplied buffer which should have room for at least 26 bytes. πŸ“¦

jmktime() converts a broken-down jalali time structure, expressed as local time, into calendar time representation. It ignores the values supplied by the caller in the tm_wday field. πŸ™ˆ The value specified in tm_isdst informs jmktime() whether or not daylight saving time (DST) is in effect for the time supplied in the jtm structure: a positive value means DST is in effect; zero means that DST is not in effect. β˜€οΈ

jmktime() also modifies the fields of the jtm structure like this: tm_wday and tm_yday get set to values determined from the contents of the other fields; and if structure members are outside their valid interval, they get normalized (so for example, 40 Bahman is changed into 10 Esfand β€” she will fix your mess πŸ’…). Calling jmktime() also sets the external variable tzname with information about the current timezone. 🌍


There are a whole bunch of non-standard functions provided too, for working with jalali date and time:

jalali_is_jleap() returns an integer indicating whether the year specified is leap or not. It returns 1 on the event of encountering a leap year, 0 otherwise. 🐸

jalali_create_time_from_secs() fills out the ab_jtm structure members based on the absolute number of seconds elapsed since UTC Epoch. ⏳

jalali_create_secs_from_time() is the converse function to jalali_create_time_from_secs(), and it returns the absolute number of seconds elapsed since UTC Epoch based on the supplied ab_jtm structure. πŸ”„

jalali_create_date_from_days() alters the tm_mon and tm_mday fields of the broken-down jalali time structure based on its tm_yday field. It returns -1 on the event of encountering any errors, and the structure fields remain untouched. 😭

jalali_create_days_from_date() alters the tm_yday field of the broken-down jalali time structure based on its tm_mon and tm_mday fields. It returns -1 on the event of encountering any errors, and the structure fields remain untouched. 😭

jalali_get_jyear_info() modifies jyinfo structure fields to match information for the year specified by its y field. Info about a year in the jalali system includes the leap flag, passed and remaining years in the grand cycle, passed and remaining leap years in the grand cycle, and the absolute number of passed leaps since UTC Epoch. 🌌

jalali_get_date() calculates the jalali date based on the number of days since UTC epoch. It alters the broken-down jalali time structure fields accordingly. πŸ“…

jalali_get_diff() is the converse function of jalali_get_date(), and it calculates the number of days passed since UTC Epoch based on a broken-down jalali time structure supplied to it. πŸ”„

jalali_update() updates the tm_wday and tm_yday fields of the broken-down jalali time structure based on its tm_year, tm_mon, tm_mday, tm_hour, tm_min and tm_sec fields. If structure members are outside their valid interval, they get normalized (so, again, 40 Bahman becomes 10 Esfand πŸ’…). tm_isdst, tm_gmtoff and tm_zone fields are set accordingly. ✨


🌸 EXAMPLES 🌸

This lil program converts a jalali date to gregorian, and it's giving main character energy:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <jalali.h>
#include <jtime.h>

int
main(int argc, char ** argv)
{
    struct tm tm;
    struct jtm jtm;
    time_t t;

    jtm.tm_year = atoi(argv[1]);
    jtm.tm_mon = atoi(argv[2]);
    jtm.tm_mday = atoi(argv[3]);

    t = jmktime(&jtm);
    localtime_r(&t, &tm);
    printf("%d/%d/%d \n", tm.tm_year, tm.tm_mon, tm.tm_mday);
    exit(EXIT_SUCCESS);
}

⭐ RETURN VALUE ⭐

Every one of these functions returns the value described above, or NULL β€” and that's -1 in the case of jmktime() β€” if an error was detected. So check your returns, bestie, fr fr. 🫢


okay that's part 1, don't go anywhere β€” more of this page is coming πŸ‘€

✨ CONFORMING TO ✨

okay bestie so C99 Standards. These functions are provided with APIs similar to that of POSIX.1-2001 date and time manipulation and are NOT part of POSIX standard. For thread safety jasctime(), jctime(), gmtime(), localtime(), and mktime() set of functions should nout be used. See reentrant versions.

like POSIX.1-2008, the following functions: jasctime(), jasctime_r(), jctime(), and jctime_r() should be considered obsolete. Use jstrftime(3) instead.


🌸 NOTES 🌸

The four functions jasctime(), jctime(), jgmtime() and jlocaltime() return a pointer to static data and hence are not thread-safe. Thread-safe versions are jasctime_r(), jctime_r(), jgmtime_r() and jlocaltime_r()

libjalali version of struct jtm has additional fields

long tm_gmtoff;           /* Seconds east of UTC */
const char *tm_zone;      /* Timezone abbreviation */

πŸ‘‘ SEE ALSO πŸ‘‘


πŸŽ€ COLOPHON πŸŽ€

This page is part of release 0.2 of the libjalali man-pages


🫢 AUTHOR 🫢

Written by Ashkan Ghassemi. <ghassemi@ftml.net>


😭 REPORTING BUGS 😭

Report libjalali bugs to <ghassemi@ftml.net>

libjalali home page: <http://savannah.nongnu.org/projects/jcal/>


Copyright (C) 2011 Ashkan Ghassemi.

License LGPLv3+: GNU LGPL version 3 or later <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.


and that’s the whole vibe check, bestie πŸ’… this page comes from release 0.2 of the libjalali man-pages, dated 2011-05-28, and it’s JCTIME(3) β€” go forth and maybe use the reentrant versions, fr fr πŸ¦‹βœ¨

πŸ“¦ where this page comes from