LOCALTIME(3)               POSIX Programmer's Manual              LOCALTIME(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       localtime, localtime_r -- convert a time value to a broken-down local
       time

SSYYNNOOPPSSIISS
       #include <time.h>

       struct tm *localtime(const time_t *_t_i_m_e_r);
       struct tm *localtime_r(const time_t *restrict _t_i_m_e_r,
           struct tm *restrict _r_e_s_u_l_t);

DDEESSCCRRIIPPTTIIOONN
       For _l_o_c_a_l_t_i_m_e(): The functionality described on this reference page is
       aligned with the ISO C standard. Any conflict between the requirements
       described here and the ISO C standard is unintentional. This volume of
       POSIX.1-2008 defers to the ISO C standard.

       The _l_o_c_a_l_t_i_m_e() function shall convert the time in seconds since the
       Epoch pointed to by _t_i_m_e_r into a broken-down time, expressed as a local
       time. The function corrects for the timezone and any seasonal time
       adjustments.  Local timezone information is used as though _l_o_c_a_l_t_i_m_e()
       calls _t_z_s_e_t().

       The relationship between a time in seconds since the Epoch used as an
       argument to _l_o_c_a_l_t_i_m_e() and the ttmm structure (defined in the _<_t_i_m_e_._h_>
       header) is that the result shall be as specified in the expression
       given in the definition of seconds since the Epoch (see the Base
       Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _4_._1_5, _S_e_c_o_n_d_s _S_i_n_c_e _t_h_e
       _E_p_o_c_h) corrected for timezone and any seasonal time adjustments, where
       the names in the structure and in the expression correspond.

       The same relationship shall apply for _l_o_c_a_l_t_i_m_e___r().

       The _l_o_c_a_l_t_i_m_e() function need not be thread-safe.

       The _a_s_c_t_i_m_e(), _c_t_i_m_e(), _g_m_t_i_m_e(), and _l_o_c_a_l_t_i_m_e() functions shall
       return values in one of two static objects: a broken-down time
       structure and an array of type cchhaarr.  Execution of any of the functions
       may overwrite the information returned in either of these objects by
       any of the other functions.

       The _l_o_c_a_l_t_i_m_e___r() function shall convert the time in seconds since the
       Epoch pointed to by _t_i_m_e_r into a broken-down time stored in the
       structure to which _r_e_s_u_l_t points. The _l_o_c_a_l_t_i_m_e___r() function shall also
       return a pointer to that same structure.

       Unlike _l_o_c_a_l_t_i_m_e(), the _l_o_c_a_l_t_i_m_e___r() function is not required to set
       _t_z_n_a_m_e.  If _l_o_c_a_l_t_i_m_e___r() does not set _t_z_n_a_m_e, it shall not set
       _d_a_y_l_i_g_h_t and shall not set _t_i_m_e_z_o_n_e.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _l_o_c_a_l_t_i_m_e() function shall return a
       pointer to the broken-down time structure.  If an error is detected,
       _l_o_c_a_l_t_i_m_e() shall return a null pointer and set _e_r_r_n_o to indicate the
       error.

       Upon successful completion, _l_o_c_a_l_t_i_m_e___r() shall return a pointer to the
       structure pointed to by the argument _r_e_s_u_l_t.  If an error is detected,
       _l_o_c_a_l_t_i_m_e___r() shall return a null pointer and set _e_r_r_n_o to indicate the
       error.

EERRRROORRSS
       The _l_o_c_a_l_t_i_m_e() and _l_o_c_a_l_t_i_m_e___r() functions shall fail if:

       EEOOVVEERRFFLLOOWW
              The result cannot be represented.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   GGeettttiinngg tthhee LLooccaall DDaattee aanndd TTiimmee
       The following example uses the _t_i_m_e() function to calculate the time
       elapsed, in seconds, since January 1, 1970 0:00 UTC (the Epoch),
       _l_o_c_a_l_t_i_m_e() to convert that value to a broken-down time, and _a_s_c_t_i_m_e()
       to convert the broken-down time values into a printable string.

           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ttiimmee..hh>>

           int main(void)
           {
               time_t result;

               result = time(NULL);
               printf("%s%ju secs since the Epoch\n",
                   asctime(localtime(&result)),
                       (uintmax_t)result);
               return(0);
           }

       This example writes the current time to _s_t_d_o_u_t in a form like this:

           WWeedd JJuunn 2266 1100::3322::1155 11999966
           883355881100333355 sseeccss ssiinnccee tthhee EEppoocchh

   GGeettttiinngg tthhee MMooddiiffiiccaattiioonn TTiimmee ffoorr aa FFiillee
       The following example prints the last data modification timestamp in
       the local timezone for a given file.

           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ttiimmee..hh>>
           ##iinncclluuddee <<ssyyss//ssttaatt..hh>>

           int
           print_file_time(const char *pathname)
           {
               struct stat statbuf;
               struct tm *tm;
               char timestr[BUFSIZ];

               if(stat(pathname, &statbuf) == -1)
                   return -1;
               if((tm = localtime(&statbuf.st_mtime)) == NULL)
                   return -1;
               if(strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm) == 0)
                   return -1;
               printf("%s: %s.%09ld\n", pathname, timestr, statbuf.st_mtim.tv_nsec);
               return 0;
           }

   TTiimmiinngg aann EEvveenntt
       The following example gets the current time, converts it to a string
       using _l_o_c_a_l_t_i_m_e() and _a_s_c_t_i_m_e(), and prints it to standard output using
       _f_p_u_t_s().  It then prints the number of minutes to an event being timed.

           ##iinncclluuddee <<ttiimmee..hh>>
           ##iinncclluuddee <<ssttddiioo..hh>>
           ......
           ttiimmee__tt nnooww;;
           iinntt mmiinnuutteess__ttoo__eevveenntt;;
           ......
           ttiimmee((&&nnooww));;
           pprriinnttff((""TThhee ttiimmee iiss ""));;
           ffppuuttss((aassccttiimmee((llooccaallttiimmee((&&nnooww)))),, ssttddoouutt));;
           pprriinnttff((""TThheerree aarree ssttiillll %%dd mmiinnuutteess ttoo tthhee eevveenntt..\\nn"",,
               mmiinnuutteess__ttoo__eevveenntt));;
           ......

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The _l_o_c_a_l_t_i_m_e___r() function is thread-safe and returns values in a user-
       supplied buffer instead of possibly using a static data area that may
       be overwritten by each call.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _a_s_c_t_i_m_e(), _c_l_o_c_k(), _c_t_i_m_e(), _d_i_f_f_t_i_m_e(), _g_e_t_d_a_t_e(), _g_m_t_i_m_e(), _m_k_t_i_m_e(),
       _s_t_r_f_t_i_m_e(), _s_t_r_p_t_i_m_e(), _t_i_m_e(), _t_z_s_e_t(), _u_t_i_m_e()

       The Base Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _4_._1_5, _S_e_c_o_n_d_s
       _S_i_n_c_e _t_h_e _E_p_o_c_h, <<ttiimmee..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                         LOCALTIME(3)
