📄 datemodule.java
字号:
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus.lib.date;import com.caucho.quercus.UnimplementedException;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.NotNull;import com.caucho.quercus.annotation.ReturnNullAsFalse;import com.caucho.quercus.env.*;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.util.Alarm;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import com.caucho.util.QDate;import com.caucho.vfs.Path;import java.util.TimeZone;import java.util.logging.Level;import java.util.logging.Logger;/** * Date functions. */public class DateModule extends AbstractQuercusModule { private static final L10N L = new L10N(DateModule.class); private static final Logger log = Logger.getLogger(DateModule.class.getName()); public static final int CAL_GREGORIAN = 0; public static final int CAL_JULIAN = 1; private static final String []_shortDayOfWeek = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; private static final String []_fullDayOfWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; private static final String []_shortMonth = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; private static final String []_fullMonth = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", }; private static final long MINUTE = 60000L; private static final long HOUR = 60 * MINUTE; private static final long DAY = 24 * HOUR; private final QDate _localCalendar = QDate.createLocal(); private final QDate _gmtCalendar = new QDate(); /** * Returns the days in a given month. */ public static int cal_days_in_month(int cal, int month, int year) { QDate date = new QDate(); date.setYear(year); date.setMonth(month - 1); return date.getDaysInMonth(); } /** * Returns the days in a given month. */ public static boolean checkdate(int month, int day, int year) { if (! (1 <= year && year <= 32767)) return false; if (! (1 <= month && month <= 12)) return false; return 1 <= day && day <= cal_days_in_month(0, month, year); } /** * Returns the formatted date. */ public String date(String format, @Optional("time()") long time) { return date(format, time, false); } /** * Returns the formatted date as an int. */ public Value idate(Env env, String format, @Optional("time()") long time) { if (format.length() != 1) { log.log(Level.FINE, L.l("idate format '{0}' needs to be of length one and only one", format)); env.warning(L.l("idate format '{0}' needs to be of length one and only one", format)); return BooleanValue.FALSE; } switch (format.charAt(0)) { case 'B': case 'd': case 'h': case 'H': case 'i': case 'I': case 'L': case 'm': case 's': case 't': case 'U': case 'w': case 'W': case 'y': case 'Y': case 'z': case 'Z': String dateString = date(format, time, false); int sign = 1; long result = 0; int length = dateString.length(); for (int i = 0; i < length; i++) { char ch = dateString.charAt(i); if ('0' <= ch && ch <= '9') result = result * 10 + ch - '0'; else if (ch == '-' && i == 0) sign = -1; else { log.log(Level.FINEST, L.l("error parsing idate string '{0}'", dateString)); break; } } return LongValue.create(result * sign); default: log.log(Level.FINE, L.l("'{0}' is not a valid idate format", format)); env.warning(L.l("'{0}' is not a valid idate format", format)); return BooleanValue.FALSE; } } /** * Returns the timestamp of easter. */ public static long easter_date(@Optional("-1") int year) { QDate date = new QDate(); if (year < 0) { date.setGMTTime(Alarm.getCurrentTime()); year = date.getYear(); } int y = year; int c = y / 100; int n = y - 19 * (y / 19); int k = (c - 17) / 25; int i = c - c /4 - (c - k) / 3 + 19 * n + 15; i = i - 30 * (i / 30); i = i - (i / 28) * (1 - ((i / 28) * (29 / (i + 1)) * ((21 - n) / 11))); int j = y + y / 4 + i + 2 - c + c / 4; j = j - 7 * (j / 7); int l = i - j; int m = 3 + (l + 40) / 44; int d = l + 28 - 31 * (m / 4); date.setYear(year); date.setMonth(m - 1); date.setDayOfMonth(d); return date.getGMTTime() / 1000; } /** * Returns the timestamp of easter. */ public static long easter_days(@Optional("-1") int year, @Optional int method) { return easter_date(year); } /** * Returns an array of the current date. */ public Value getdate(@Optional("time()") long time) { QDate date = new QDate(false); date.setLocalTime(1000 * time); ArrayValue array = new ArrayValueImpl(); array.put("seconds", date.getSecond()); array.put("minutes", date.getMinute()); array.put("hours", date.getHour()); array.put("mday", date.getDayOfMonth()); array.put("wday", date.getDayOfWeek() - 1); array.put("mon", date.getMonth() + 1); array.put("year", date.getYear()); array.put("yday", date.getDayOfYear()); array.put("weekday", _fullDayOfWeek[date.getDayOfWeek() - 1]); array.put("month", _fullMonth[date.getMonth()]); array.put(new LongValue(0), new LongValue(time)); return array; } public Value gettimeofday(Env env, @Optional boolean isFloatReturn) { long gmtTime = Alarm.getExactTime(); if (isFloatReturn) { return new DoubleValue(((double) gmtTime) / 1000.0); } else { ArrayValueImpl result = new ArrayValueImpl(); TimeZone localTimeZone = TimeZone.getDefault(); long sec = gmtTime / 1000L; long microsec = (gmtTime - (sec * 1000)) * 1000L; long minutesWest = localTimeZone.getRawOffset() / 1000L / 60L * -1L; long dstTime = localTimeZone.useDaylightTime() ? 1 : 0; result.put("sec", sec); result.put("usec", microsec); result.put("minuteswest", minutesWest); result.put("dsttime", dstTime); return result; } } /** * Returns the formatted date. */ public String gmdate(String format, @Optional("time()") long time) { return date(format, time, true); } /** * Returns the formatted date. */ public long gmmktime(@Optional() Value hourV, @Optional() Value minuteV, @Optional() Value secondV, @Optional() Value monthV, @Optional() Value dayV, @Optional() Value yearV) { QDate localDate = new QDate(false); QDate gmtDate = new QDate(false); long now = Alarm.getCurrentTime(); localDate.setLocalTime(now); long gmtNow = localDate.getGMTTime(); gmtDate.setGMTTime(gmtNow); setMktime(gmtDate, hourV, minuteV, secondV, monthV, dayV, yearV); return gmtDate.getGMTTime() / 1000L; } /** * Returns the formatted date. */ public String gmstrftime(String format, @Optional("-1") long phpTime) { long time; if (phpTime == -1) time = Alarm.getCurrentTime(); else time = 1000 * phpTime; return QDate.formatGMT(time, format); } /** * Convert from a gregorian date to a julian day. */ public double gregoriantojd(int month, int day, int year) { if (month <= 2) { year -= 1; month += 12; } long a = year / 100; long b = a / 4; long c = 2 - a + b; long e = (long) (365.25 * (year + 4716)); long f = (long) (30.6001 * (month + 1)); return (c + day + e + f - 1524.5); } private String date(String format, long time, boolean isGMT) { QDate calendar = isGMT ? _gmtCalendar : _localCalendar; return dateImpl(format, time, calendar); } protected static String date(String format, long time, QDate calendar) { calendar = (QDate) calendar.clone(); return dateImpl(format, time, calendar); } /** * Returns the formatted date. */ private static String dateImpl(String format, long time, QDate calendar) { long now = 1000 * time; synchronized (calendar) { calendar.setGMTTime(now); CharBuffer sb = new CharBuffer(); int len = format.length(); for (int i = 0; i < len; i++) { char ch = format.charAt(i); switch (ch) { // // day // case 'd': { int day = calendar.getDayOfMonth(); sb.append(day / 10); sb.append(day % 10); break; } case 'D': { // subtract 1 to be zero-based for array int day = calendar.getDayOfWeek() - 1; sb.append(_shortDayOfWeek[day]); break; } case 'j': { int day = calendar.getDayOfMonth(); sb.append(day); break; } case 'l': { // subtract 1 to be zero-based for array int day = calendar.getDayOfWeek() - 1; sb.append(_fullDayOfWeek[day]); break; } case 'N': { int day = calendar.getDayOfWeek(); // Mon=1, Sun=7 day = day - 1; if (day == 0) day = 7; sb.append(day); break; } case 'S': { int day = calendar.getDayOfMonth(); switch (day) { case 1: case 21: case 31: sb.append("st"); break; case 2: case 22: sb.append("nd"); break; case 3: case 23: sb.append("rd"); break; default: sb.append("th"); break; } break; } case 'w': { int day = calendar.getDayOfWeek() - 1; sb.append(day); break; } case 'z': { int day = calendar.getDayOfYear(); sb.append(day); break; } // // week // case 'W': { int week = calendar.getWeek(); sb.append(week / 10); sb.append(week % 10); break; } // // month // case 'F': { int month = calendar.getMonth(); sb.append(_fullMonth[month]); break; } case 'm': { int month = calendar.getMonth() + 1; sb.append(month / 10); sb.append(month % 10); break; } case 'M': { int month = calendar.getMonth(); sb.append(_shortMonth[month]); break; } case 'n': { int month = calendar.getMonth() + 1; sb.append(month); break; } case 't': { int days = calendar.getDaysInMonth(); sb.append(days); break; } // // year // case 'L': { if (calendar.isLeapYear()) sb.append(1); else sb.append(0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -