diff --git a/posix.mak b/posix.mak index a62a29a92..ec39be794 100644 --- a/posix.mak +++ b/posix.mak @@ -156,13 +156,12 @@ MAIN = $(ROOT)/emptymain.d # Stuff in std/ STD_MODULES = $(addprefix std/, algorithm array ascii base64 bigint \ bitmanip compiler complex concurrency container contracts conv \ - cpuid cstream ctype csv date datetime datebase dateparse demangle \ - encoding exception file format functional getopt gregorian \ - json loader math mathspecial md5 metastrings mmfile numeric \ - outbuffer parallelism path perf process random range regex \ - regexp signals socket socketstream stdint stdio stdiobase \ - stream string syserror system traits typecons typetuple uni \ - uri utf variant xml zip zlib) + cpuid cstream ctype csv datetime demangle encoding exception \ + file format functional getopt json loader math mathspecial md5 \ + metastrings mmfile numeric outbuffer parallelism path perf \ + process random range regex regexp signals socket socketstream \ + stdint stdio stdiobase stream string syserror system traits \ + typecons typetuple uni uri utf variant xml zip zlib) STD_NET_MODULES = $(addprefix std/net/, isemail curl) diff --git a/std/date.d b/std/date.d deleted file mode 100644 index 22c32b480..000000000 --- a/std/date.d +++ /dev/null @@ -1,1218 +0,0 @@ -// Written in the D programming language. - -/** - * $(RED Deprecated. It will be removed in March 2012. - * Please use std.datetime instead.) - * - * Dates are represented in several formats. The date implementation - * revolves around a central type, $(D d_time), from which other - * formats are converted to and from. Dates are calculated using the - * Gregorian calendar. - * - * References: $(WEB wikipedia.org/wiki/Gregorian_calendar, Gregorian - * calendar (Wikipedia)) - * - * Macros: WIKI = Phobos/StdDate - * - * Copyright: Copyright Digital Mars 2000 - 2009. - * License: Boost License 1.0. - * Authors: $(WEB digitalmars.com, Walter Bright) - * Source: $(PHOBOSSRC std/_date.d) - */ -/* Copyright Digital Mars 2000 - 2009. - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -module std.date; - -import std.conv, std.datebase, std.dateparse, std.exception, std.stdio; -import std.c.stdlib; - -pragma(msg, "Notice: As of Phobos 2.055, std.date and std.dateparse have been " ~ - "deprecated. They will be removed in March 2012. " ~ - "Please use std.datetime instead."); - -deprecated: - -/** - * $(D d_time) is a signed arithmetic type giving the time elapsed - * since January 1, 1970. Negative values are for dates preceding - * 1970. The time unit used is Ticks. Ticks are milliseconds or - * smaller intervals. - * - * The usual arithmetic operations can be performed on d_time, such as adding, - * subtracting, etc. Elapsed time in Ticks can be computed by subtracting a - * starting d_time from an ending d_time. - */ -alias long d_time; - -/** - * A value for d_time that does not represent a valid time. - */ -enum d_time d_time_nan = long.min; - -/** - * Time broken down into its components. - */ -struct Date -{ - int year = int.min; /// use int.min as "nan" year value - int month; /// 1..12 - int day; /// 1..31 - int hour; /// 0..23 - int minute; /// 0..59 - int second; /// 0..59 - int ms; /// 0..999 - int weekday; /// 0: not specified, 1..7: Sunday..Saturday - int tzcorrection = int.min; /// -1200..1200 correction in hours - - /// Parse date out of string s[] and store it in this Date instance. - void parse(string s) - { - DateParse dp; - dp.parse(s, this); - } -} - -enum -{ - hoursPerDay = 24, - minutesPerHour = 60, - msPerMinute = 60 * 1000, - msPerHour = 60 * msPerMinute, - msPerDay = 86_400_000, - ticksPerMs = 1, - ticksPerSecond = 1000, /// Will be at least 1000 - ticksPerMinute = ticksPerSecond * 60, - ticksPerHour = ticksPerMinute * 60, - ticksPerDay = ticksPerHour * 24, -} - -deprecated alias ticksPerSecond TicksPerSecond; -deprecated alias ticksPerMs TicksPerMs; -deprecated alias ticksPerMinute TicksPerMinute; -deprecated alias ticksPerHour TicksPerHour; -deprecated alias ticksPerDay TicksPerDay; - -deprecated -unittest -{ - assert(ticksPerSecond == TicksPerSecond); -} - -__gshared d_time localTZA = 0; - -private immutable char[] daystr = "SunMonTueWedThuFriSat"; -private immutable char[] monstr = "JanFebMarAprMayJunJulAugSepOctNovDec"; - -private immutable int[12] mdays = - [ 0,31,59,90,120,151,181,212,243,273,304,334 ]; - -/******************************** - * Compute year and week [1..53] from t. The ISO 8601 week 1 is the first week - * of the year that includes January 4. Monday is the first day of the week. - * References: - * $(LINK2 http://en.wikipedia.org/wiki/ISO_8601, ISO 8601 (Wikipedia)) - */ - -void toISO8601YearWeek(d_time t, out int year, out int week) -{ - year = yearFromTime(t); - - auto yday = day(t) - dayFromYear(year); - - /* Determine day of week Jan 4 falls on. - * Weeks begin on a Monday. - */ - - auto d = dayFromYear(year); - auto w = (d + 3/*Jan4*/ + 3) % 7; - if (w < 0) - w += 7; - - /* Find yday of beginning of ISO 8601 year - */ - auto ydaybeg = 3/*Jan4*/ - w; - - /* Check if yday is actually the last week of the previous year - */ - if (yday < ydaybeg) - { - year -= 1; - week = 53; - return; - } - - /* Check if yday is actually the first week of the next year - */ - if (yday >= 362) // possible - { int d2; - int ydaybeg2; - - d2 = dayFromYear(year + 1); - w = (d2 + 3/*Jan4*/ + 3) % 7; - if (w < 0) - w += 7; - //printf("w = %d\n", w); - ydaybeg2 = 3/*Jan4*/ - w; - if (d + yday >= d2 + ydaybeg2) - { - year += 1; - week = 1; - return; - } - } - - week = (yday - ydaybeg) / 7 + 1; -} - -/* *********************************** - * Divide time by divisor. Always round down, even if d is negative. - */ - -pure d_time floor(d_time d, int divisor) -{ - return (d < 0 ? d - divisor - 1 : d) / divisor; -} - -int dmod(d_time n, d_time d) -{ d_time r; - - r = n % d; - if (r < 0) - r += d; - assert(cast(int)r == r); - return cast(int)r; -} - -/******************************** - * Calculates the hour from time. - * - * Params: - * time = The time to compute the hour from. - * Returns: - * The calculated hour, 0..23. - */ -int hourFromTime(d_time time) -{ - return dmod(floor(time, msPerHour), hoursPerDay); -} - -/******************************** - * Calculates the minute from time. - * - * Params: - * time = The time to compute the minute from. - * Returns: - * The calculated minute, 0..59. - */ -int minFromTime(d_time time) -{ - return dmod(floor(time, msPerMinute), minutesPerHour); -} - -/******************************** - * Calculates the second from time. - * - * Params: - * time = The time to compute the second from. - * Returns: - * The calculated second, 0..59. - */ -int secFromTime(d_time time) -{ - return dmod(floor(time, ticksPerSecond), 60); -} - -/******************************** - * Calculates the milisecond from time. - * - * Params: - * time = The time to compute the milisecond from. - * Returns: - * The calculated milisecond, 0..999. - */ -int msFromTime(d_time time) -{ - return dmod(time / (ticksPerSecond / 1000), 1000); -} - -int timeWithinDay(d_time t) -{ - return dmod(t, msPerDay); -} - -d_time toInteger(d_time n) -{ - return n; -} - -int day(d_time t) -{ - return cast(int)floor(t, msPerDay); -} - -pure bool leapYear(uint y) -{ - return (y % 4) == 0 && (y % 100 || (y % 400) == 0); -} - -unittest { - assert(!leapYear(1970)); - assert(leapYear(1984)); - assert(leapYear(2000)); - assert(!leapYear(2100)); -} - -/******************************** - * Calculates the number of days that exists in a year. - * - * Leap years have 366 days, while other years have 365. - * - * Params: - * year = The year to compute the number of days from. - * Returns: - * The number of days in the year, 365 or 366. - */ -pure uint daysInYear(uint year) -{ - return (leapYear(year) ? 366 : 365); -} - - -/******************************** - * Calculates the number of days elapsed since 1 January 1970 - * until 1 January of the given year. - * - * Params: - * year = The year to compute the number of days from. - * Returns: - * The number of days elapsed. - * - * Example: - * ---------- - * writeln(dayFromYear(1970)); // writes '0' - * writeln(dayFromYear(1971)); // writes '365' - * writeln(dayFromYear(1972)); // writes '730' - * ---------- - */ -pure int dayFromYear(int year) -{ - return cast(int) (365 * (year - 1970) + - floor((year - 1969), 4) - - floor((year - 1901), 100) + - floor((year - 1601), 400)); -} - -pure d_time timeFromYear(int y) -{ - return cast(d_time)msPerDay * dayFromYear(y); -} - -/***************************** - * Calculates the year from the d_time t. - */ - -pure int yearFromTime(d_time t) -{ - - if (t == d_time_nan) - return 0; - - // Hazard a guess - //y = 1970 + cast(int) (t / (365.2425 * msPerDay)); - // Use integer only math - int y = 1970 + cast(int) (t / (3652425 * (msPerDay / 10000))); - - if (timeFromYear(y) <= t) - { - while (timeFromYear(y + 1) <= t) - y++; - } - else - { - do - { - y--; - } - while (timeFromYear(y) > t); - } - return y; -} - -/******************************* - * Determines if d_time t is a leap year. - * - * A leap year is every 4 years except years ending in 00 that are not - * divsible by 400. - * - * Returns: !=0 if it is a leap year. - * - * References: - * $(LINK2 http://en.wikipedia.org/wiki/Leap_year, Wikipedia) - */ - -pure bool inLeapYear(d_time t) -{ - return leapYear(yearFromTime(t)); -} - -/***************************** - * Calculates the month from the d_time t. - * - * Returns: Integer in the range 0..11, where - * 0 represents January and 11 represents December. - */ - -int monthFromTime(d_time t) -{ - auto year = yearFromTime(t); - auto day = day(t) - dayFromYear(year); - - int month; - if (day < 59) - { - if (day < 31) - { assert(day >= 0); - month = 0; - } - else - month = 1; - } - else - { - day -= leapYear(year); - if (day < 212) - { - if (day < 59) - month = 1; - else if (day < 90) - month = 2; - else if (day < 120) - month = 3; - else if (day < 151) - month = 4; - else if (day < 181) - month = 5; - else - month = 6; - } - else - { - if (day < 243) - month = 7; - else if (day < 273) - month = 8; - else if (day < 304) - month = 9; - else if (day < 334) - month = 10; - else if (day < 365) - month = 11; - else - assert(0); - } - } - return month; -} - -/******************************* - * Compute which day in a month a d_time t is. - * Returns: - * Integer in the range 1..31 - */ -int dateFromTime(d_time t) -{ - auto year = yearFromTime(t); - auto day = day(t) - dayFromYear(year); - auto leap = leapYear(year); - auto month = monthFromTime(t); - int date; - switch (month) - { - case 0: date = day + 1; break; - case 1: date = day - 30; break; - case 2: date = day - 58 - leap; break; - case 3: date = day - 89 - leap; break; - case 4: date = day - 119 - leap; break; - case 5: date = day - 150 - leap; break; - case 6: date = day - 180 - leap; break; - case 7: date = day - 211 - leap; break; - case 8: date = day - 242 - leap; break; - case 9: date = day - 272 - leap; break; - case 10: date = day - 303 - leap; break; - case 11: date = day - 333 - leap; break; - default: - assert(0); - } - return date; -} - -/******************************* - * Compute which day of the week a d_time t is. - * Returns: - * Integer in the range 0..6, where 0 represents Sunday - * and 6 represents Saturday. - */ -int weekDay(d_time t) -{ - auto w = (cast(int)day(t) + 4) % 7; - if (w < 0) - w += 7; - return w; -} - -/*********************************** - * Convert from UTC to local time. - */ - -d_time UTCtoLocalTime(d_time t) -{ - return (t == d_time_nan) - ? d_time_nan - : t + localTZA + daylightSavingTA(t); -} - -/*********************************** - * Convert from local time to UTC. - */ - -d_time localTimetoUTC(d_time t) -{ - return (t == d_time_nan) - ? d_time_nan -/* BUGZILLA 1752 says this line should be: - * : t - localTZA - daylightSavingTA(t); - */ - : t - localTZA - daylightSavingTA(t - localTZA); -} - - -d_time makeTime(d_time hour, d_time min, d_time sec, d_time ms) -{ - return hour * ticksPerHour + - min * ticksPerMinute + - sec * ticksPerSecond + - ms * ticksPerMs; -} - -/* ***************************** - * Params: - * month = 0..11 - * date = day of month, 1..31 - * Returns: - * number of days since start of epoch - */ - -d_time makeDay(d_time year, d_time month, d_time date) -{ - const y = cast(int)(year + floor(month, 12)); - const m = dmod(month, 12); - - const leap = leapYear(y); - auto t = timeFromYear(y) + cast(d_time) mdays[m] * msPerDay; - if (leap && month >= 2) - t += msPerDay; - - if (yearFromTime(t) != y || - monthFromTime(t) != m || - dateFromTime(t) != 1) - { - return d_time_nan; - } - - return day(t) + date - 1; -} - -d_time makeDate(d_time day, d_time time) -{ - if (day == d_time_nan || time == d_time_nan) - return d_time_nan; - - return day * ticksPerDay + time; -} - -d_time timeClip(d_time time) -{ - //printf("TimeClip(%g) = %g\n", time, toInteger(time)); - - return toInteger(time); -} - -/*************************************** - * Determine the date in the month, 1..31, of the nth - * weekday. - * Params: - * year = year - * month = month, 1..12 - * weekday = day of week 0..6 representing Sunday..Saturday - * n = nth occurrence of that weekday in the month, 1..5, where - * 5 also means "the last occurrence in the month" - * Returns: - * the date in the month, 1..31, of the nth weekday - */ - -int dateFromNthWeekdayOfMonth(int year, int month, int weekday, int n) -in -{ - assert(1 <= month && month <= 12); - assert(0 <= weekday && weekday <= 6); - assert(1 <= n && n <= 5); -} -body -{ - // Get day of the first of the month - auto x = makeDay(year, month - 1, 1); - - // Get the week day 0..6 of the first of this month - auto wd = weekDay(makeDate(x, 0)); - - // Get monthday of first occurrence of weekday in this month - auto mday = weekday - wd + 1; - if (mday < 1) - mday += 7; - - // Add in number of weeks - mday += (n - 1) * 7; - - // If monthday is more than the number of days in the month, - // back up to 'last' occurrence - if (mday > 28 && mday > daysInMonth(year, month)) - { assert(n == 5); - mday -= 7; - } - - return mday; -} - -unittest -{ - assert(dateFromNthWeekdayOfMonth(2003, 3, 0, 5) == 30); - assert(dateFromNthWeekdayOfMonth(2003, 10, 0, 5) == 26); - assert(dateFromNthWeekdayOfMonth(2004, 3, 0, 5) == 28); - assert(dateFromNthWeekdayOfMonth(2004, 10, 0, 5) == 31); -} - -/************************************** - * Determine the number of days in a month, 1..31. - * Params: - * month = 1..12 - */ - -int daysInMonth(int year, int month) -{ - switch (month) - { - case 1: - case 3: - case 5: - case 7: - case 8: - case 10: - case 12: - return 31; - case 2: - return 28 + leapYear(year); - case 4: - case 6: - case 9: - case 11: - return 30; - default: - break; - } - return enforce(false, "Invalid month passed to daysInMonth"); -} - -unittest -{ - assert(daysInMonth(2003, 2) == 28); - assert(daysInMonth(2004, 2) == 29); -} - -/************************************* - * Converts UTC time into a text string of the form: - * "Www Mmm dd hh:mm:ss GMT+-TZ yyyy". - * For example, "Tue Apr 02 02:04:57 GMT-0800 1996". - * If time is invalid, i.e. is d_time_nan, - * the string "Invalid date" is returned. - * - * Example: - * ------------------------------------ - d_time lNow; - char[] lNowString; - - // Grab the date and time relative to UTC - lNow = std.date.getUTCtime(); - // Convert this into the local date and time for display. - lNowString = std.date.UTCtoString(lNow); - * ------------------------------------ - */ - -string UTCtoString(d_time time) -{ - // Years are supposed to be -285616 .. 285616, or 7 digits - // "Tue Apr 02 02:04:57 GMT-0800 1996" - auto buffer = new char[29 + 7 + 1]; - - if (time == d_time_nan) - return "Invalid Date"; - - auto dst = daylightSavingTA(time); - auto offset = localTZA + dst; - auto t = time + offset; - auto sign = '+'; - if (offset < 0) - { sign = '-'; -// offset = -offset; - offset = -(localTZA + dst); - } - - auto mn = cast(int)(offset / msPerMinute); - auto hr = mn / 60; - mn %= 60; - - //printf("hr = %d, offset = %g, localTZA = %g, dst = %g, + = %g\n", hr, offset, localTZA, dst, localTZA + dst); - - auto len = sprintf(buffer.ptr, - "%.3s %.3s %02d %02d:%02d:%02d GMT%c%02d%02d %d", - &daystr[weekDay(t) * 3], - &monstr[monthFromTime(t) * 3], - dateFromTime(t), - hourFromTime(t), minFromTime(t), secFromTime(t), - sign, hr, mn, - cast(long)yearFromTime(t)); - - // Ensure no buggy buffer overflows - //printf("len = %d, buffer.length = %d\n", len, buffer.length); - assert(len < buffer.length); - buffer = buffer[0 .. len]; - return assumeUnique(buffer); -} - -/// Alias for UTCtoString (deprecated). -deprecated alias UTCtoString toString; - -/*********************************** - * Converts t into a text string of the form: "Www, dd Mmm yyyy hh:mm:ss UTC". - * If t is invalid, "Invalid date" is returned. - */ - -string toUTCString(d_time t) -{ - // Years are supposed to be -285616 .. 285616, or 7 digits - // "Tue, 02 Apr 1996 02:04:57 GMT" - auto buffer = new char[25 + 7 + 1]; - - if (t == d_time_nan) - return "Invalid Date"; - - auto len = sprintf(buffer.ptr, "%.3s, %02d %.3s %d %02d:%02d:%02d UTC", - &daystr[weekDay(t) * 3], dateFromTime(t), - &monstr[monthFromTime(t) * 3], - yearFromTime(t), - hourFromTime(t), minFromTime(t), secFromTime(t)); - - // Ensure no buggy buffer overflows - assert(len < buffer.length); - - return cast(string) buffer[0 .. len]; -} - -/************************************ - * Converts the date portion of time into a text string of the form: "Www Mmm dd - * yyyy", for example, "Tue Apr 02 1996". - * If time is invalid, "Invalid date" is returned. - */ - -string toDateString(d_time time) -{ - // Years are supposed to be -285616 .. 285616, or 7 digits - // "Tue Apr 02 1996" - auto buffer = new char[29 + 7 + 1]; - - if (time == d_time_nan) - return "Invalid Date"; - - auto dst = daylightSavingTA(time); - auto offset = localTZA + dst; - auto t = time + offset; - - auto len = sprintf(buffer.ptr, "%.3s %.3s %02d %d", - &daystr[weekDay(t) * 3], - &monstr[monthFromTime(t) * 3], - dateFromTime(t), - cast(long)yearFromTime(t)); - - // Ensure no buggy buffer overflows - assert(len < buffer.length); - - return cast(string) buffer[0 .. len]; -} - -/****************************************** - * Converts the time portion of t into a text string of the form: "hh:mm:ss - * GMT+-TZ", for example, "02:04:57 GMT-0800". - * If t is invalid, "Invalid date" is returned. - * The input must be in UTC, and the output is in local time. - */ - -string toTimeString(d_time time) -{ - // "02:04:57 GMT-0800" - auto buffer = new char[17 + 1]; - - if (time == d_time_nan) - return "Invalid Date"; - - auto dst = daylightSavingTA(time); - auto offset = localTZA + dst; - auto t = time + offset; - auto sign = '+'; - if (offset < 0) - { sign = '-'; -// offset = -offset; - offset = -(localTZA + dst); - } - - auto mn = cast(int)(offset / msPerMinute); - auto hr = mn / 60; - mn %= 60; - - //printf("hr = %d, offset = %g, localTZA = %g, dst = %g, + = %g\n", hr, offset, localTZA, dst, localTZA + dst); - - auto len = sprintf(buffer.ptr, "%02d:%02d:%02d GMT%c%02d%02d", - hourFromTime(t), minFromTime(t), secFromTime(t), - sign, hr, mn); - - // Ensure no buggy buffer overflows - assert(len < buffer.length); - - // Lop off terminating 0 - return cast(string) buffer[0 .. len]; -} - - -/****************************************** - * Parses s as a textual date string, and returns it as a d_time. If - * the string is not a valid date, $(D d_time_nan) is returned. - */ - -d_time parse(string s) -{ - try - { - Date dp; - dp.parse(s); - auto time = makeTime(dp.hour, dp.minute, dp.second, dp.ms); - // Assume UTC if no tzcorrection is set (runnable/testdate). - if (dp.tzcorrection != int.min) - { - time += cast(d_time)(dp.tzcorrection / 100) * msPerHour + - cast(d_time)(dp.tzcorrection % 100) * msPerMinute; - } - auto day = makeDay(dp.year, dp.month - 1, dp.day); - auto result = makeDate(day,time); - return timeClip(result); - } - catch - { - return d_time_nan; // erroneous date string - } -} - -extern(C) void std_date_static_this() -{ - localTZA = getLocalTZA(); -} - -version (Windows) -{ - private import std.c.windows.windows; - //import c.time; - - /****** - * Get current UTC time. - */ - d_time getUTCtime() - { - SYSTEMTIME st; - GetSystemTime(&st); // get time in UTC - return SYSTEMTIME2d_time(&st, 0); - //return c.time.time(null) * ticksPerSecond; - } - - static d_time FILETIME2d_time(const FILETIME *ft) - { - SYSTEMTIME st = void; - if (!FileTimeToSystemTime(ft, &st)) - return d_time_nan; - return SYSTEMTIME2d_time(&st, 0); - } - - FILETIME d_time2FILETIME(d_time dt) - { - static assert(10_000_000 >= ticksPerSecond); - static assert(10_000_000 % ticksPerSecond == 0); - enum ulong ticksFrom1601To1970 = 11_644_473_600UL * ticksPerSecond; - ulong t = (dt + ticksFrom1601To1970) * (10_000_000 / ticksPerSecond); - FILETIME result = void; - result.dwLowDateTime = cast(uint) (t & uint.max); - result.dwHighDateTime = cast(uint) (t >> 32); - return result; - } - - unittest - { - auto dt = getUTCtime(); - auto ft = d_time2FILETIME(dt); - auto dt1 = FILETIME2d_time(&ft); - assert(dt == dt1, text(dt, " != ", dt1)); - } - - static d_time SYSTEMTIME2d_time(const SYSTEMTIME *st, d_time t) - { - /* More info: http://delphicikk.atw.hu/listaz.php?id=2667&oldal=52 - */ - d_time day = void; - d_time time = void; - - if (st.wYear) - { - time = makeTime(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); - day = makeDay(st.wYear, st.wMonth - 1, st.wDay); - } - else - { /* wYear being 0 is a flag to indicate relative time: - * wMonth is the month 1..12 - * wDayOfWeek is weekday 0..6 corresponding to Sunday..Saturday - * wDay is the nth time, 1..5, that wDayOfWeek occurs - */ - - auto year = yearFromTime(t); - auto mday = dateFromNthWeekdayOfMonth(year, - st.wMonth, st.wDay, st.wDayOfWeek); - day = makeDay(year, st.wMonth - 1, mday); - time = makeTime(st.wHour, st.wMinute, 0, 0); - } - auto n = makeDate(day,time); - return timeClip(n); - } - - d_time getLocalTZA() - { - TIME_ZONE_INFORMATION tzi = void; - - /* http://msdn.microsoft.com/library/en-us/sysinfo/base/gettimezoneinformation.asp - * http://msdn2.microsoft.com/en-us/library/ms725481.aspx - */ - auto r = GetTimeZoneInformation(&tzi); - //printf("bias = %d\n", tzi.Bias); - //printf("standardbias = %d\n", tzi.StandardBias); - //printf("daylightbias = %d\n", tzi.DaylightBias); - switch (r) - { - case TIME_ZONE_ID_STANDARD: - return -(tzi.Bias + tzi.StandardBias) - * cast(d_time)(60 * ticksPerSecond); - case TIME_ZONE_ID_DAYLIGHT: - // falthrough - //t = -(tzi.Bias + tzi.DaylightBias) * cast(d_time)(60 * ticksPerSecond); - //break; - case TIME_ZONE_ID_UNKNOWN: - return -(tzi.Bias) * cast(d_time)(60 * ticksPerSecond); - default: - return 0; - } - } - - /* - * Get daylight savings time adjust for time dt. - */ - - int daylightSavingTA(d_time dt) - { - TIME_ZONE_INFORMATION tzi = void; - d_time ts; - d_time td; - - /* http://msdn.microsoft.com/library/en-us/sysinfo/base/gettimezoneinformation.asp - */ - auto r = GetTimeZoneInformation(&tzi); - auto t = 0; - switch (r) - { - case TIME_ZONE_ID_STANDARD: - case TIME_ZONE_ID_DAYLIGHT: - if (tzi.StandardDate.wMonth == 0 || - tzi.DaylightDate.wMonth == 0) - break; - - ts = SYSTEMTIME2d_time(&tzi.StandardDate, dt); - td = SYSTEMTIME2d_time(&tzi.DaylightDate, dt); - - if (td <= dt && dt < ts) - { - t = -tzi.DaylightBias * (60 * ticksPerSecond); - //printf("DST is in effect, %d\n", t); - } - else - { - //printf("no DST\n"); - } - break; - - case TIME_ZONE_ID_UNKNOWN: - // Daylight savings time not used in this time zone - break; - - default: - assert(0); - } - return t; - } -} - -version (Posix) -{ - private import core.sys.posix.time; - private import core.sys.posix.sys.time; - - /****** - * Get current UTC time. - */ - d_time getUTCtime() - { timeval tv; - - //printf("getUTCtime()\n"); - if (gettimeofday(&tv, null)) - { // Some error happened - try time() instead - return time(null) * ticksPerSecond; - } - - return tv.tv_sec * cast(d_time)ticksPerSecond + - (tv.tv_usec / (1000000 / cast(d_time)ticksPerSecond)); - } - - d_time getLocalTZA() - { - time_t t; - - time(&t); - version (OSX) - { - tm result; - localtime_r(&t, &result); - return result.tm_gmtoff * ticksPerSecond; - } - else version (FreeBSD) - { - tm result; - localtime_r(&t, &result); - return result.tm_gmtoff * ticksPerSecond; - } - else - { - localtime(&t); // this will set timezone - return -(timezone * ticksPerSecond); - } - } - - /* - * Get daylight savings time adjust for time dt. - */ - - int daylightSavingTA(d_time dt) - { - tm *tmp; - time_t t; - int dst = 0; - - if (dt != d_time_nan) - { - d_time seconds = dt / ticksPerSecond; - t = cast(time_t) seconds; - if (t == seconds) // if in range - { - tmp = localtime(&t); - if (tmp.tm_isdst > 0) - dst = ticksPerHour; // BUG: Assume daylight savings time is plus one hour. - } - else // out of range for system time, use our own calculation - { - /* BUG: this works for the US, but not other timezones. - */ - - dt -= localTZA; - - int year = yearFromTime(dt); - - /* Compute time given year, month 1..12, - * week in month, weekday, hour - */ - d_time dstt(int year, int month, int week, int weekday, int hour) - { - auto mday = dateFromNthWeekdayOfMonth(year, month, weekday, week); - return timeClip(makeDate( - makeDay(year, month - 1, mday), - makeTime(hour, 0, 0, 0))); - } - - d_time start; - d_time end; - if (year < 2007) - { // Daylight savings time goes from 2 AM the first Sunday - // in April through 2 AM the last Sunday in October - start = dstt(year, 4, 1, 0, 2); - end = dstt(year, 10, 5, 0, 2); - } - else - { - // the second Sunday of March to - // the first Sunday in November - start = dstt(year, 3, 2, 0, 2); - end = dstt(year, 11, 1, 0, 2); - } - - if (start <= dt && dt < end) - dst = ticksPerHour; - //writefln("start = %s, dt = %s, end = %s, dst = %s", start, dt, end, dst); - } - } - return dst; - } - -} - - -/+ DOS File Time +/ - -/*** - * Type representing the DOS file date/time format. - */ -alias uint DosFileTime; - -/************************************ - * Convert from DOS file date/time to d_time. - */ - -d_time toDtime(DosFileTime time) -{ - uint dt = cast(uint)time; - - if (dt == 0) - return d_time_nan; - - int year = ((dt >> 25) & 0x7F) + 1980; - int month = ((dt >> 21) & 0x0F) - 1; // 0..12 - int dayofmonth = ((dt >> 16) & 0x1F); // 0..31 - int hour = (dt >> 11) & 0x1F; // 0..23 - int minute = (dt >> 5) & 0x3F; // 0..59 - int second = (dt << 1) & 0x3E; // 0..58 (in 2 second increments) - - d_time t; - - t = std.date.makeDate(std.date.makeDay(year, month, dayofmonth), - std.date.makeTime(hour, minute, second, 0)); - - assert(yearFromTime(t) == year); - assert(monthFromTime(t) == month); - assert(dateFromTime(t) == dayofmonth); - assert(hourFromTime(t) == hour); - assert(minFromTime(t) == minute); - assert(secFromTime(t) == second); - - t -= localTZA + daylightSavingTA(t); - - return t; -} - -/**************************************** - * Convert from d_time to DOS file date/time. - */ - -DosFileTime toDosFileTime(d_time t) -{ uint dt; - - if (t == d_time_nan) - return cast(DosFileTime)0; - - t += localTZA + daylightSavingTA(t); - - uint year = yearFromTime(t); - uint month = monthFromTime(t); - uint dayofmonth = dateFromTime(t); - uint hour = hourFromTime(t); - uint minute = minFromTime(t); - uint second = secFromTime(t); - - dt = (year - 1980) << 25; - dt |= ((month + 1) & 0x0F) << 21; - dt |= (dayofmonth & 0x1F) << 16; - dt |= (hour & 0x1F) << 11; - dt |= (minute & 0x3F) << 5; - dt |= (second >> 1) & 0x1F; - - return cast(DosFileTime)dt; -} - -/** -Benchmarks code for speed assessment and comparison. - -Params: - -fun = aliases of callable objects (e.g. function names). Each should -take no arguments. - -times = The number of times each function is to be executed. - -result = The optional store for the return value. If $(D null) is -passed in, new store is allocated appropriately. - -Returns: - -An array of $(D n) $(D uint)s. Element at slot $(D i) contains the -number of milliseconds spent in calling the $(D i)th function $(D -times) times. - -Example: ----- -int a; -void f0() { } -void f1() { auto b = a; } -void f2() { auto b = to!(string)(a); } -auto r = benchmark!(f0, f1, f2)(10_000_000); ----- - */ -ulong[] benchmark(fun...)(uint times, ulong[] result = null) -{ - result.length = fun.length; - result.length = 0; - foreach (i, Unused; fun) - { - immutable t = getUTCtime(); - foreach (j; 0 .. times) - { - fun[i](); - } - immutable delta = getUTCtime() - t; - result ~= cast(uint)delta; - } - foreach (ref e; result) - { - e *= 1000; - e /= ticksPerSecond; - } - return result; -} - -unittest -{ - int a; - void f0() { } - //void f1() { auto b = to!(string)(a); } - void f2() { auto b = (a); } - auto r = benchmark!(f0, f2)(100); - //writeln(r); -} diff --git a/std/datebase.d b/std/datebase.d deleted file mode 100644 index f95f448c5..000000000 --- a/std/datebase.d +++ /dev/null @@ -1,25 +0,0 @@ -// Written in the D programming language. - -/** - * The only purpose of this module is to do the static construction for - * std.date, to eliminate cyclic construction errors. - * - * Copyright: Copyright Digital Mars 2000 - 2009. - * License: Boost License 1.0. - * Authors: $(WEB digitalmars.com, Walter Bright) - * Source: $(PHOBOSSRC std/_datebase.d) - */ -/* - * Copyright Digital Mars 2000 - 2009. - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -module std.datebase; - -extern(C) void std_date_static_this(); - -shared static this() -{ - std_date_static_this(); -} diff --git a/std/dateparse.d b/std/dateparse.d deleted file mode 100644 index 6b38d703f..000000000 --- a/std/dateparse.d +++ /dev/null @@ -1,784 +0,0 @@ -// Written in the D programming language. - -/** - * $(RED Deprecated. It will be removed in March 2012. - * Please use std.datetime instead.) - * - * dateparse module. - * - * Copyright: Copyright Digital Mars 2000 - 2009. - * License: Boost License 1.0. - * Authors: $(WEB digitalmars.com, Walter Bright) - * Source: $(PHOBOSSRC std/_dateparse.d) - */ -/* - * Copyright Digital Mars 2000 - 2009. - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -module std.dateparse; - -private -{ - import std.algorithm, std.string; - import std.c.stdlib; - import std.date; -} - -deprecated: - -//debug=dateparse; - -class DateParseError : Error -{ - this(string s) - { - super("Invalid date string: " ~ s); - } -} - -struct DateParse -{ - void parse(string s, out Date date) - { - this = DateParse.init; - - //version (Windows) - buffer = (cast(char *)alloca(s.length))[0 .. s.length]; - //else - //buffer = new char[s.length]; - - debug(dateparse) printf("DateParse.parse('%.*s')\n", s); - if (!parseString(s)) - { - goto Lerror; - } - - /+ - if (year == year.init) - year = 0; - else - +/ - debug(dateparse) - printf("year = %d, month = %d, day = %d\n%02d:%02d:%02d.%03d\nweekday = %d, tzcorrection = %d\n", - year, month, day, - hours, minutes, seconds, ms, - weekday, tzcorrection); - if ( - year == year.init || - (month < 1 || month > 12) || - (day < 1 || day > 31) || - (hours < 0 || hours > 23) || - (minutes < 0 || minutes > 59) || - (seconds < 0 || seconds > 59) || - (tzcorrection != int.min && - ((tzcorrection < -2300 || tzcorrection > 2300) || - (tzcorrection % 10))) - ) - { - Lerror: - throw new DateParseError(s); - } - - if (ampm) - { if (hours > 12) - goto Lerror; - if (hours < 12) - { - if (ampm == 2) // if P.M. - hours += 12; - } - else if (ampm == 1) // if 12am - { - hours = 0; // which is midnight - } - } - -// if (tzcorrection != tzcorrection.init) -// tzcorrection /= 100; - - if (year >= 0 && year <= 99) - year += 1900; - - date.year = year; - date.month = month; - date.day = day; - date.hour = hours; - date.minute = minutes; - date.second = seconds; - date.ms = ms; - date.weekday = weekday; - date.tzcorrection = tzcorrection; - } - - -private: - int year = int.min; // our "nan" Date value - int month; // 1..12 - int day; // 1..31 - int hours; // 0..23 - int minutes; // 0..59 - int seconds; // 0..59 - int ms; // 0..999 - int weekday; // 1..7 - int ampm; // 0: not specified - // 1: AM - // 2: PM - int tzcorrection = int.min; // -1200..1200 correction in hours - - string s; - int si; - int number; - char[] buffer; - - enum DP : byte - { - err, - weekday, - month, - number, - end, - colon, - minus, - slash, - ampm, - plus, - tz, - dst, - dsttz, - } - - DP nextToken() - { int nest; - uint c; - int bi; - DP result = DP.err; - - //printf("DateParse::nextToken()\n"); - for (;;) - { - assert(si <= s.length); - if (si == s.length) - { result = DP.end; - goto Lret; - } - //printf("\ts[%d] = '%c'\n", si, s[si]); - switch (s[si]) - { - case ':': result = DP.colon; goto ret_inc; - case '+': result = DP.plus; goto ret_inc; - case '-': result = DP.minus; goto ret_inc; - case '/': result = DP.slash; goto ret_inc; - case '.': - version(DATE_DOT_DELIM) - { - result = DP.slash; - goto ret_inc; - } - else - { - si++; - break; - } - - ret_inc: - si++; - goto Lret; - - case ' ': - case '\n': - case '\r': - case '\t': - case ',': - si++; - break; - - case '(': // comment - nest = 1; - for (;;) - { - si++; - if (si == s.length) - goto Lret; // error - switch (s[si]) - { - case '(': - nest++; - break; - - case ')': - if (--nest == 0) - goto Lendofcomment; - break; - - default: - break; - } - } - Lendofcomment: - si++; - break; - - default: - number = 0; - for (;;) - { - if (si == s.length) - // c cannot be undefined here - break; - c = s[si]; - if (!(c >= '0' && c <= '9')) - break; - result = DP.number; - number = number * 10 + (c - '0'); - si++; - } - if (result == DP.number) - goto Lret; - - bi = 0; - bufloop: - while (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') - { - if (c < 'a') // if upper case - c += cast(uint)'a' - cast(uint)'A'; // to lower case - buffer[bi] = cast(char)c; - bi++; - do - { - si++; - if (si == s.length) - break bufloop; - c = s[si]; - } while (c == '.'); // ignore embedded '.'s - } - result = classify(buffer[0 .. bi].idup); - goto Lret; - } - } - Lret: - //printf("-DateParse::nextToken()\n"); - return result; - } - - DP classify(string buf) - { - struct DateID - { - string name; - DP tok; - short value; - } - - static immutable DateID dateidtab[] = - [ - { "january", DP.month, 1}, - { "february", DP.month, 2}, - { "march", DP.month, 3}, - { "april", DP.month, 4}, - { "may", DP.month, 5}, - { "june", DP.month, 6}, - { "july", DP.month, 7}, - { "august", DP.month, 8}, - { "september", DP.month, 9}, - { "october", DP.month, 10}, - { "november", DP.month, 11}, - { "december", DP.month, 12}, - { "jan", DP.month, 1}, - { "feb", DP.month, 2}, - { "mar", DP.month, 3}, - { "apr", DP.month, 4}, - { "jun", DP.month, 6}, - { "jul", DP.month, 7}, - { "aug", DP.month, 8}, - { "sep", DP.month, 9}, - { "sept", DP.month, 9}, - { "oct", DP.month, 10}, - { "nov", DP.month, 11}, - { "dec", DP.month, 12}, - - { "sunday", DP.weekday, 1}, - { "monday", DP.weekday, 2}, - { "tuesday", DP.weekday, 3}, - { "tues", DP.weekday, 3}, - { "wednesday", DP.weekday, 4}, - { "wednes", DP.weekday, 4}, - { "thursday", DP.weekday, 5}, - { "thur", DP.weekday, 5}, - { "thurs", DP.weekday, 5}, - { "friday", DP.weekday, 6}, - { "saturday", DP.weekday, 7}, - - { "sun", DP.weekday, 1}, - { "mon", DP.weekday, 2}, - { "tue", DP.weekday, 3}, - { "wed", DP.weekday, 4}, - { "thu", DP.weekday, 5}, - { "fri", DP.weekday, 6}, - { "sat", DP.weekday, 7}, - - { "am", DP.ampm, 1}, - { "pm", DP.ampm, 2}, - - { "gmt", DP.tz, +000}, - { "ut", DP.tz, +000}, - { "utc", DP.tz, +000}, - { "wet", DP.tz, +000}, - { "z", DP.tz, +000}, - { "wat", DP.tz, +100}, - { "a", DP.tz, +100}, - { "at", DP.tz, +200}, - { "b", DP.tz, +200}, - { "c", DP.tz, +300}, - { "ast", DP.tz, +400}, - { "d", DP.tz, +400}, - { "est", DP.tz, +500}, - { "e", DP.tz, +500}, - { "cst", DP.tz, +600}, - { "f", DP.tz, +600}, - { "mst", DP.tz, +700}, - { "g", DP.tz, +700}, - { "pst", DP.tz, +800}, - { "h", DP.tz, +800}, - { "yst", DP.tz, +900}, - { "i", DP.tz, +900}, - { "ahst", DP.tz, +1000}, - { "cat", DP.tz, +1000}, - { "hst", DP.tz, +1000}, - { "k", DP.tz, +1000}, - { "nt", DP.tz, +1100}, - { "l", DP.tz, +1100}, - { "idlw", DP.tz, +1200}, - { "m", DP.tz, +1200}, - - { "cet", DP.tz, -100}, - { "fwt", DP.tz, -100}, - { "met", DP.tz, -100}, - { "mewt", DP.tz, -100}, - { "swt", DP.tz, -100}, - { "n", DP.tz, -100}, - { "eet", DP.tz, -200}, - { "o", DP.tz, -200}, - { "bt", DP.tz, -300}, - { "p", DP.tz, -300}, - { "zp4", DP.tz, -400}, - { "q", DP.tz, -400}, - { "zp5", DP.tz, -500}, - { "r", DP.tz, -500}, - { "zp6", DP.tz, -600}, - { "s", DP.tz, -600}, - { "wast", DP.tz, -700}, - { "t", DP.tz, -700}, - { "cct", DP.tz, -800}, - { "u", DP.tz, -800}, - { "jst", DP.tz, -900}, - { "v", DP.tz, -900}, - { "east", DP.tz, -1000}, - { "gst", DP.tz, -1000}, - { "w", DP.tz, -1000}, - { "x", DP.tz, -1100}, - { "idle", DP.tz, -1200}, - { "nzst", DP.tz, -1200}, - { "nzt", DP.tz, -1200}, - { "y", DP.tz, -1200}, - - { "bst", DP.dsttz, 000}, - { "adt", DP.dsttz, +400}, - { "edt", DP.dsttz, +500}, - { "cdt", DP.dsttz, +600}, - { "mdt", DP.dsttz, +700}, - { "pdt", DP.dsttz, +800}, - { "ydt", DP.dsttz, +900}, - { "hdt", DP.dsttz, +1000}, - { "mest", DP.dsttz, -100}, - { "mesz", DP.dsttz, -100}, - { "sst", DP.dsttz, -100}, - { "fst", DP.dsttz, -100}, - { "wadt", DP.dsttz, -700}, - { "eadt", DP.dsttz, -1000}, - { "nzdt", DP.dsttz, -1200}, - - { "dst", DP.dst, 0}, - ]; - - //message(DTEXT("DateParse::classify('%s')\n"), buf); - - // Do a linear search. Yes, it would be faster with a binary - // one. - for (uint i = 0; i < dateidtab.length; i++) - { - if (cmp(dateidtab[i].name, buf) == 0) - { - number = dateidtab[i].value; - return dateidtab[i].tok; - } - } - return DP.err; - } - - int parseString(string s) - { - int n1; - int dp; - int sisave; - int result; - - //message(DTEXT("DateParse::parseString('%ls')\n"), s); - this.s = s; - si = 0; - dp = nextToken(); - for (;;) - { - //message(DTEXT("\tdp = %d\n"), dp); - switch (dp) - { - case DP.end: - result = 1; - Lret: - return result; - - case DP.err: - case_error: - //message(DTEXT("\terror\n")); - default: - result = 0; - goto Lret; - - case DP.minus: - break; // ignore spurious '-' - - case DP.weekday: - weekday = number; - break; - - case DP.month: // month day, [year] - month = number; - dp = nextToken(); - if (dp == DP.number) - { - day = number; - sisave = si; - dp = nextToken(); - if (dp == DP.number) - { - n1 = number; - dp = nextToken(); - if (dp == DP.colon) - { // back up, not a year - si = sisave; - } - else - { year = n1; - continue; - } - break; - } - } - continue; - - case DP.number: - n1 = number; - dp = nextToken(); - switch (dp) - { - case DP.end: - year = n1; - break; - - case DP.minus: - case DP.slash: // n1/ ? ? ? - dp = parseCalendarDate(n1); - if (dp == DP.err) - goto case_error; - break; - - case DP.colon: // hh:mm [:ss] [am | pm] - dp = parseTimeOfDay(n1); - if (dp == DP.err) - goto case_error; - break; - - case DP.ampm: - hours = n1; - minutes = 0; - seconds = 0; - ampm = number; - break; - - case DP.month: - day = n1; - month = number; - dp = nextToken(); - if (dp == DP.number) - { // day month year - year = number; - dp = nextToken(); - } - break; - - default: - year = n1; - break; - } - continue; - } - dp = nextToken(); - } - // @@@ bug in the compiler: this is never reachable - assert(0); - } - - int parseCalendarDate(int n1) - { - int n2; - int n3; - int dp; - - debug(dateparse) printf("DateParse.parseCalendarDate(%d)\n", n1); - dp = nextToken(); - if (dp == DP.month) // day/month - { - day = n1; - month = number; - dp = nextToken(); - if (dp == DP.number) - { // day/month year - year = number; - dp = nextToken(); - } - else if (dp == DP.minus || dp == DP.slash) - { // day/month/year - dp = nextToken(); - if (dp != DP.number) - goto case_error; - year = number; - dp = nextToken(); - } - return dp; - } - if (dp != DP.number) - goto case_error; - n2 = number; - //message(DTEXT("\tn2 = %d\n"), n2); - dp = nextToken(); - if (dp == DP.minus || dp == DP.slash) - { - dp = nextToken(); - if (dp != DP.number) - goto case_error; - n3 = number; - //message(DTEXT("\tn3 = %d\n"), n3); - dp = nextToken(); - - // case1: year/month/day - // case2: month/day/year - int case1, case2; - - case1 = (n1 > 12 || - (n2 >= 1 && n2 <= 12) && - (n3 >= 1 && n3 <= 31)); - case2 = ((n1 >= 1 && n1 <= 12) && - (n2 >= 1 && n2 <= 31) || - n3 > 31); - if (case1 == case2) - goto case_error; - if (case1) - { - year = n1; - month = n2; - day = n3; - } - else - { - month = n1; - day = n2; - year = n3; - } - } - else - { // must be month/day - month = n1; - day = n2; - } - return dp; - - case_error: - return DP.err; - } - - int parseTimeOfDay(int n1) - { - int dp; - int sign; - - // 12am is midnight - // 12pm is noon - - //message(DTEXT("DateParse::parseTimeOfDay(%d)\n"), n1); - hours = n1; - dp = nextToken(); - if (dp != DP.number) - goto case_error; - minutes = number; - dp = nextToken(); - if (dp == DP.colon) - { - dp = nextToken(); - if (dp != DP.number) - goto case_error; - seconds = number; - dp = nextToken(); - } - else - seconds = 0; - - if (dp == DP.ampm) - { - ampm = number; - dp = nextToken(); - } - else if (dp == DP.plus || dp == DP.minus) - { - Loffset: - sign = (dp == DP.minus) ? -1 : 1; - dp = nextToken(); - if (dp != DP.number) - goto case_error; - tzcorrection = -sign * number; - dp = nextToken(); - } - else if (dp == DP.tz) - { - tzcorrection = number; - dp = nextToken(); - if (number == 0 && (dp == DP.plus || dp == DP.minus)) - goto Loffset; - if (dp == DP.dst) - { tzcorrection += 100; - dp = nextToken(); - } - } - else if (dp == DP.dsttz) - { - tzcorrection = number; - dp = nextToken(); - } - - return dp; - - case_error: - return DP.err; - } - -} - -unittest -{ - DateParse dp; - Date d; - - dp.parse("March 10, 1959 12:00 -800", d); - assert(d.year == 1959); - assert(d.month == 3); - assert(d.day == 10); - assert(d.hour == 12); - assert(d.minute == 0); - assert(d.second == 0); - assert(d.ms == 0); - assert(d.weekday == 0); - assert(d.tzcorrection == 800); - - dp.parse("Tue Apr 02 02:04:57 GMT-0800 1996", d); - assert(d.year == 1996); - assert(d.month == 4); - assert(d.day == 2); - assert(d.hour == 2); - assert(d.minute == 4); - assert(d.second == 57); - assert(d.ms == 0); - assert(d.weekday == 3); - assert(d.tzcorrection == 800); - - dp.parse("March 14, -1980 21:14:50", d); - assert(d.year == 1980); - assert(d.month == 3); - assert(d.day == 14); - assert(d.hour == 21); - assert(d.minute == 14); - assert(d.second == 50); - assert(d.ms == 0); - assert(d.weekday == 0); - assert(d.tzcorrection == int.min); - - dp.parse("Tue Apr 02 02:04:57 1996", d); - assert(d.year == 1996); - assert(d.month == 4); - assert(d.day == 2); - assert(d.hour == 2); - assert(d.minute == 4); - assert(d.second == 57); - assert(d.ms == 0); - assert(d.weekday == 3); - assert(d.tzcorrection == int.min); - - dp.parse("Tue, 02 Apr 1996 02:04:57 G.M.T.", d); - assert(d.year == 1996); - assert(d.month == 4); - assert(d.day == 2); - assert(d.hour == 2); - assert(d.minute == 4); - assert(d.second == 57); - assert(d.ms == 0); - assert(d.weekday == 3); - assert(d.tzcorrection == 0); - - dp.parse("December 31, 3000", d); - assert(d.year == 3000); - assert(d.month == 12); - assert(d.day == 31); - assert(d.hour == 0); - assert(d.minute == 0); - assert(d.second == 0); - assert(d.ms == 0); - assert(d.weekday == 0); - assert(d.tzcorrection == int.min); - - dp.parse("Wed, 31 Dec 1969 16:00:00 GMT", d); - assert(d.year == 1969); - assert(d.month == 12); - assert(d.day == 31); - assert(d.hour == 16); - assert(d.minute == 0); - assert(d.second == 0); - assert(d.ms == 0); - assert(d.weekday == 4); - assert(d.tzcorrection == 0); - - dp.parse("1/1/1999 12:30 AM", d); - assert(d.year == 1999); - assert(d.month == 1); - assert(d.day == 1); - assert(d.hour == 0); - assert(d.minute == 30); - assert(d.second == 0); - assert(d.ms == 0); - assert(d.weekday == 0); - assert(d.tzcorrection == int.min); - - dp.parse("Tue, 20 May 2003 15:38:58 +0530", d); - assert(d.year == 2003); - assert(d.month == 5); - assert(d.day == 20); - assert(d.hour == 15); - assert(d.minute == 38); - assert(d.second == 58); - assert(d.ms == 0); - assert(d.weekday == 3); - assert(d.tzcorrection == -530); - - debug(dateparse) printf("year = %d, month = %d, day = %d\n%02d:%02d:%02d.%03d\nweekday = %d, tzcorrection = %d\n", - d.year, d.month, d.day, - d.hour, d.minute, d.second, d.ms, - d.weekday, d.tzcorrection); -} diff --git a/std/datetime.d b/std/datetime.d index bc897788b..59d3512ff 100644 --- a/std/datetime.d +++ b/std/datetime.d @@ -31163,61 +31163,6 @@ version(testStdDateTime) unittest // Section with public helper functions and templates. //============================================================================== -/++ - $(RED Deprecated. It will be removed in March 2012. This is only here to - help transition code which uses std.date to using std.datetime.) - - Returns a $(D d_time) for the given $(D SysTime). - +/ -deprecated long sysTimeToDTime(in SysTime sysTime) -{ - return convert!("hnsecs", "msecs")(sysTime.stdTime - 621355968000000000L); -} - -version(testStdDateTime) unittest -{ - _assertPred!"=="(sysTimeToDTime(SysTime(DateTime(1970, 1, 1), UTC())), - 0); - _assertPred!"=="(sysTimeToDTime(SysTime(DateTime(1970, 1, 1), FracSec.from!"msecs"(1), UTC())), - 1); - _assertPred!"=="(sysTimeToDTime(SysTime(DateTime(1969, 12, 31, 23, 59, 59), FracSec.from!"msecs"(999), UTC())), - -1); - - _assertPred!"=="(sysTimeToDTime(SysTime(DateTime(1970, 1, 2), UTC())), - 86_400_000); - _assertPred!"=="(sysTimeToDTime(SysTime(DateTime(1969, 12, 31), UTC())), - -86_400_000); -} - - -/++ - $(RED Deprecated. It will be removed in March 2012. This is only here to - help transition code which uses std.date to using std.datetime.) - - Returns a $(D SysTime) for the given $(D d_time). - +/ -deprecated SysTime dTimeToSysTime(long dTime, immutable TimeZone tz = null) -{ - immutable hnsecs = convert!("msecs", "hnsecs")(dTime) + 621355968000000000L; - - return SysTime(hnsecs, tz); -} - -version(testStdDateTime) unittest -{ - _assertPred!"=="(dTimeToSysTime(0), - SysTime(DateTime(1970, 1, 1), UTC())); - _assertPred!"=="(dTimeToSysTime(1), - SysTime(DateTime(1970, 1, 1), FracSec.from!"msecs"(1), UTC())); - _assertPred!"=="(dTimeToSysTime(-1), - SysTime(DateTime(1969, 12, 31, 23, 59, 59), FracSec.from!"msecs"(999), UTC())); - - _assertPred!"=="(dTimeToSysTime(86_400_000), - SysTime(DateTime(1970, 1, 2), UTC())); - _assertPred!"=="(dTimeToSysTime(-86_400_000), - SysTime(DateTime(1969, 12, 31), UTC())); -} - /++ Whether the given type defines all of the necessary functions for it to diff --git a/std/gregorian.d b/std/gregorian.d deleted file mode 100644 index 660fae5b5..000000000 --- a/std/gregorian.d +++ /dev/null @@ -1,422 +0,0 @@ -// Written in the D programming language. - -/** - * $(RED Deprecated. It will be removed in March 2012. - * Please use std.datetime instead.) - -Macros: -WIKI = Phobos/StdGregorian - -Copyright: Copyright Andrei Alexandrescu 2008 - 2009. -License: Boost License 1.0. -Authors: $(WEB erdani.org, Andrei Alexandrescu) -Source: $(PHOBOSSRC std/_gregorian.d) -*/ -/* - Copyright Andrei Alexandrescu 2010-. -Distributed under the Boost Software License, Version 1.0. - (See accompanying file LICENSE_1_0.txt or copy at - http://www.boost.org/LICENSE_1_0.txt) -*/ -module std.gregorian; - -pragma(msg, "Notice: As of Phobos 2.055, std.gregorian has been deprecated. " ~ - "It will be removed in March 2012. Please use std.datetime instead."); - -version(posix) import core.sys.posix.time; -import std.typecons; -import std.conv; -version(unittest) import std.stdio; - -deprecated: - -version(none) unittest -{ - auto d = Date(2010, May, 1); - auto d1 = d; - assert(d.year == 2010); - assert(d.month == May); - assert(d.day == 1); - assert(d.dayOfWeek == 6); - assert(d.dayOfYear == 121); - assert(Date(2010, Jan, 5).dayOfYear == 5); -} - -version(none) unittest -{ - auto d1 = Date(negInfin); - auto d2 = Date(posInfin); - auto d3 = Date(notADateTime); - auto d4 = Date(maxDateTime); - auto d5 = Date(minDateTime); -} - -version(none) unittest -{ - auto d1 = fromString("2002-1-25"); - auto d2 = fromUndelimitedString("20020125"); -} - -version(none) unittest -{ - auto d1 = dayClockLocalDay(); - auto d2 = dayClockUniversalDay(); -} - -alias ushort GregYear; -alias ushort GregMonth; -alias ushort GregDay; -alias uint GregDayOfWeek; -alias uint GregDayOfYear; - -enum { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec } - -// Special constants -struct Special { ulong value; } -static immutable - notADateTime = Special(0), - negInfin = Special(1), - posInfin = Special(2), - minDateTime = Special(3), - maxDateTime = Special(4), - notSpecial = Special(5); - -struct Date -{ - this(uint year, uint month, uint day) - { - immutable - a = cast(ushort)((14-month)/12), - y = cast(ushort)(year + 4800 - a), - m = cast(ushort)(month + 12*a - 3); - days_ = day + ((153*m + 2)/5) + 365*y + (y/4) - - (y/100) + (y/400) - 32045; - } - - this(Special s) - { - } - - // Accessors - @property GregYear year() const - { - immutable - a = days_ + 32044, - b = (4*a + 3)/146097, - c = a-((146097*b)/4), - d = (4*c + 3)/1461, - e = c - (1461*d)/4, - m = (5*e + 2)/153; - //auto day = cast(ushort) (e - ((153*m + 2)/5) + 1); - //auto month = cast(ushort) (m + 3 - 12 * (m/10)); - immutable year = cast(ushort) (100*b + d - 4800 + (m/10)); - - return year; - } - - @property GregMonth month() const - { - immutable - a = days_ + 32044, - b = (4*a + 3)/146097, - c = a-((146097*b)/4), - d = (4*c + 3)/1461, - e = c - (1461*d)/4, - m = (5*e + 2)/153; - //auto day = cast(ushort) (e - ((153*m + 2)/5) + 1); - immutable month = cast(ushort) (m + 3 - 12 * (m/10)); - //auto year = cast(ushort) (100*b + d - 4800 + (m/10)); - - return month; - } - - @property GregDay day() const - { - immutable - a = days_ + 32044, - b = (4*a + 3)/146097, - c = a-((146097*b)/4), - d = (4*c + 3)/1461, - e = c - (1461*d)/4, - m = (5*e + 2)/153, - day = cast(ushort) (e - ((153*m + 2)/5) + 1); - //auto month = cast(ushort) (m + 3 - 12 * (m/10)); - //auto year = cast(ushort) (100*b + d - 4800 + (m/10)); - - return day; - } - - @property Tuple!(GregYear, GregMonth, GregDay) - yearMonthDay() const - { - immutable - a = days_ + 32044, - b = (4*a + 3)/146097, - c = a-((146097*b)/4), - d = (4*c + 3)/1461, - e = c - (1461*d)/4, - m = (5*e + 2)/153; - auto day = cast(ushort) (e - ((153*m + 2)/5) + 1); - auto month = cast(ushort) (m + 3 - 12 * (m/10)); - auto year = cast(ushort) (100*b + d - 4800 + (m/10)); - - return tuple(year, month, day); - } - - @property GregDayOfWeek dayOfWeek() const - { - immutable - ymd = yearMonthDay, - a = cast(ushort) ((14-ymd._1)/12), - y = cast(ushort) (ymd._0 - a), - m = cast(ushort) (ymd._1 + 12*a - 2), - d = cast(ushort) ((ymd._2 + y + (y/4) - (y/100) + - (y/400) + (31*m)/12) % 7); - return d; - } - - @property GregDayOfYear dayOfYear() const - { - const start_of_year = Date(year(), 1, 1); - auto doy = cast(ushort) ((this - start_of_year).days() + 1); - return cast(GregDayOfYear) doy; - } - - @property Date endOfMonth() const - { - assert(0); - } - - @property bool isInfinity() const - { - return isNegInfinity || isPosInfinity; - } - - @property bool isNegInfinity() const - { - return days_ == negInfin.value; - } - - @property bool isPosInfinity() const - { - return days_ == posInfin.value; - } - - @property bool isNotADate() const - { - return days_ == notADateTime.value; - } - - @property bool isSpecial() const - { - return isNotADate || isInfinity; - } - - @property Special asSpecial() const - { - return days_ < notSpecial.value - ? Special(days_) - : notSpecial; - } - - @property long modJulianDay() const - { - auto ymd = yearMonthDay(); - return julianDay(ymd) - 2400001; //prerounded - } - - static @property long julianDay(Tuple!(ushort, ushort, ushort) ymd) - { - immutable - a = cast(ushort) ((14-ymd._1)/12), - y = cast(ushort) (ymd._0 + 4800 - a), - m = cast(ushort) (ymd._1 + 12*a - 3), - d = ymd._2 + ((153*m + 2)/5) + 365*y + (y/4) - (y/100) - + (y/400) - 32045; - return d; - } - - static bool isLeapYear(uint year) - { - //divisible by 4, not if divisible by 100, but true if - //divisible by 400 - return (!(year % 4)) && ((year % 100) || (!(year % 400))); - } - - @property int weekNumber() const - { - auto - ymd = yearMonthDay, - julianbegin = julianDay(tuple(cast(ushort)ymd._0, - cast(ushort)1, cast(ushort)1)), - juliantoday = julianDay(ymd); - long day = (julianbegin + 3) % 7; - ulong week = (juliantoday + day - julianbegin + 4)/7; - - if ((week >= 1) && (week <= 52)) - { - return cast(int) week; - } - - if ((week == 53)) - { - if((day==6) ||(day == 5 && isLeapYear(ymd._0))) - { - return cast(int) week; //under these circumstances week == 53. - } - else - { - return 1; //monday - wednesday is in week 1 of next year - } - } - - //if the week is not in current year recalculate using the - //previous year as the beginning year - else - if (week == 0) - { - julianbegin = julianDay( - tuple(cast(ushort) (ymd._0 - 1), cast(ushort) 1, - cast(ushort) 1)); - juliantoday = julianDay(ymd); - day = (julianbegin + 3) % 7; - week = (juliantoday + day - julianbegin + 4)/7; - return cast(int) week; - } - return cast(int) week; //not reachable -- well except if day == 5 and - //is_leap_year != true - } - - @property uint endOfMonthDay() const - { - switch (month) { - case 2: - if (isLeapYear(year)) { - return 29; - } else { - return 28; - } - case 4: - case 6: - case 9: - case 11: - return 30; - default: - return 31; - } - } - - @property string toSimpleString() const - { - auto ymd = yearMonthDay; - return text(ymd._0, '-', ymd._1, '-', ymd._2); - } - - @property string toIsoString() const - { - assert(0); - } - - @property string toIsoExtendedString() const - { - assert(0); - } - - bool opEquals(ref const Date rhs) const - { - return days_ == rhs.days_; - } - - int opCmp(in Date rhs) const - { - return days_ < rhs.days_ ? -1 : days_ > rhs.days_ ? 1 : 0; - } - - // Date opBinary(string op)(const Date d) const - // if (op == "-") - // { - // } - - Days opBinary(string op)(const Date d) const - if (op == "-") - { - if (!isSpecial && !d.isSpecial) - { - return Days(days_ - d.days_); - } - else - { - assert(0); - } - } - - static if (is(tm)) - { - tm toTm() - { - assert(0); - } - } - - private ulong days_; -} - -struct Days -{ - private long days_; - this(long d) { days_ = d; } - this(Special s) { } - @property long days() const { return days_; } - @property bool isNegative() const { return days_ < 0; } - @property static Days unit() { return Days(1); } - @property bool isSpecial() - { - assert(0); - } - bool opEquals(ref const Days rhs) const - { - return days_ == rhs.days_; - } - - int opCmp(in Days rhs) const - { - return days_ < rhs.days_ ? -1 : days_ > rhs.days_ ? 1 : 0; - } - - Days opBinary(string op)(Days d) const - if (op == "+" || op == "-") - { - } -} - -Date fromString(in char[] s) -{ - Date result; - return result; -} - -Date fromUndelimitedString(in char[] s) -{ - Date result; - return result; -} - -Date dayClockLocalDay() -{ - Date result; - return result; -} - -Date dayClockUniversalDay() -{ - Date result; - return result; -} - -static if (is(tm)) -{ - Date dateFromTm(tm) - { - assert(0); - } -} diff --git a/unittest.d b/unittest.d index ba32689db..2b2f44fac 100644 --- a/unittest.d +++ b/unittest.d @@ -22,8 +22,7 @@ public import std.conv; public import std.cpuid; public import std.cstream; public import std.ctype; -public import std.date; -public import std.dateparse; +public import std.datetime; public import std.demangle; public import std.file; public import std.format; @@ -79,7 +78,7 @@ version (all) int a[]; a.reverse; // adi a.sort; // qsort - std.date.getUTCtime(); // date + Clock.currTime(); // datetime Exception e = new ReadException(""); // stream din.eof(); // cstream isValidDchar(cast(dchar)0); // utf diff --git a/win32.mak b/win32.mak index 553d4de73..33e73e0f5 100644 --- a/win32.mak +++ b/win32.mak @@ -109,7 +109,7 @@ SRCS_12 = std\array.d std\functional.d std\range.d \ std\path.d std\outbuffer.d std\utf.d SRCS_2 = std\csv.d std\math.d std\complex.d std\numeric.d std\bigint.d \ - std\dateparse.d std\date.d std\datetime.d \ + std\datetime.d \ std\metastrings.d std\bitmanip.d std\typecons.d \ std\uni.d std\base64.d std\md5.d std\ctype.d std\ascii.d \ std\demangle.d std\uri.d std\mmfile.d std\getopt.d \ @@ -126,13 +126,11 @@ SRCS_3 = std\variant.d \ std\stream.d std\socket.d std\socketstream.d \ std\perf.d std\container.d std\conv.d \ std\zip.d std\cstream.d std\loader.d \ - std\datebase.d \ std\regex.d \ std\stdarg.d \ std\stdint.d \ std\json.d \ std\parallelism.d \ - std\gregorian.d \ std\mathspecial.d \ std\internal\math\biguintcore.d \ std\internal\math\biguintnoasm.d std\internal\math\biguintx86.d \ @@ -195,7 +193,6 @@ DOCS= $(DOC)\object.html \ $(DOC)\std_cstream.html \ $(DOC)\std_ctype.html \ $(DOC)\std_csv.html \ - $(DOC)\std_date.html \ $(DOC)\std_datetime.html \ $(DOC)\std_demangle.html \ $(DOC)\std_encoding.html \ @@ -205,7 +202,6 @@ DOCS= $(DOC)\object.html \ $(DOC)\std_functional.html \ $(DOC)\std_gc.html \ $(DOC)\std_getopt.html \ - $(DOC)\std_gregorian.html \ $(DOC)\std_json.html \ $(DOC)\std_math.html \ $(DOC)\std_mathspecial.html \ @@ -263,10 +259,10 @@ DOCS= $(DOC)\object.html \ SRC= unittest.d crc32.d index.d SRC_STD= std\zlib.d std\zip.d std\stdint.d std\container.d std\conv.d std\utf.d std\uri.d \ - std\math.d std\string.d std\path.d std\date.d std\datetime.d \ + std\math.d std\string.d std\path.d std\datetime.d \ std\ctype.d std\csv.d std\file.d std\compiler.d std\system.d \ std\outbuffer.d std\md5.d std\base64.d \ - std\dateparse.d std\mmfile.d \ + std\mmfile.d \ std\syserror.d \ std\regexp.d std\random.d std\stream.d std\process.d \ std\socket.d std\socketstream.d std\loader.d std\stdarg.d std\format.d \ @@ -278,8 +274,8 @@ SRC_STD= std\zlib.d std\zip.d std\stdint.d std\container.d std\conv.d std\utf.d std\functional.d std\algorithm.d std\array.d std\typecons.d \ std\json.d std\xml.d std\encoding.d std\bigint.d std\concurrency.d \ std\range.d std\stdiobase.d std\parallelism.d \ - std\regex.d std\datebase.d \ - std\gregorian.d std\exception.d std\ascii.d + std\regex.d \ + std\exception.d std\ascii.d SRC_STD_NET= std\net\isemail.d std\net\curl.d @@ -433,12 +429,6 @@ ctype.obj : std\ctype.d csv.obj : std\csv.d $(DMD) -c $(DFLAGS) std\csv.d -date.obj : std\dateparse.d std\date.d - $(DMD) -c $(DFLAGS) std\date.d - -dateparse.obj : std\dateparse.d std\date.d - $(DMD) -c $(DFLAGS) std\dateparse.d - datetime.obj : std\datetime.d $(DMD) -c $(DFLAGS) std\datetime.d @@ -746,9 +736,6 @@ $(DOC)\std_ctype.html : $(STDDOC) std\ctype.d $(DOC)\std_csv.html : $(STDDOC) std\csv.d $(DMD) -c -o- $(DFLAGS) -Df$(DOC)\std_csv.html $(STDDOC) std\csv.d -$(DOC)\std_date.html : $(STDDOC) std\date.d - $(DMD) -c -o- $(DDOCFLAGS) -Df$(DOC)\std_date.html $(STDDOC) std\date.d - $(DOC)\std_datetime.html : $(STDDOC) std\datetime.d $(DMD) -c -o- $(DDOCFLAGS) -Df$(DOC)\std_datetime.html $(STDDOC) std\datetime.d @@ -773,9 +760,6 @@ $(DOC)\std_gc.html : $(STDDOC) $(DRUNTIME)\src\core\memory.d $(DOC)\std_getopt.html : $(STDDOC) std\getopt.d $(DMD) -c -o- $(DDOCFLAGS) -Df$(DOC)\std_getopt.html $(STDDOC) std\getopt.d -$(DOC)\std_gregorian.html : $(STDDOC) std\gregorian.d - $(DMD) -c -o- $(DDOCFLAGS) -Df$(DOC)\std_gregorian.html $(STDDOC) std\gregorian.d - $(DOC)\std_json.html : $(STDDOC) std\json.d $(DMD) -c -o- $(DDOCFLAGS) -Df$(DOC)\std_json.html $(STDDOC) std\json.d