---
name: "jctime"
section: "3"
platform: "linux"
package: "jcal"
version: "0.5.1-0.2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/jctime"
---

# jctime(3)

transform jalali date and time to broken-down jalali time or ASCII

## NAME

jasctime, jctime, jgmtime, jlocaltime, jmktime, jasctime\_r, jctime\_r, jgmtime\_r, jlocaltime\_r - transform jalali date and time to broken-down jalali time or 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);
```

Link with -ljalali

## DESCRIPTION

The **jctime**(), **jgmtime**() and **jlocaltime**() functions all take an argument of data type *time\_t* which represents calendar time. When interpreted as an absolute time value, it represents the number of seconds elapsed since the Epoch, 1970-01-01 (1348-10-11 in Jalali) 00:00:00 +0000 (UTC).

The **jasctime**() and **jmktime**() functions both take an argument representing broken-down jalali time which is a representation separated into year, month, day, etc.

Broken-down jalali time is stored in the structure *jtm* which is defined in *\<jtime.h\>* as follows:

```
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 */
};
```

The members of the *jtm* structure are:

- *tm\_sec*

  The number of seconds after the minute, in the range 0 to 59.

- *tm\_min*

  The number of minutes after the hour, in the range 0 to 59.

- *tm\_hour*

  The number of hours past midnight, in the range 0 to 23.

- *tm\_mday*

  The day of the month, in the range 1 to 31.

- *tm\_mon*

  The number of months since Farvadin, in the range 0 to 11.

- *tm\_year*

  Absolute year number including the century.

- *tm\_wday*

  The number of days since Saturday, in the range 0 to 6.

- *tm\_yday*

  The number of days since Farvadin 1, in the range 0 to 365.

- *tm\_isdst*

  A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time is in effect, zero otherwise.

Information about a certain year in jalali system is stored in the structure *jyinfo* in the following format:

```
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 */
};
```

The members of the *jyinfo* structure are:

- *lf*

  A flag that indicates whether a year is leap or not.

- *y*

  Absolute year number including the century.

- *r*

  The number of remaining years in the grand cycle. (2820 years in length)

- *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.

Internal jalali date functions make use of passed days since UTC Epoch to calculate date and time. To store these information, the following structure is also defined:

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

The members of the *ab\_jtm* structure are as follows:

- *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)

The call **jctime(***t***)** is equivalent to **jasctime(jlocaltime(***t***))***.* It converts the calendar time *t* into a null-terminated string of the form

"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". 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 which might be overwritten by subsequent calls to any of the date and time functions. The reentrant version **jctime\_r**() does the same, but stores the string in a user-supplied buffer which should have room for at least 26 bytes.

The **jgmtime**() function converts the calendar time *timep* to broken-down jalali time representation, expressed in Coordinated Universal Time (UTC). 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. The **jgmtime\_r**() function does the same, but stores the data in a user-supplied struct. The function acts as if it called [**tzset**(3)](/tzset/3.md)

The **jlocaltime**() function converts the calendar time *timep* to broken-down jalali time representation, expressed relative to the user's specified timezone. The function acts as if it called [**tzset**(3)](/tzset/3.md) and sets the external variables *tzname* with information 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. The **jlocaltime\_r**() function does the same, but stores the data in a user-supplied struct.

The **jasctime**() function 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. The **jasctime\_r**() function does the same, but stores the string in a user-supplied buffer which should have room for at least 26 bytes.

The **jmktime**() function converts a broken-down jalali time structure, expressed as local time, to calendar time representation. The function ignores the values supplied by the caller in the *tm\_wday* field. The value specified in the *tm\_isdst* field 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;

The **jmktime**() function modifies the fields of the *jtm* structure as follows: *tm\_wday* and *tm\_yday* are set to values determined from the contents of the other fields; if structure members are outside their valid interval, they will be normalized (so that, for example, 40 Bahman is changed into 10 Esfand); Calling **jmktime**() also sets the external variable *tzname* with information about the current timezone.

There are a number of non-standard functions also provided to work with jalali date and time.

The **jalali\_is\_jleap**() function 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.

The **jalali\_create\_time\_from\_secs**() function fills out the *ab\_jtm* structure members based on the absolute number of seconds elapsed since UTC Epoch.

The **jalali\_create\_secs\_from\_time**() function is the converse function to **jalali\_create\_time\_from\_secs**() which returns absolute number of seconds elapsed since UTC Epoch based on the supplied *ab\_jtm* structure.

The **jalali\_create\_date\_from\_days**() function alters *tm\_mon* and *tm\_mday* fields of the broken-down jalali time strucutre based on it's *tm\_yday* field. It returns -1 on the event of encountering any errors and structure fields remain untouched.

The **jalali\_create\_days\_from\_date**() function alters *tm\_yday* field of the broken-down jalali time structure based on it's *tm\_mon* and *tm\_mday* fields. It returns -1 on the event of encountering any errors and structure fields remain untouched.

The **jalali\_get\_jyear\_info**() function modifies *jyinfo* structure fields to match information for year specified by it's *y* field. Information regarding a year in jalali system includes leap flag, passed and reamining years in the grand cycle, passed and remaining leap years in the grand cycle and absolute number of passed leaps since UTC Epoch.

The **jalali\_get\_date**() function calculates the jalali date based on number of days since UTC epoch. It alters the broken-down jalali time structure fields accordingly.

The **jalali\_get\_diff**() function is the converse function of **jalali\_get\_date**() and calculates the number of days passed since UTC Epoch based on a broken-down jalali time structure supplied to it.

The **jalali\_update**() function updates *tm\_wday* and *tm\_yday* fields of the broken-down jalali time structure based on it's *tm\_year*, *tm\_mon*, *tm\_mday*, *tm\_hour*, *tm\_min* and *tm\_sec* fields. If structure members are outside their valid interval, they will be normalized (so that, for example, 40 Bahman is changed into 10 Esfand). *tm\_isdst*, *tm\_gmtoff* and *tm\_zone* fields are set accordingly.

## EXAMPLES

The following program converts a jalali date to gregorian

```
#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

Each of these functions returns the value described, or NULL (-1 in case of **jmktime**()) in case an error was detected.

## CONFORMING TO

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)](/jstrftime/3.md) 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

[**jdate**(1)](/jdate/1.md), [**jcal**(1)](/jcal/1.md), [**gettimeofday**(2)](/gettimeofday/2.md), [**time**(2)](/time/2.md), [**utime**(2)](/utime/2.md), [**clock**(3)](/clock/3.md), [**difftime**(3)](/difftime/3.md), [**jstrftime**(3)](/jstrftime/3.md), [**jstrptime**(3)](/jstrptime/3.md), [**timegm**(3)](/timegm/3.md), [**tzset**(3)](/tzset/3.md), [**time**(7)](/time/7.md)

## 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

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.
