From 92e86cff2b913c4b690596c7f344c43146246417 Mon Sep 17 00:00:00 2001 From: Tobias Boege Date: Sat, 5 Jan 2019 13:10:53 +0100 Subject: [PATCH 1/4] Allow to retrieve timezone from Date.FromRFC822() [GB.UTIL] * NEW: Allow to retrieve timezone from Date.FromRFC822(). --- comp/src/gb.util/.component | 2 +- comp/src/gb.util/.project | 2 +- comp/src/gb.util/.src/Date.module | 5 +++-- comp/src/gb.util/.src/MMain.module | 24 ++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/comp/src/gb.util/.component b/comp/src/gb.util/.component index 0e8770d52..a711f5d56 100644 --- a/comp/src/gb.util/.component +++ b/comp/src/gb.util/.component @@ -1,4 +1,4 @@ [Component] Key=gb.util -Version=3.12.0 +Version=3.12.90 State=1 diff --git a/comp/src/gb.util/.project b/comp/src/gb.util/.project index 8f8258f75..f55263f0c 100644 --- a/comp/src/gb.util/.project +++ b/comp/src/gb.util/.project @@ -1,7 +1,7 @@ # Gambas Project File 3.0 Title=Gambas utilities Startup=MMain -Version=3.12.0 +Version=3.12.90 VersionFile=1 TabSize=2 Language=fr diff --git a/comp/src/gb.util/.src/Date.module b/comp/src/gb.util/.src/Date.module index 43dd7d76e..c170860ed 100644 --- a/comp/src/gb.util/.src/Date.module +++ b/comp/src/gb.util/.src/Date.module @@ -95,12 +95,12 @@ End Public Sub ToRFC822({Date} As Date, Optional TimeZone As String = "GMT") As String InitDaysMonths - {Date} += System.TimeZone / 86400 + GetRFC822Zone(TimeZone) + {Date} = ToUTC({Date}) + GetRFC822Zone(TimeZone) Return $aDay[WeekDay({Date})] & ", " & Format(Day({Date}), "00") & " " & $aMonth[Month({Date}) - 1] & " " & Year({Date}) & " " & Format(Hour({Date}), "00") & ":" & Format(Minute({Date}), "00") & ":" & Format(Second({Date}), "00") & " " & TimeZone End -Public Sub FromRFC822(Value As String) As Date +Public Sub FromRFC822(Value As String, Optional ByRef TimeZone As String) As Date Dim aDate As String[] Dim dDate As Date @@ -139,6 +139,7 @@ Public Sub FromRFC822(Value As String) As Date dDate -= Frac(Date(Now)) dDate += GetRFC822Zone(aDate[6]) + TimeZone = aDate[6] Return dDate Catch diff --git a/comp/src/gb.util/.src/MMain.module b/comp/src/gb.util/.src/MMain.module index b0a15b1d0..0cbc2d0f0 100644 --- a/comp/src/gb.util/.src/MMain.module +++ b/comp/src/gb.util/.src/MMain.module @@ -19,5 +19,29 @@ Public Sub Main() Print CStr(CDate("1/1/1970")) + RFC822Test() + +End + +Public Sub RFC822Test() + + Dim sTimeZone As String + + ' Timezone to-from conversion should be the identity mapping + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0000"), "+0000") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0000"), "+0100") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0000"), "+0800") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0100"), "+0000") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0100"), "+0100") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0100"), "+0800") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0800"), "+0000") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0800"), "+0100") + Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0800"), "+0800") + ' Ability to extract timezone + Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0100", ByRef sTimeZone) + Print sTimeZone + ' 21 Apr 2019 is not a Wednesday + Try Date.FromRFC822("Wed, 21 Apr 2019 05:00:00 +0100") + If Error Then Print Error.Text End From f11054de74962ca9fd04fed298ea7530c17efe52 Mon Sep 17 00:00:00 2001 From: Tobias Boege Date: Sat, 5 Jan 2019 14:48:46 +0100 Subject: [PATCH 2/4] Propgate internal errors from Date.FromRFC822() [GB.UTIL] * NEW: Propagte internal errors from Date.FromRFC822() instead of shadowing them with a Catch block. --- comp/src/gb.util/.src/Date.module | 1 + 1 file changed, 1 insertion(+) diff --git a/comp/src/gb.util/.src/Date.module b/comp/src/gb.util/.src/Date.module index c170860ed..5a70d748f 100644 --- a/comp/src/gb.util/.src/Date.module +++ b/comp/src/gb.util/.src/Date.module @@ -144,6 +144,7 @@ Public Sub FromRFC822(Value As String, Optional ByRef TimeZone As String) As Dat Catch + If Error.Class.Name = "Date" Then Error.Propagate Error.Raise("Not a RFC822 date format") End From 38ca01684e26a2b808701ab7063000936133427e Mon Sep 17 00:00:00 2001 From: Tobias Boege Date: Sat, 5 Jan 2019 15:50:06 +0100 Subject: [PATCH 3/4] Improve error message from Date.GetRFC822Zone() [GB.UTIL] * OPT: Improve error message from Date.GetRFC822Zone() by showing what couldn't be recognized as a timezone. --- comp/src/gb.util/.src/Date.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comp/src/gb.util/.src/Date.module b/comp/src/gb.util/.src/Date.module index 5a70d748f..bedb1627d 100644 --- a/comp/src/gb.util/.src/Date.module +++ b/comp/src/gb.util/.src/Date.module @@ -83,7 +83,7 @@ Private Sub GetRFC822Zone(sZone As String) As Float Case Like "[+-][0-1][0-9][0-5][0-9]" fZone = CInt(Left(sZone, 3)) + CInt(Mid$(sZone, 4)) / 60 Case Else - Error.Raise("Unknown timezone") + Error.Raise(Subst$("Unknown timezone '&1'", sZone)) End Select Return fZone / 24 From 3a16b32addb14b8ae1bf5df3359180fc3fce0f58 Mon Sep 17 00:00:00 2001 From: Tobias Boege Date: Sat, 5 Jan 2019 17:24:52 +0100 Subject: [PATCH 4/4] Fix timezone bug in Date.FromRFC822() [GB.UTIL] * NEW: Add Date.FromUTC() as inverse to Date.ToUTC(). * BUG: Fix timezone bug in Date.FromRFC822() and pass all tests. See the thread "gb.web.feed not stable yet?" [1] for explanations of the bug and fix. [1] https://lists.gambas-basic.org/pipermail/user/2018-December/066103.html --- comp/src/gb.util/.src/Date.module | 16 ++++++++++------ comp/src/gb.util/.src/MMain.module | 11 +++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/comp/src/gb.util/.src/Date.module b/comp/src/gb.util/.src/Date.module index bedb1627d..ee0718539 100644 --- a/comp/src/gb.util/.src/Date.module +++ b/comp/src/gb.util/.src/Date.module @@ -51,10 +51,15 @@ Private Sub InitDaysMonths() End Public Sub ToUTC({Date} As Date) As Date - - {Date} += System.TimeZone / 86400 - Return {Date} - + + Return {Date} + System.TimeZone / 86400 + +End + +Public Sub FromUTC({Date} As Date) As Date + + Return {Date} - System.TimeZone / 86400 + End Private Sub GetRFC822Zone(sZone As String) As Float @@ -136,8 +141,7 @@ Public Sub FromRFC822(Value As String, Optional ByRef TimeZone As String) As Dat dDate = Date(iYear, $aMonth.Find(aDate[1]) + 1, CInt(aDate[0]), CInt(aDate[3]), CInt(aDate[4]), CInt(aDate[5])) If iWeekDay >= 0 And If WeekDay(dDate) <> iWeekDay Then Error.Raise("Incorrect week day") - dDate -= Frac(Date(Now)) - dDate += GetRFC822Zone(aDate[6]) + dDate = FromUTC(dDate) - GetRFC822Zone(aDate[6]) TimeZone = aDate[6] Return dDate diff --git a/comp/src/gb.util/.src/MMain.module b/comp/src/gb.util/.src/MMain.module index 0cbc2d0f0..b8191d169 100644 --- a/comp/src/gb.util/.src/MMain.module +++ b/comp/src/gb.util/.src/MMain.module @@ -27,6 +27,16 @@ Public Sub RFC822Test() Dim sTimeZone As String + ' Must be correct in the local timezone + Print CStr(Now) + Print Format(Now) + Print CStr(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0000")) + Print Format$(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0000")) + Print Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0100") + Print Date.FromRFC822("Tue, 1 Jan 2019 00:00:00 +0800") + Print Date.ToRFC822(Now) + Print Date.ToRFC822(Now, "+0100") + Print "---" ' Timezone to-from conversion should be the identity mapping Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0000"), "+0000") Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0000"), "+0100") @@ -37,6 +47,7 @@ Public Sub RFC822Test() Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0800"), "+0000") Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0800"), "+0100") Print Date.ToRFC822(Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0800"), "+0800") + Print "---" ' Ability to extract timezone Date.FromRFC822("Sun, 21 Apr 2019 05:00:00 +0100", ByRef sTimeZone) Print sTimeZone