Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a54d053e authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 5350 into donut

* changes:
  move ui/Time.cpp to core/jni, since this is the only place it is used
parents 457cecdc a8664df8
Loading
Loading
Loading
Loading

include/utils/TimeUtils.h

deleted100644 → 0
+0 −89
Original line number Diff line number Diff line
/*
 * Copyright (C) 2005 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_TIME_H
#define ANDROID_TIME_H

#include <time.h>
#include <cutils/tztime.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/time.h>
#include <utils/String8.h>
#include <utils/String16.h>

namespace android {

/*
 * This class is the core implementation of the android.util.Time java
 * class.  It doesn't implement some of the methods that are implemented
 * in Java.  They could be done here, but it's not expected that this class
 * will be used.  If that assumption is incorrect, feel free to update this
 * file.  The reason to do it here is to not mix the implementation of this
 * class and the jni glue code.
 */
class Time
{
public:
    struct tm t;

    // this object doesn't own this string
    const char *timezone;

    enum {
        SEC = 1,
        MIN = 2,
        HOUR = 3,
        MDAY = 4,
        MON = 5,
        YEAR = 6,
        WDAY = 7,
        YDAY = 8
    };

    static int compare(Time& a, Time& b);

    Time();

    void switchTimezone(const char *timezone);
    String8 format(const char *format, const struct strftime_locale *locale) const;
    void format2445(short* buf, bool hasTime) const;
    String8 toString() const;
    void setToNow();
    int64_t toMillis(bool ignoreDst);
    void set(int64_t millis);

    inline void set(int sec, int min, int hour, int mday, int mon, int year,
            int isdst)
    {
        this->t.tm_sec = sec;
        this->t.tm_min = min;
        this->t.tm_hour = hour;
        this->t.tm_mday = mday;
        this->t.tm_mon = mon;
        this->t.tm_year = year;
        this->t.tm_isdst = isdst;
#ifdef HAVE_TM_GMTOFF
        this->t.tm_gmtoff = 0;
#endif
        this->t.tm_wday = 0;
        this->t.tm_yday = 0;
    }
};

}; // namespace android

#endif // ANDROID_TIME_H
+1 −2
Original line number Diff line number Diff line
@@ -24,8 +24,7 @@ LOCAL_SRC_FILES:= \
	Region.cpp \
	Surface.cpp \
	SurfaceComposerClient.cpp \
	SurfaceFlingerSynchro.cpp \
	Time.cpp
	SurfaceFlingerSynchro.cpp 

LOCAL_SHARED_LIBRARIES := \
	libcorecg \

libs/ui/Time.cpp

deleted100644 → 0
+0 −199
Original line number Diff line number Diff line
#include <utils/TimeUtils.h>
#include <stdio.h>
#include <cutils/tztime.h>

namespace android {

static void
dump(const Time& t)
{
    #ifdef HAVE_TM_GMTOFF
        long tm_gmtoff = t.t.tm_gmtoff;
    #else
        long tm_gmtoff = 0;
    #endif
    printf("%04d-%02d-%02d %02d:%02d:%02d (%d,%ld,%d,%d)\n",
            t.t.tm_year+1900, t.t.tm_mon+1, t.t.tm_mday,
            t.t.tm_hour, t.t.tm_min, t.t.tm_sec,
            t.t.tm_isdst, tm_gmtoff, t.t.tm_wday, t.t.tm_yday);
}

Time::Time()
{
    t.tm_sec = 0;
    t.tm_min = 0;
    t.tm_hour = 0;
    t.tm_mday = 0;
    t.tm_mon = 0;
    t.tm_year = 0;
    t.tm_wday = 0;
    t.tm_yday = 0;
    t.tm_isdst = -1; // we don't know, so let the C library determine
    #ifdef HAVE_TM_GMTOFF
        t.tm_gmtoff = 0;
    #endif
}


#define COMPARE_FIELD(field) do { \
        int diff = a.t.field - b.t.field; \
        if (diff != 0) return diff; \
    } while(0)

