예제 #1
0
        public bool hequals(object obj)
        {
            if (!(obj is HTimeZone))
            {
                return(false);
            }
            // Compare the timezone by name
            HTimeZone objTZ      = (HTimeZone)obj;
            string    strGMTDNTZ = "Dubai";

            if ((m_strName == "GMT") || (m_strName == "Rel"))
            {
                if ((obj.ToString().CompareTo(m_strName) == 0) || (obj.ToString().CompareTo(strGMTDNTZ) == 0))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            if (obj.ToString().CompareTo(m_strName) == 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #2
0
 private HDateTime(DateTimeOffset dto, HTimeZone htz)
 {
     m_dtoParsed = dto;
     TimeZone    = htz;
     date        = HDate.make(dto.Year, dto.Month, dto.Day);
     time        = HTime.make(dto.Hour, dto.Minute, dto.Second, dto.Millisecond);
 }
예제 #3
0
        public static HDateTimeRange lastYear(HTimeZone tz)
        {
            var firstOfYear = new DateTime(DateTime.Today.Year, 1, 1).AddYears(-1);

            return(M.Map(new HaystackDateTimeRange(
                             new HaystackDateTime(firstOfYear, M.Map(tz)),
                             new HaystackDateTime(firstOfYear.AddYears(1), M.Map(tz)))));
        }
예제 #4
0
        public static HDateTimeRange lastMonth(HTimeZone tz)
        {
            var firstOfMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(-1);

            return(M.Map(new HaystackDateTimeRange(
                             new HaystackDateTime(firstOfMonth, M.Map(tz)),
                             new HaystackDateTime(firstOfMonth.AddMonths(1), M.Map(tz)))));
        }
예제 #5
0
        public static HDateTimeRange lastWeek(HTimeZone tz)
        {
            var firstOfWeek = DateTime.Today.AddDays(DayOfWeek.Sunday - DateTime.Today.DayOfWeek).AddDays(-7);

            return(M.Map(new HaystackDateTimeRange(
                             new HaystackDateTime(firstOfWeek, M.Map(tz)),
                             new HaystackDateTime(firstOfWeek.AddDays(7), M.Map(tz)))));
        }
예제 #6
0
        /** Make a range which encompasses the previous year. */
        public static HDateTimeRange lastYear(HTimeZone tz)
        {
            HDate today = HDate.today();
            HDate first = HDate.make(today.Year - 1, 1, 1);
            HDate last  = HDate.make(today.Year - 1, 12, 31);

            return(make(first, last, tz));
        }
예제 #7
0
 // Constructor with basic fields
 public static HDateTime make(HDate date, HTime time, HTimeZone tz)
 {
     if (date == null || time == null || tz == null)
     {
         throw new ArgumentException("null args");
     }
     return(new HDateTime(date, time, tz));
 }
예제 #8
0
 // Private constructor
 private HDateTime(HDate date, HTime time, HTimeZone tz)
 {
     this.date   = date;
     this.time   = time;
     TimeZone    = tz;
     m_dtoParsed = new DateTimeOffset(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second, time.Millisecond, tz.dntz.BaseUtcOffset);
     // NOTE: Here offset is fixed - normal path is through other constructor for a parsed ISO string
 }
예제 #9
0
        /** Make a range which encompasses the current month. */
        public static HDateTimeRange thisMonth(HTimeZone tz)
        {
            HDate today = HDate.today();
            HDate first = HDate.make(today.Year, today.Month, 1);
            HDate last  = HDate.make(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));

            return(make(first, last, tz));
        }
예제 #10
0
        /** Make a range which encompasses the current week.
         *  The week is defined as Sunday thru Saturday. */
        public static HDateTimeRange thisWeek(HTimeZone tz)
        {
            HDate today = HDate.today();
            HDate sun   = today.minusDays(today.weekday() - DayOfWeek.Sunday);
            HDate sat   = today.plusDays(DayOfWeek.Saturday - today.weekday());

            return(make(sun, sat, tz));
        }
예제 #11
0
        // Constructor with ticks and DN TimeZone instance
        public static HDateTime make(long ticks, HTimeZone tz)
        {
            // use DateTimeOffset to decode ticks to fields
            DateTimeOffset dt = new DateTimeOffset(ticks, tz.dntz.BaseUtcOffset);

            HDateTime ts = new HDateTime(dt, tz);

            return(ts);
        }
예제 #12
0
        /** Make a range which encompasses the previous week.
         *  The week is defined as Sunday thru Saturday. */
        public static HDateTimeRange lastWeek(HTimeZone tz)
        {
            HDate today = HDate.today();
            HDate prev  = today.minusDays(7);
            HDate sun   = prev.minusDays(prev.weekday() - DayOfWeek.Sunday);
            HDate sat   = prev.plusDays(DayOfWeek.Saturday - prev.weekday());

            return(make(sun, sat, tz));
        }
예제 #13
0
        /** Make a range which encompasses the previous month. */
        public static HDateTimeRange lastMonth(HTimeZone tz)
        {
            HDate    today       = HDate.today();
            DateTime dttoday     = new DateTime(today.Year, today.Month, today.Day);
            DateTime dtLastMonth = dttoday.AddMonths(-1);

            HDate lastMonth = HDate.make(dtLastMonth.Year, dtLastMonth.Month, dtLastMonth.Day);
            HDate first     = HDate.make(lastMonth.Year, lastMonth.Month, 1);
            HDate last      = HDate.make(lastMonth.Year, lastMonth.Month, DateTime.DaysInMonth(lastMonth.Year, lastMonth.Month));

            return(make(first, last, tz));
        }
예제 #14
0
        /**
         * Parse from string using the given timezone as context for
         * date based ranges.  The formats are:
         *  - "today"
         *  - "yesterday"
         *  - "{date}"
         *  - "{date},{date}"
         *  - "{dateTime},{dateTime}"
         *  - "{dateTime}"  // anything after given timestamp
         * Throw ParseException is invalid string format.
         */
        public static HDateTimeRange make(string str, HTimeZone tz)
        {
            // handle keywords
            str = str.Trim();
            if (str.CompareTo("today") == 0)
            {
                return(make(HDate.today(), tz));
            }
            if (str.CompareTo("yesterday") == 0)
            {
                return(make(HDate.today().minusDays(1), tz));
            }

            // parse scalars
            int  comma = str.IndexOf(',');
            HVal start = null, end = null;

            if (comma < 0)
            {
                start = new HZincReader(str).readVal();
            }
            else
            {
                start = new HZincReader(str.Substring(0, comma)).readVal();
                end   = new HZincReader(str.Substring(comma + 1)).readVal();
            }

            // figure out what we parsed for start,end
            if (start is HDate)
            {
                if (end == null)
                {
                    return(make((HDate)start, tz));
                }
                if (end is HDate)
                {
                    return(make((HDate)start, (HDate)end, tz));
                }
            }
            else if (start is HDateTime)
            {
                if (end == null)
                {
                    return(make((HDateTime)start, HDateTime.now(tz)));
                }
                if (end is HDateTime)
                {
                    return(make((HDateTime)start, (HDateTime)end));
                }
            }

            throw new FormatException("Invalid HDateTimeRange: " + str);
        }
예제 #15
0
 // Make for inclusive dates within given timezone
 public static HDateTimeRange make(HDate start, HDate end, HTimeZone tz)
 {
     return(make(start.midnight(tz), end.plusDays(1).midnight(tz)));
 }
예제 #16
0
 // Make for single date within given timezone
 public static HDateTimeRange make(HDate date, HTimeZone tz)
 {
     return(make(date, date, tz));
 }
예제 #17
0
        /// <summary>
        /// Make a HtimeZone by Haystack name
        /// </summary>
        /// <param name="name">Haystack Timezone name</param>
        /// <param name="bChecked">Allow null return or check for error and throw exception</param>
        /// <returns></returns>
        public static HTimeZone make(string name, bool bChecked)
        {
            string strNameToSearch = name;

            if (name.ToUpper().Trim() == "REL") // Known non tzi equivalent - but should be GMT
            {
                strNameToSearch = "GMT";
            }

            TimeZoneInfo tziFound = null;

            try
            {
                string strWindowsTimeZoneID = "";
                string strIANATimeZoneID    = "";
                bool   bFound    = false;
                int    iLength   = TimeZoneConverter.TZConvert.KnownIanaTimeZoneNames.ToArray().Length;
                int    iCurIndex = 0;
                while ((!bFound) && (iCurIndex < iLength))
                {
                    if (TimeZoneConverter.TZConvert.KnownIanaTimeZoneNames.ToArray()[iCurIndex].ToUpper().Contains(strNameToSearch.ToUpper()))
                    {
                        bFound            = true;
                        strIANATimeZoneID = TimeZoneConverter.TZConvert.KnownIanaTimeZoneNames.ToArray()[iCurIndex];
                    }
                    iCurIndex++;
                }
                if (bFound)
                {
                    try
                    {
                        strWindowsTimeZoneID = TimeZoneConverter.TZConvert.IanaToWindows(strIANATimeZoneID);
                        tziFound             = TimeZoneInfo.FindSystemTimeZoneById(strWindowsTimeZoneID);
                    }
                    catch (Exception)
                    {
                        if (bChecked)
                        {
                            // Throw a new exception
                            throw new ArgumentException("Name " + name + " not able to convert from IANA to Windows Time Zone, exception occurred", "name");
                        }
                        else
                        {
                            return((HTimeZone)null);
                        }
                    }
                    HTimeZone tzReturn = new HTimeZone(name, tziFound);
                    return(tzReturn);
                }
                else
                {
                    if (bChecked)
                    {
                        throw new ArgumentException("Name " + name + " not found as valid IANA timezone", "name");
                    }
                    else
                    {
                        return((HTimeZone)null);
                    }
                }
            }
            catch (Exception)
            {
                if (bChecked)
                {
                    throw new ArgumentException("Name " + name + " not able to convert from IANA to Windows Time Zone, exception occurred", "name");
                }
                else
                {
                    return((HTimeZone)null);
                }
            }

            /* Unreachable
             * if (bChecked)
             * {
             *  throw new ArgumentException("Name " + name + " not able to convert from IANA to Windows Time Zone, exception occurred", "name");
             * }
             * else
             * {
             *  return (HTimeZone)null;
             * }*/
        }
 public static HDateTime make(HDate date, HTime time, HTimeZone tz) => M.Map(new HaystackDateTime(M.Map(date), M.Map(time), M.Map(tz)));
예제 #19
0
 // Convert this date into HDateTime for midnight in given timezone.
 public HDateTime midnight(HTimeZone tz)
 {
     return(HDateTime.make(this, HTime.MIDNIGHT, tz));
 }
예제 #20
0
 public HDateTime midnight(HTimeZone tz) => M.Map(new HaystackDateTime(Source.Value, M.Map(tz)));
예제 #21
0
 public static HDateTimeRange make(HDate start, HDate end, HTimeZone tz)
 => M.Map(new HaystackDateTimeRange(new HaystackDateTime(start.Source.Value, M.Map(tz)), new HaystackDateTime(end.Source.Value, M.Map(tz))));
예제 #22
0
 // Get HDateTime for given timezone
 public static HDateTime now(HTimeZone tz)
 {
     return(make(DateTime.Now.Ticks, tz));
 }
예제 #23
0
        /* Parse from string ISO8601 excluding W specifier:
         * fomats accepted are - "yyyy-MM-dd'T'HH:mm:ss.FFFtzooffsetname"   - Year Month Date Hour minute second and non zero miliseconds then the
         *                                                                  Zone offset with name
         *                     - "yyyy-MM-dd'T'HH:mm:ss.FFFZ UTC"           - Year Month Date Hour minute second and non zero miliseconds then the
         *                                                                  Zone offset as Z specifier
         *                     - "yyyy-MM-dd'T'HH:mm:sstzoffsetname"        - Same but without millisecond - assume millisecond is zero
         *                     - "yyyy-MM-dd'T'HH:mm:ssZ UTC"               - Same but without millisecond - assume millisecond is zero (offset as Z specifier)
         *                     - "yyyy-MM-dd'T'HH:mm:ss"                    - Same but without millisecond and offset
         * or raise ParseException
         */
        /* Haven't dealt with this format - need to find correct method to meet Haystack specificaiton here */
        public static HDateTime make(string s, bool bException)
        {
            // Tested 17.06.2018 with 2018-06-17T13:00:00.123+10:00 Melbourne
            string sCurrent        = s;
            bool   bUTC            = false;
            bool   bNoZoneOffset   = false;
            string strDateTimeOnly = "";
            string strDateTimeOffsetFormatSpecifier = "";
            string strHTimeZone = "";
            int    iPosOfLastSecondChar, iPosOfSpaceBeforeTZID = 0;

            DateTimeOffset dto;
            TimeSpan       tsOffset = new TimeSpan();

            if (sCurrent.Contains('W'))
            {
                if (bException)
                {
                    throw new FormatException("Invalid DateTime format for string " + s + " ISO8601 W specifier not supported by this toolkit");
                }
                else
                {
                    return(null);
                }
            }
            if (sCurrent.Trim().Contains("Z UTC"))
            {
                bUTC = true;
                int iPos = sCurrent.IndexOf('Z');
                iPosOfLastSecondChar = iPos - 1;
                strDateTimeOnly      = sCurrent.Substring(0, iPos);
            }
            else if (sCurrent.Trim().Contains("Z"))
            {
                bUTC = true;
                int iPos = sCurrent.IndexOf('Z');
                iPosOfLastSecondChar = iPos - 1;
                strDateTimeOnly      = sCurrent.Substring(0, iPos);
            }
            if (!sCurrent.Contains('T'))
            {
                // Only possible in ISO 8601 with just a date - this is not an allowed case for HDateTime
                if (bException)
                {
                    throw new FormatException("Invalid DateTime format for string " + s + " missing ISO8601 T specifier");
                }
                else
                {
                    return(null);
                }
            }
            // if it is offset with name then it must have a + or - sign for the offset after the T
            int iPosOfT = sCurrent.IndexOf('T');

            if (iPosOfT + 1 >= sCurrent.Length)
            {
                // Nothing after 'T' this is not legall
                if (bException)
                {
                    throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier");
                }
                else
                {
                    return(null);
                }
            }

            // Stip of the timezone by finding the space
            iPosOfSpaceBeforeTZID = sCurrent.IndexOf(' ', iPosOfT);
            int    iEndLen = iPosOfSpaceBeforeTZID - (iPosOfT + 1);
            string sEnd    = sCurrent.Substring(iPosOfT + 1, iEndLen);

            if (!bUTC)
            {
                if ((sEnd.Trim().Contains('+')) || (sEnd.Trim().Contains('-')))
                {
                    bool bPositive = false;
                    int  iPosSign  = 0;
                    // In ISO 8601 this is either a +/-hh:mm or +/-hhmm or +/-hh
                    // See how many characters there is till space - that is the offset specifier
                    if (sEnd.Trim().Contains('+'))
                    {
                        bPositive = true;
                        iPosSign  = sCurrent.IndexOf('+', iPosOfT);
                    }
                    else
                    {
                        iPosSign = sCurrent.IndexOf('-', iPosOfT);
                    }
                    iPosOfLastSecondChar = iPosSign - 1;
                    strDateTimeOnly      = sCurrent.Substring(0, iPosSign);
                    // Find the next space - requires it contains a Haystack Zone specifier if not UTC
                    int iPosSpace = sCurrent.Trim().IndexOf(' ', iPosSign);
                    if ((iPosSpace < iPosSign) || (iPosSpace < 0))
                    {
                        if (bException)
                        {
                            throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier");
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    // What is the number of characters between the sign and the space - 5 4 or 2
                    iPosOfSpaceBeforeTZID = iPosSpace;
                    int    iNumOffsetChars = iPosSpace - iPosSign - 1;
                    string strOffset       = sCurrent.Substring(iPosSign + 1, iNumOffsetChars);
                    if (iNumOffsetChars == 5)
                    {
                        // Assume +/-hh:mm
                        string[] strHM = strOffset.Split(':');
                        if (strHM.Length != 2)
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }

                        int iHour;
                        if (!int.TryParse(strHM[0], out iHour))
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if (iHour < 0)
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }

                        int iMinute;
                        if (!int.TryParse(strHM[1], out iMinute))
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if ((iMinute < 0) || (iMinute > 60))
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if (!bPositive)
                        {
                            // Problem: if minute is non-zero we will get undesired result e.g. -10:10 will return -9:50 therefore both must be negative
                            iMinute = iMinute * -1;
                            iHour   = iHour * -1;
                        }
                        tsOffset = new TimeSpan(iHour, iMinute, 0);
                    }
                    else if (iNumOffsetChars == 4)
                    {
                        int iHour;
                        // Assume hhmm
                        if (!Int32.TryParse(strOffset.Substring(0, 2), out iHour))
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if (iHour < 0)
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }

                        int iMinute;
                        if (!int.TryParse(strOffset.Substring(2, 2), out iMinute))
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if ((iMinute < 0) || (iMinute > 60))
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if (!bPositive)
                        {
                            // Problem: if minute is non-zero we will get undesired result e.g. -10:10 will return -9:50 therefore both must be negative
                            iMinute = iMinute * -1;
                            iHour   = iHour * -1;
                        }
                        tsOffset = new TimeSpan(iHour, iMinute, 0);
                    }
                    else if (iNumOffsetChars == 2)
                    {
                        // Assume hh
                        int iHour;
                        // Assume hhmm
                        if (!int.TryParse(strOffset, out iHour))
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if (iHour < 0)
                        {
                            // Invalid offset
                            if (bException)
                            {
                                throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                            }
                            else
                            {
                                return(null);
                            }
                        }
                        if (!bPositive)
                        {
                            iHour = iHour * -1;
                        }
                        tsOffset = new TimeSpan(iHour, 0, 0);
                    }
                    else
                    {
                        // Invalid offset
                        if (bException)
                        {
                            throw new FormatException("Invalid DateTime format for string " + s + " missing suitable length string after ISO8601 T specifier for offset");
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                else
                {
                    // Must not contain a Zone offset string
                    bNoZoneOffset         = true;
                    iPosOfSpaceBeforeTZID = sCurrent.IndexOf(' ', iPosOfT);
                    strDateTimeOnly       = sCurrent.Substring(0, iPosOfSpaceBeforeTZID);
                }
            }
            // Get the Haystack time zone identifier and create the HTimeZone object.
            if ((iPosOfSpaceBeforeTZID < sCurrent.Length) && (sCurrent.Length - iPosOfSpaceBeforeTZID > 2) && (!bUTC))
            {
                strHTimeZone = sCurrent.Substring(iPosOfSpaceBeforeTZID + 1);
            }
            // Check for the 'T' for the Formats that include that
            // Check for milliseconds
            if (strDateTimeOnly.Trim().Contains("."))
            {
                // All fields with 'T'
                strDateTimeOffsetFormatSpecifier = "yyyy-MM-dd'T'HH:mm:ss.FFF";
            }
            else
            {
                // no milliseconds
                // DateTimeOffset will adopt 0 milliseconds
                strDateTimeOffsetFormatSpecifier = "yyyy-MM-dd'T'HH:mm:ss";
            }
            try
            {
                DateTime dt = DateTime.ParseExact(strDateTimeOnly, strDateTimeOffsetFormatSpecifier,
                                                  CultureInfo.InvariantCulture);
                if (!bNoZoneOffset)
                {
                    dto = new DateTimeOffset(dt, tsOffset);
                }
                else if (bUTC)
                {
                    DateTime dtutc = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
                    dto = new DateTimeOffset(dtutc);
                }
                else
                {
                    dto = new DateTimeOffset(dt);
                }
            }
            catch (Exception)
            {
                if (bException)
                {
                    throw new FormatException("Invalid DateTime format for string " + s);
                }
                else
                {
                    return(null);
                }
            }
            HTimeZone htz;

            if (bUTC)
            {
                htz = HTimeZone.UTC;
            }
            else
            {
                try
                {
                    htz = HTimeZone.make(strHTimeZone, true);
                }
                catch (Exception genexcep)
                {
                    if (bException)
                    {
                        throw new FormatException("Invalid DateTime format for string " + s + " for Timezone [" + genexcep.Message + "]");
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            return(new HDateTime(dto, htz));
        }
 public static HDateTime make(long ticks, HTimeZone tz)
 => M.Map(new HaystackDateTime(new DateTime(ticks), M.Map(tz)));
 public static HDateTime now(HTimeZone tz) => M.Map(new HaystackDateTime(DateTime.Now, M.Map(tz)));
예제 #26
0
 // Constructor with date and time (to sec) fields
 public static HDateTime make(int year, int month, int day, int hour, int min, int sec, HTimeZone tz)
 {
     return(make(HDate.make(year, month, day), HTime.make(hour, min, sec), tz));
 }
 public static HDateTime make(int year, int month, int day, int hour, int min, HTimeZone tz)
 => M.Map(new HaystackDateTime(new DateTime(year, month, day, hour, min, 0), M.Map(tz)));
예제 #28
0
 public static HaystackTimeZone Map(HTimeZone value)
 {
     return(value.Source);
 }
예제 #29
0
 public static HDateTimeRange make(HDate date, HTimeZone tz)
 => M.Map(new HaystackDateTimeRange(new HaystackDateTime(date.Source.Value, M.Map(tz)), new HaystackDateTime(date.Source.Value.AddDays(1), M.Map(tz))));