Пример #1
0
def localtime(t=None):
    if t is None:
        t = time()
    date = JS("new Date(@{{t}}*1000)")
    dateOffset = date.getTimezoneOffset()
    tm = struct_time()
    tm_year = tm.tm_year = int(date.getFullYear())
    tm_mon = tm.tm_mon = int(date.getMonth()) + 1
    tm_mday = tm.tm_mday = int(date.getDate())
    tm.tm_hour = int(date.getHours())
    tm.tm_min = int(date.getMinutes())
    tm.tm_sec = int(date.getSeconds())
    tm.tm_wday = (int(date.getDay()) + 6) % 7
    tm.tm_isdst = 0 if timezone == 60*date.getTimezoneOffset() else 1
    startOfYear = JS("new Date(@{{tm_year}},0,1)") # local time
    startOfYearOffset = startOfYear.getTimezoneOffset()
    startOfDay = JS("new Date(@{{tm_year}},@{{tm_mon}}-1,@{{tm_mday}})")
    dt = float(startOfDay.getTime() - startOfYear.getTime())/1000
    dt = dt + 60 * (startOfYearOffset - dateOffset)
    tm.tm_yday = 1 + int(dt/86400.0)
    return tm
Пример #2
0
def localtime(t=None):
    if t is None:
        t = time()
    date = JS("new Date(@{{t}}*1000)")
    dateOffset = date.getTimezoneOffset()
    tm = struct_time()
    tm_year = tm.tm_year = int(date.getFullYear())
    tm_mon = tm.tm_mon = int(date.getMonth()) + 1
    tm_mday = tm.tm_mday = int(date.getDate())
    tm.tm_hour = int(date.getHours())
    tm.tm_min = int(date.getMinutes())
    tm.tm_sec = int(date.getSeconds())
    tm.tm_wday = (int(date.getDay()) + 6) % 7
    tm.tm_isdst = 0 if timezone == 60 * date.getTimezoneOffset() else 1
    startOfYear = JS("new Date(@{{tm_year}},0,1)")  # local time
    startOfYearOffset = startOfYear.getTimezoneOffset()
    startOfDay = JS("new Date(@{{tm_year}},@{{tm_mon}}-1,@{{tm_mday}})")
    dt = float(startOfDay.getTime() - startOfYear.getTime()) / 1000
    dt = dt + 60 * (startOfYearOffset - dateOffset)
    tm.tm_yday = 1 + int(dt / 86400.0)
    return tm
Пример #3
0
def strftime(fmt, t=None):
    if t is None:
        t = localtime()
    else:
        if not isinstance(t, struct_time) and len(t) != 9:
            raise TypeError('argument must be 9-item sequence, not float')
    tm_year = t.tm_year
    tm_mon = t.tm_mon
    tm_mday = t.tm_mday
    tm_hour = t.tm_hour
    tm_min = t.tm_min
    tm_sec = t.tm_sec
    tm_wday = t.tm_wday
    tm_yday = t.tm_yday
    date = JS("new Date(@{{tm_year}}, @{{tm_mon}} - 1, @{{tm_mday}}, @{{tm_hour}}, @{{tm_min}}, @{{tm_sec}})")
    startOfYear = JS("new Date(@{{tm_year}},0,1)")
    firstMonday = 1 - ((startOfYear.getDay() + 6) % 7) + 7
    firstWeek = JS("new Date(@{{tm_year}},0,@{{firstMonday}})")
    weekNo = date.getTime() - firstWeek.getTime()
    if weekNo < 0:
        weekNo = 0
    else:
        weekNo = 1 + int(weekNo/604800000)

    def format(c):
        if c == '%':
            return '%'
        elif c == 'a':
            return format('A')[:3]
        elif c == 'A':
            return __c__days[format('w')]
        elif c == 'b':
            return format('B')[:3]
        elif c == 'B':
            return __c__months[tm_mon-1]
        elif c == 'c':
            return date.toLocaleString()
        elif c == 'd':
            return "%02d" % tm_mday
        elif c == 'H':
            return "%02d" % tm_hour
        elif c == 'I':
            return "%02d" % (tm_hour % 12)
        elif c == 'j':
            return "%03d" % tm_yday
        elif c == 'm':
            return "%02d" % tm_mon
        elif c == 'M':
            return "%02d" % tm_min
        elif c == 'p': # FIXME: should be locale dependent
            if tm_hour < 12:
                return "AM"
            return "PM"
        elif c == 'S':
            return "%02d" % tm_sec
        elif c == 'U':
            raise NotImplementedError("strftime format character '%s'" % c)
        elif c == 'w':
            return "%d" % ((tm_wday+1) % 7)
        elif c == 'W':
            return "%d" % weekNo
        elif c == 'x':
            return "%s" % date.toLocaleDateString()
        elif c == 'X':
            return "%s" % date.toLocaleTimeString()
        elif c == 'y':
            return "%02d" % (tm_year % 100)
        elif c == 'Y':
            return "%04d" % tm_year
        elif c == 'Z':
            raise NotImplementedError("strftime format character '%s'" % c)
        return "%" + c
    result = ''
    remainder = fmt
    re_pct = JS("/([^%]*)%(.)(.*)/")
    JS("var a, fmtChar;")
    while remainder:
        JS("""{
        @{{!a}} = @{{re_pct}}.exec(@{{remainder}});
        if (!@{{!a}}) {
            @{{result}} += @{{remainder}};
            @{{remainder}} = false;
        } else {
            @{{result}} += @{{!a}}[1];
            @{{!fmtChar}} = @{{!a}}[2];
            @{{remainder}} = @{{!a}}[3];
            if (typeof @{{!fmtChar}} != 'undefined') {
                @{{result}} += @{{format}}(@{{!fmtChar}});
            }
        }
       }""")
    return str(result)