int
Time::compare(Time& a, Time& b)
{
    if (0 == strcmp(a.timezone, b.timezone)) {
        // if the timezones are the same, we can easily compare the two
        // times.  Otherwise, convert to milliseconds and compare that.
        // This requires that object be normalized.
        COMPARE_FIELD(tm_year);
        COMPARE_FIELD(tm_mon);
        COMPARE_FIELD(tm_mday);
        COMPARE_FIELD(tm_hour);
        COMPARE_FIELD(tm_min);
        COMPARE_FIELD(tm_sec);
        return 0;
    } else {
        int64_t am = a.toMillis(false /* use isDst */);
        int64_t bm = b.toMillis(false /* use isDst */);
        int64_t diff = am-bm;
        return (diff < 0) ? -1 : ((diff > 0) ? 1 : 0);
    }
}

static const int DAYS_PER_MONTH[] = {
                        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
                    };

static inline int days_this_month(int year, int month)
{
    int n = DAYS_PER_MONTH[month];
    if (n != 28) {
        return n;
    } else {
        int y = year;
        return ((y%4)==0&&((y%100)!=0||(y%400)==0)) ? 29 : 28;
    }
}

void 
Time::switchTimezone(const char* timezone)
{
    time_t seconds = mktime_tz(&(this->t), this->timezone);
    localtime_tz(&seconds, &(this->t), timezone);
}

String8 
Time::format(const char *format, const struct strftime_locale *locale) const
{
    char buf[257];
    int n = strftime_tz(buf, 257, format, &(this->t), locale);
    if (n > 0) {
        return String8(buf);
    } else {
        return String8();
    }
}

static inline short
tochar(int n)
{
    return (n >= 0 && n <= 9) ? ('0'+n) : ' ';
}

static inline short
next_char(int *m, int k)
{
    int n = *m / k;
    *m = *m % k;
    return tochar(n);
}

void
Time::format2445(short* buf, bool hasTime) const
{
    int n;

    n = t.tm_year+1900;
    buf[0] = next_char(&n, 1000);
    buf[1] = next_char(&n, 100);
    buf[2] = next_char(&n, 10);
    buf[3] = tochar(n);

    n = t.tm_mon+1;
    buf[4] = next_char(&n, 10);
    buf[5] = tochar(n);

    n = t.tm_mday;
    buf[6] = next_char(&n, 10);
    buf[7] = tochar(n);

    if (hasTime) {
      buf[8] = 'T';

      n = t.tm_hour;
      buf[9] = next_char(&n, 10);
      buf[10] = tochar(n);
      
      n = t.tm_min;
      buf[11] = next_char(&n, 10);
      buf[12] = tochar(n);
      
      n = t.tm_sec;
      buf[13] = next_char(&n, 10);
      buf[14] = tochar(n);
      bool inUtc = strcmp("UTC", timezone) == 0;
      if (inUtc) {
          buf[15] = 'Z';
      }
    }
}

String8 
Time::toString() const
{
    String8 str;
    char* s = str.lockBuffer(150);
    #ifdef HAVE_TM_GMTOFF
        long tm_gmtoff = t.tm_gmtoff;
    #else
        long tm_gmtoff = 0;
    #endif
    sprintf(s, "%04d%02d%02dT%02d%02d%02d%s(%d,%d,%ld,%d,%d)", 
            t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min,
            t.tm_sec, timezone, t.tm_wday, t.tm_yday, tm_gmtoff, t.tm_isdst,
            (int)(((Time*)this)->toMillis(false /* use isDst */)/1000));
    str.unlockBuffer();
    return str;
}

void 
Time::setToNow()
{
    time_t seconds;
    time(&seconds);
    localtime_tz(&seconds, &(this->t), this->timezone);
}

int64_t 
Time::toMillis(bool ignoreDst)
{
    if (ignoreDst) {
        this->t.tm_isdst = -1;
    }
    int64_t r = mktime_tz(&(this->t), this->timezone);
    if (r == -1)
        return -1;
    return r * 1000;
}

void 
Time::set(int64_t millis)
{
    time_t seconds = millis / 1000;
    localtime_tz(&seconds, &(this->t), this->timezone);
}

}; // namespace android