1

For example,

import parsedatetime p = parsedatetime.Calendar() p.parse("on December 15th")

will return

(time.struct_time(tm_year=2020, tm_mon=12, tm_mday=15, tm_hour=13, tm_min=41, tm_sec=47, tm_wday=5, tm_yday=11, tm_isdst=0), 1)

I'm mainly wondering what tm_wday, tm_yday, tm_isdst, and the final number behind the comma (in this case 1 mean).

Hojung Kim
  • 143
  • 1
  • 2
  • 13

1 Answers1

1

Those fields of time.struct_time simply mean:

  • tm_wday: the day of the week (Monday is 0)
  • tm_yday: the day of the year (1st of January is 1)
  • tm_isdst: whether daylight saving time (DST) is in effect (1), not in effect (0) or unknown (-1)

See the documentation for details.

The parse method of the Calendar class of parsedatetime returns a tuple of 2 elements: a struct_time and a parse status (the 1 after the comma, in your case). The status tells you whether parse found:

  • 0: nothing
  • 1: a date
  • 2: a time
  • 3: a datetime

See the documentation for details.

Well, OK, the documentation is somewhat incomplete, so I had to find out the rest. Here it is:

Calendar.parse() has an optional second parameter, a struct_time which it calls sourceTime, that is used as a starting point for the returned struct_time. If the sourceTime is not provided, it is constructed from the time of the call.

The “funny” part is, only some fields of the sourceTime are changed by the parse method, and which fields are changed depends on the parse status. And, most confusingly, tm_wday, tm_yday, tm_isdst are never changed!

If the parse status is

  • 0, no field is changed
  • 1, only tm_year, tm_mon and tm_mday are changed
  • 2, only tm_hour, tm_min and tm_sec are changed
  • 3, only tm_year, tm_mon, tm_mday, tm_hour, tm_min and tm_sec are changed

So by your output, I can see that you made the call on Saturday January 11 at 1:41:47 pm.

Walter Tross
  • 12,237
  • 2
  • 40
  • 64
  • Got it, got it – but now I'm confused why I got: `tm_wday=5, tm_yday=11` when December 15th is a Tuesday, and definitely not the 11th day of the month? – Hojung Kim Jan 11 '20 at 23:47
  • 1
    Interesting question, I will have to dive deeper into that. – Walter Tross Jan 11 '20 at 23:55
  • 1
    OK, I found out... Quite weird! No wonder people usually extract the meaningful values from the `struct_time` and leave the rest alone... – Walter Tross Jan 12 '20 at 00:36
  • Interesting! Is there a function that would separately have a better way to return the day of the week from the main info from `parsedatetime`? – Hojung Kim Jan 13 '20 at 02:20
  • Also, wondered if you happened to have any insights into this as well, a separate question about `parsedatetime` I posted on SO: https://stackoverflow.com/questions/59708073/after-using-parsedatetime-to-get-a-time-structure-from-the-input-string-how-doe – Hojung Kim Jan 13 '20 at 02:21