Пример #4
0
def strftime(fmt, t=None):
    if t is None:
        t = localtime()
    else:
        if not isinstance(t, struct_time) and len(t) != 9:
            raise TypeError('argument must be 9-item sequence, not float')
    tm_year = t.tm_year
    tm_mon = t.tm_mon
    tm_mday = t.tm_mday
    tm_hour = t.tm_hour
    tm_min = t.tm_min
    tm_sec = t.tm_sec
    tm_wday = t.tm_wday
    tm_yday = t.tm_yday
    date = JS(
        "new Date(@{{tm_year}}, @{{tm_mon}} - 1, @{{tm_mday}}, @{{tm_hour}}, @{{tm_min}}, @{{tm_sec}})"
    )
    startOfYear = JS("new Date(@{{tm_year}},0,1)")
    firstMonday = 1 - ((startOfYear.getDay() + 6) % 7) + 7
    firstWeek = JS("new Date(@{{tm_year}},0,@{{firstMonday}})")
    weekNo = date.getTime() - firstWeek.getTime()
    if weekNo < 0:
        weekNo = 0
    else:
        weekNo = 1 + int(weekNo / 604800000)

    def format(c):
        if c == '%':
            return '%'
        elif c == 'a':
            return format('A')[:3]
        elif c == 'A':
            return __c__days[format('w')]
        elif c == 'b':
            return format('B')[:3]
        elif c == 'B':
            return __c__months[tm_mon - 1]
        elif c == 'c':
            return date.toLocaleString()
        elif c == 'd':
            return "%02d" % tm_mday
        elif c == 'H':
            return "%02d" % tm_hour
        elif c == 'I':
            return "%02d" % (tm_hour % 12)
        elif c == 'j':
            return "%03d" % tm_yday
        elif c == 'm':
            return "%02d" % tm_mon
        elif c == 'M':
            return "%02d" % tm_min
        elif c == 'p':  # FIXME: should be locale dependent
            if tm_hour < 12:
                return "AM"
            return "PM"
        elif c == 'S':
            return "%02d" % tm_sec
        elif c == 'U':
            raise NotImplementedError("strftime format character '%s'" % c)
        elif c == 'w':
            return "%d" % ((tm_wday + 1) % 7)
        elif c == 'W':
            return "%d" % weekNo
        elif c == 'x':
            return "%s" % date.toLocaleDateString()
        elif c == 'X':
            return "%s" % date.toLocaleTimeString()
        elif c == 'y':
            return "%02d" % (tm_year % 100)
        elif c == 'Y':
            return "%04d" % tm_year
        elif c == 'Z':
            raise NotImplementedError("strftime format character '%s'" % c)
        return "%" + c

    result = ''
    remainder = fmt
    re_pct = JS("/([^%]*)%(.)(.*)/")
    JS("var a, fmtChar;")
    while remainder:
        JS("""{
        @{{!a}} = @{{re_pct}}.exec(@{{remainder}});
        if (!@{{!a}}) {
            @{{result}} += @{{remainder}};
            @{{remainder}} = false;
        } else {
            @{{result}} += @{{!a}}[1];
            @{{!fmtChar}} = @{{!a}}[2];
            @{{remainder}} = @{{!a}}[3];
            if (typeof @{{!fmtChar}} != 'undefined') {
                @{{result}} += @{{format}}(@{{!fmtChar}});
            }
        }
       }""")
    return str(result)