gonzui


Format: Advanced Search

t2ex/t2ex_source/t2ex/datetime/src/localtime.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: /*
   15:  * This software package is available for use, modification, 
   16:  * and redistribution in accordance with the terms of the attached 
   17:  * T-License 2.x.
   18:  * If you want to redistribute the source code, you need to attach 
   19:  * the T-License 2.x document.
   20:  * There's no obligation to publish the content, and no obligation 
   21:  * to disclose it to the TRON Forum if you have modified the 
   22:  * software package.
   23:  * You can also distribute the modified source code. In this case, 
   24:  * please register the modification to T-Kernel traceability service.
   25:  * People can know the history of modifications by the service, 
   26:  * and can be sure that the version you have inherited some 
   27:  * modification of a particular version or not.
   28:  *
   29:  *    http://trace.tron.org/tk/?lang=en
   30:  *    http://trace.tron.org/tk/?lang=ja
   31:  *
   32:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   33:  * the portion of the software that is copyrighted by Ken Sakamura or 
   34:  * the TRON Forum does not infringe the copyrights of a third party.
   35:  * However, it does not make any warranty other than this.
   36:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   37:  * responsible for any consequences or damages caused directly or
   38:  * indirectly by the use of this software package.
   39:  *
   40:  * The source codes in bsd_source.tar.gz in this software package are 
   41:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   42:  * They need to be changed or redistributed according to the 
   43:  * representation of each source header.
   44:  */
   45: 
   46: /*
   47:  *      @(#)localtime.c
   48:  *
   49:  *       T2EX: calendar functions
   50:  *       dt_localtime, dt_localtime_ms, dt_localtime_us
   51:  */
   52: 
   53: #include <basic.h>
   54: #include <errno.h>
   55: #include <tk/tkernel.h>
   56: #include <t2ex/datetime.h>
   57: #include "internal.h"
   58: 
   59: LOCAL BOOL chkDST( const struct tm* tm, union dsttimespec tspec, BOOL start )
   60: {
   61:         int i;
   62:         long v, w;
   63: 
   64:         switch (tspec.j.type) {
   65:         case DSTTIMESPEC_JULIAN_NL:
   66:                 if (isleap(tm->tm_year + 1900)
   67:                     && tm->tm_yday > (_dt_mdays[0] + _dt_mdays[1])) {
   68:                         v = (tm->tm_yday - 1) * 24 * 60 * 60 + tm->tm_hour * 60 * 60 
   69:                                 + tm->tm_min * 60 + tm->tm_sec;
   70:                         return start ? (v >= tspec.j.offset) : (v <= tspec.j.offset);
   71:                 }
   72:                 /* FALLTHROUGH */
   73: 
   74:         case DSTTIMESPEC_JULIAN:
   75:                 v = tm->tm_yday * 24 * 60 * 60 + tm->tm_hour * 60 * 60 
   76:                         + tm->tm_min * 60 + tm->tm_sec;
   77:                 return start ? (v >= tspec.j.offset) : (v <= tspec.j.offset);
   78: 
   79:         case DSTTIMESPEC_MWD:
   80:                 if (tm->tm_mon + 1 != tspec.m.m) {
   81:                         return start ? (tm->tm_mon + 1 > tspec.m.m) : (tm->tm_mon + 1 < tspec.m.m);
   82:                 }
   83: 
   84:                 w = (tm->tm_wday - (tm->tm_mday - 1) % 7 + 7) % 7; /* wday for the first day of the month */
   85:                 v = (tspec.m.d - w + 7) % 7; /* First day of the month (origin:0) which satisfies wday = tspec.m.d */
   86: 
   87:                 /* Calculate yday for tspec.m */
   88:                 w = (v + 7 * (tspec.m.n - 1)); /* Day of the month for tspec.m */
   89:                 for (i = 0; i < tm->tm_mon; i++) {
   90:                         w += _dt_mdays[i];
   91:                 }
   92: 
   93:                 w = w * 24 * 60 * 60 + tspec.m.offset;
   94:                 v = tm->tm_yday * 24 * 60 * 60 + tm->tm_hour * 60 * 60 
   95:                         + tm->tm_min * 60 + tm->tm_sec;
   96:                 return start ? (v >= w) : (v <= w);
   97: 
   98:         default:
   99:                 return FALSE;
  100:         }
  101: }
  102: 
  103: Inline BOOL isDST( struct tm* tm, const struct tzinfo* tz )
  104: {
  105:         if (tz->daylight) {
  106:                 return FALSE;
  107:         }
  108: 
  109:         return chkDST(tm, tz->dst_start, TRUE) && chkDST(tm, tz->dst_end, FALSE);
  110: }
  111: 
  112: EXPORT ER dt_localtime_us( SYSTIM_U tim_u, const struct tzinfo* tz, struct tm* result )
  113: {
  114:         struct tzinfo systz;
  115:         ER er;
  116: 
  117:         if (tz == NULL) {
  118:                 /* If tz is not specified, use system timezone */
  119:                 er = dt_getsystz(&systz);
  120:                 if (er < E_OK) {
  121:                         return er;
  122:                 }
  123:                 tz = &systz;
  124:         }
  125: 
  126:         /* First try with standard (non-DST) time */
  127:         er = dt_gmtime_us(tim_u - ((SYSTIM_U)tz->offset) * 1000000, result);
  128:         if (er < E_OK) {
  129:                 return er;
  130:         }
  131: 
  132:         /* Check if the local time is during the DST period */
  133:         if (!isDST(result, tz)) {
  134:                 result->tm_isdst = 0;
  135:                 return E_OK;
  136:         }
  137: 
  138:         /* Try again with DST */
  139:         er = dt_gmtime_us(tim_u - ((SYSTIM_U)tz->dst_offset) * 1000000, result);
  140:         if (er < E_OK) {
  141:                 return er;
  142:         }
  143: 
  144:         result->tm_isdst = 1;
  145:         return E_OK;
  146: }
  147: 
  148: EXPORT ER dt_localtime( time_t tims, const struct tzinfo* tz, struct tm* result )
  149: {
  150:         return dt_localtime_us(s_to_us(tims), tz, result);
  151: }
  152: 
  153: EXPORT ER dt_localtime_ms( const SYSTIM* tim, const struct tzinfo* tz, struct tm* result )
  154: {
  155:         return dt_localtime_us(ms_to_us(tim), tz, result);
  156: }