Announcement

Collapse
No announcement yet.

Email UTC Header to Local Date String

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    Originally posted by Gary Beene View Post
    Well, Stuart, now that I think about what you said, the incoming email header date string typically has an alpha description of the time zone, such as this ...

    Code:
    Date: Tue, 19 Jun 2018 09:05:10 -0400 (EDT)
    ... where I need to trim off the "(EDT)". Is that what you meant?
    That's what is defined as a "comment" in the RFC. You can safely ignore it.
    --
    [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
    PNG Domain Hosting[/URL]

    Comment


    • #22
      Originally posted by Dale Yarker View Post
      If the date/time string contains "GMT", the offset is zero, so it (the offset) may not be there.
      That hasn't been true since RFC 822 was obsoleted by RFC 2822. in 2001
      --
      [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
      PNG Domain Hosting[/URL]

      Comment


      • #23
        Which? "...the offset is zero..." or "... may not be there" is untrue?
        Dale

        Comment


        • #24
          Originally posted by Dale Yarker View Post
          Which? "...the offset is zero..." or "... may not be there" is untrue?
          "may not be there".

          RFC2822:


          "In some of the definitions, there will be nonterminals whose names start with "obs-". These "obs-" elements refer to tokens defined in the obsolete syntax in section 4. In all cases, these productions are to be ignored for the purposes of generating legal Internet messages and MUST NOT be used as part of such a message. However, when interpreting messages, these tokens MUST be honored as part of the legal syntax. In this sense, section 3 defines a grammar for generation of messages, with "obs-" elements that are to be ignored, while section 4 adds grammar for interpretation of messages."

          Para 3.3:

          ... zone = (( "+" / "-" ) 4DIGIT) / obs-zone ...

          So the 4digit offset (plus sign) must be there.
          --
          [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
          PNG Domain Hosting[/URL]

          Comment


          • #25
            Then you brought it (GMT, UT, EDT, etc) up in post 4 just to be ignored. Got it. TA
            Dale

            Comment


            • #26
              Originally posted by Dale Yarker View Post
              Then you brought it (GMT, UT, EDT, etc) up in post 4 just to be ignored. Got it. TA
              However, when interpreting messages, these tokens MUST be honored as part of the legal syntax. In this sense, section 3 defines a grammar for generation of messages, with "obs-" elements that are to be ignored, while section 4 adds grammar for interpretation of messages."
              --
              [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
              PNG Domain Hosting[/URL]

              Comment


              • #27
                Stuart,
                Talking of date variations ... today, I received an email from Yahoo that had NO date in the email header.

                By No date in the email header, I mean no line that starts with "Date:". I didn't realize that was optional, but apparently it is.

                In such a case how could the receiving email client interpret the email date? The time it was received, perhaps?


                Comment


                • #28
                  ---

                  Comment


                  • #29
                    Another simple one using APIs, beside many invalid date checking made by the APIs, some more could of course be added.

                    Click image for larger version  Name:	MailTime01.png Views:	1 Size:	15.4 KB ID:	773171

                    Code:
                    #COMPILE EXE '#Win 9.074#
                    #DIM ALL
                    #REGISTER NONE
                    #INCLUDE "Win32Api.inc"
                    '#RESOURCE ".pbr"
                    
                    GLOBAL hDlg AS DWORD
                    
                    $AppName = "Mail local time conversion"
                    %Edit01  = 101
                    %Edit02  = 102
                    '_____________________________________________________________________________
                    
                    CALLBACK FUNCTION DlgProc
                     LOCAL st              AS SYSTEMTIME
                     LOCAL ft              AS FILETIME
                     LOCAL qft             AS QUAD 'Aka FILETIME
                     LOCAL index           AS LONG
                     LOCAL sDate           AS STRING
                     LOCAL zDate           AS ASCIIZ * 50
                     LOCAL zTime           AS ASCIIZ * 15
                     DIM   Element(1 TO 7) AS STRING
                    
                     SELECT CASE CBMSG
                    
                       CASE %WM_INITDIALOG
                         PostMessage(GetDlgItem(hDlg, %Edit01), %EM_SETSEL, -1, 0) 'Set caret at the end of text
                         PostMessage(hDlg, %WM_COMMAND, MAK(DWORD, %Edit01, %EN_CHANGE), 0) 'Init second edit box
                    
                       CASE %WM_COMMAND
                         SELECT CASE CBCTL
                           CASE %Edit01
                             IF HIWRD(CBWPARAM) = %EN_CHANGE THEN
                               CONTROL GET TEXT hDlg, %Edit01 TO sDate
                               PARSE MID$(sDate, INSTR(sDate, ANY "0123456789")), Element(), ANY ": " & $TAB 'Get time elements
                               sDate = "?" 'This will be overwritten if no error occur
                               st.wMonth = INSTR("janfevmaraprmayjunjulaugsepoctnovdec", LCASE$(Element(2)))
                               IF st.wMonth > 0 AND LEN(Element(2)) = 3 THEN 'Validate month string
                                 st.wYear   = VAL(Element(3))
                                 st.wMonth  = 1 + st.wMonth \ 3
                                 st.wDay    = VAL(Element(1))
                                 st.wHour   = VAL(Element(4))
                                 st.wMinute = VAL(Element(5))
                                 st.wSecond = VAL(Element(6))
                                 IF SystemTimeToFileTime(st, BYVAL VARPTR(qft)) THEN 'Convert mail time to FileTime
                                   qft -= ((VAL(Element(7)) \ 100) * 60 + VAL(Element(7)) MOD 100) * 600000000 'Ajust TZ offset nanoseconds to UTC
                                   FileTimeToLocalFileTime(BYVAL VARPTR(qft), ft) 'Ajust to local time from UTC according to local TZ
                                   FileTimeToSystemTime(ft, st) 'Final local SystemTime
                                   IF GetDateFormat(0, 0, st, "ddd, d MMM yyyy", zDate, SIZEOF(zDate)) THEN 'Format date
                                     GetTimeFormat(0, 0, st, "HH':'mm':'ss", zTime, SIZEOF(zTime))    'Format time
                                     sDate = "Date: " & zDate & $SPC & zTime & " (Local time)"        'Set result
                                   END IF
                                 END IF
                               END IF
                               CONTROL SET TEXT hDlg, %Edit02, sDate 'Return result
                             END IF
                         END SELECT
                    
                      END SELECT
                    
                    END FUNCTION
                    '_____________________________________________________________________________
                    
                    FUNCTION PBMAIN() AS LONG
                     LOCAL hIcon AS DWORD
                    
                     DIALOG FONT "Segoe UI", 9
                     DIALOG NEW %HWND_DESKTOP, $AppName, , , 200, 60, _
                     %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SYSMENU, 0 TO hDlg
                    
                     hIcon = ExtractIcon(GetModuleHandle(""), "%SystemRoot%\system32\PowrProf.dll", 1)
                     SetClassLong(hDlg, %GCL_HICONSM, hIcon)
                    
                     CONTROL ADD TEXTBOX, hDlg, %Edit01, "Date: Tue, 19 Jun 2018 13:30:10 -0400 (EDT)", 15, 15, 170, 12
                    
                     CONTROL ADD TEXTBOX, hDlg, %Edit02, "", 15, 30, 170, 12, %ES_READONLY OR %WS_TABSTOP, %WS_EX_CLIENTEDGE
                    
                     DIALOG SHOW MODAL hDlg CALL DlgProc
                     DestroyIcon(hIcon)
                    
                    END FUNCTION
                    '_____________________________________________________________________________
                    '

                    Comment


                    • #30
                      Originally posted by Gary Beene View Post
                      Stuart,
                      Talking of date variations ... today, I received an email from Yahoo that had NO date in the email header.

                      By No date in the email header, I mean no line that starts with "Date:". I didn't realize that was optional, but apparently it is.

                      In such a case how could the receiving email client interpret the email date? The time it was received, perhaps?

                      It's not optional, Date: is one of the two required header fields for an email message.

                      RFC5322 Para 3.6:
                      "
                      The only required header fields are the origination date field and the originator address field(s). All other header fields are syntactically optional. More information is contained in the table following this definition. "
                      I'm surprised that Yahoo did that. Many email services, including Yahoo!, reject such messages with a message such as "554 Message is not RFC compliant" after the DATA has been sent. ( Sure it wasn't spam configured to look like it came from Yahoo? )
                      --
                      [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                      PNG Domain Hosting[/URL]

                      Comment


                      • #31
                        Just to give you something extra to think about. Here's an example from RFC 5322 of a perfectly valid email. Note the missing seconds and the multiple spaces and CRLFs in the Date: field. (You actually need to "unfold" the date: field before parsing it.)

                        Code:
                         White space, including folding white space, and comments can be
                           inserted between many of the tokens of fields.  Taking the example
                           from A.1.3, white space and comments can be inserted into all of the
                           fields.
                        
                           ----
                           From: Pete(A nice \) chap) <pete(his account)@silly.test(his host)>
                           To:A Group(Some people)
                                :Chris Jones <c@(Chris's host.)public.example>,
                                    joe@example.org,
                             John <jdoe@one.test> (my dear friend); (the end of the group)
                           Cc:(Empty list)(start)Hidden recipients  :(nobody(that I know))  ;
                           Date: Thu,
                                 13
                                   Feb
                                     1969
                                 23:32
                                          -0330 (Newfoundland Time)
                           Message-ID:              <testabcd.1234@silly.test>
                        
                           Testing.
                           ----
                        
                           The above example is aesthetically displeasing, but perfectly legal.
                           Note particularly (1) the comments in the "From:" field (including
                           one that has a ")" character appearing as part of a quoted-pair); (2)
                           the white space absent after the ":" in the "To:" field as well as
                           the comment and folding white space after the group name, the special
                           character (".") in the comment in Chris Jones's address, and the
                           folding white space before and after "joe@example.org,"; (3) the
                           multiple and nested comments in the "Cc:" field as well as the
                           comment immediately following the ":" after "Cc"; (4) the folding
                           white space (but no comments except at the end) and the missing
                           seconds in the time of the date field; and (5) the white space before
                           (but not within) the identifier in the "Message-ID:" field.
                        --
                        [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                        PNG Domain Hosting[/URL]

                        Comment


                        • #32
                          Hi Stuart,
                          Well, the email came from Yahoo recently when I created the email account. The timing was appropriate so probably not spam.

                          I did run across an example of a folded header - multiple lines as in your example, so I had to unfold it as you mention.

                          Comment

                          Working...
                          X