Example #1
0
        public static string CreateDateTimeString(Calendar cal, bool parseDate, bool parseTime)
        {
            var datetimestr = new StringBuilder();

            if (parseDate)
            {
                AppendInt(datetimestr, cal.get(Calendar.YEAR), 4, true, "");
                AppendInt(datetimestr, cal.get(Calendar.MONTH), 2, false, "-");
                AppendInt(datetimestr, cal.get(Calendar.DAY_OF_MONTH), 2, false, "-");
                if (parseTime)
                {
                    datetimestr.Append('T');
                }
            }
            if (parseTime)
            {
                AppendInt(datetimestr, cal.get(Calendar.HOUR_OF_DAY), 2, false, "");
                AppendInt(datetimestr, cal.get(Calendar.MINUTE), 2, false, ":");
                AppendInt(datetimestr, cal.get(Calendar.SECOND), 2, false, ":");
                if (cal.get(Calendar.MILLISECOND) > 0)
                {
                    AppendInt(datetimestr, cal.get(Calendar.MILLISECOND), 3, false, ".");
                }
            }

            TimeZone timezone   = cal.TimeZone;
            int      zoneoffset = cal.get(Calendar.ZONE_OFFSET);

            if (timezone.inDaylightTime(cal.Time))
            {
                zoneoffset += cal.get(Calendar.DST_OFFSET);
            }
            if (zoneoffset == 0)
            {
                datetimestr.Append('Z');
            }
            else
            {
                if (zoneoffset < 0)
                {
                    datetimestr.Append('-');
                    zoneoffset = -zoneoffset;
                }
                else
                {
                    datetimestr.Append('+');
                }
                int min  = zoneoffset / (60 * 1000);
                int hour = zoneoffset / (60 * 60 * 1000);
                min -= hour * 60;
                AppendInt(datetimestr, hour, 2, false, "");
                AppendInt(datetimestr, min, 2, false, ":");
            }
            return(datetimestr.ToString());
        }
Example #2
0
        public static Calendar ParseDateTime(string parsedstring, bool parseDate, bool parseTime)
        {
            if (parsedstring == null)
            {
                return(null);
            }
            int offset = 0;
            int len = parsedstring.Length;
            int year = 0, month = 0, day = 0;

            if (parseDate)
            {
                offset += SkipOneCharacter(parsedstring, offset, '-', true);

                year   = ParseInt(parsedstring, offset, 4);
                offset = offset + 4;

                offset += SkipOneCharacter(parsedstring, offset, '-', false);

                month  = ParseInt(parsedstring, offset, 2);
                offset = offset + 2;

                offset += SkipOneCharacter(parsedstring, offset, '-', false);

                day    = ParseInt(parsedstring, offset, 2);
                offset = offset + 2;

                if (parseTime)
                {
                    offset += SkipOneCharacter(parsedstring, offset, 'T', false);
                }
            }

            int    hour = 0, min = 0, sec = 0, msec = 0;
            string timezone = "GMT";

            if (parseTime)
            {
                hour   = ParseInt(parsedstring, offset, 2);
                offset = offset + 2;

                offset += SkipOneCharacter(parsedstring, offset, ':', false);

                min    = ParseInt(parsedstring, offset, 2);
                offset = offset + 2;

                offset += SkipOneCharacter(parsedstring, offset, ':', false);

                sec    = ParseInt(parsedstring, offset, 2);
                offset = offset + 2;

                if (offset < len && parsedstring[offset] == '.')
                {
                    offset++;
                    var digits = new StringBuilder();

                    for (; offset < len; offset++)
                    {
                        char c = parsedstring[offset];
                        if (char.IsDigit(c))
                        {
                            digits.Append(c);
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (digits.Length > 0)
                    {
                        msec = Convert.ToInt32(digits.ToString());
                    }
                    else
                    {
                        throw new Indeterminate(Indeterminate.IndeterminateSyntaxError);
                    }
                }
                timezone = ParseTimeZone(parsedstring, offset);
            }

            Calendar cal = Calendar.GetInstance(TimeZone.getTimeZone(timezone));

            cal.set(year, parseDate ? month : 0, day, hour, min, sec);
            cal.set(Calendar.MILLISECOND, msec);
            return(cal);
        }
Example #3
0
 public void SetUtc()
 {
     this._timeZone = new TimeZone(TimeZoneInfo.Utc);
     this._date     = DateTime.UtcNow;
 }
Example #4
0
 public static Calendar GetInstance(TimeZone timeZone)
 {
     return(new Calendar(timeZone));
 }