How can I check if a datetime is this week? - python-datetime

How can I check whether an email was sent this week or not from the datetime parsed from the email? For today we can do:
yourdatetime.date() < datetime.today().date()
But for a week, first we need to define what a week is, which in our case is all emails since the previous Friday. I should be able to compute it by hand, but trying to see if there are datetime functions I can use to make the code more readable for the next person.

The best you could get directly would be the use of strftime to get the week number. Extract from the page on strftime() and strptime() Behavior :
%U : Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. Output : 00, 01, ..., 53
%W : Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. Output : 00, 01, ..., 53
But ... this only accept either monday or sunday as first day of the week, neither friday, nor saturday. If it is not enough, you will have to develop your own algorythm in a dedicated function.

Related

Ruby week number results

Why when I use
Time.now.strftime('%Y%W')
or
Date.today.strftime('%Y%W')
they return 201912 while it should be 201913 as we are in week 13, not 12.
How to get the current week number?
If you look in the documentation, it says this:
Week number: The first week of YYYY that starts with a Sunday or
Monday (according to %U or %W). The days in the year before the first
week are in week 0.
%U - Week number of the year. The week starts with Sunday. (00..53)
%W - Week number of the year. The week starts with Monday. (00..53)
January 1st, 2019 was on a Tuesday, so that would have been week 0 - making today week 12.
Time.now.strftime('%Y%V')
Would give you the output your looking for.
%V - Week number of the week-based year (01..53)
You have to look for the week-based year.

Why does Ruby standard library Date Module start at -4712 year?

Date.new results in # -4712-01-01.
Just as per the title, what logic am i missing, why start from -4712?
https://en.wikipedia.org/wiki/Julian_day
The Julian Period is a chronological interval of 7980 years beginning 4713 BC. It has been used by historians since its introduction in 1583 to convert between different calendars. 2015 is year 6728 of the current Julian Period. The next Julian Period begins in the year 3268 AD.
The internal representation of a Date or DateTime object is an astronomical Julian date: a fractional number of days since a "time zero" in 4712 BCE.
More information are also available in the Ruby documentation for Date.
The Julian day number is in elapsed days since noon (Greenwich mean time) on January 1, 4713 BCE (in the Julian calendar).
In this document, the astronomical Julian day number is same as the original Julian day number. And the chronological Julian day number is a variation of the Julian day number. Its days begin at midnight on local time.
From Wikipedia
The Julian day number can be calculated using the following formulas (integer division is used exclusively, that is, the remainder of all divisions are dropped):
The months (M) January to December are 1 to 12. For the year (Y) astronomical year numbering is used, thus 1 BC is 0, 2 BC is −1, and 4713 BC is −4712. D is the day of the month. JDN is the Julian Day Number, which pertains to the noon occurring in the corresponding calendar date.

oracle function to calculate week of date by considering Sunday to Saturday as a week

Hi we have requirement to determine week of date by considering "Sunday to Saturday" as one week but i went through the link [oracle function][1]
[1]: http://www.techonthenet.com/oracle/functions/to_char.php here there are options like 'IW'(week of year ISO standard) which calculates week of year by considering Monday to Sunday as one week but we have specs to consider "Sunday to Saturday" as one week.Can any one suggest how to calculate ?
In order to determine the "week number" you need basically two informations:
On which day does your week start? This you provided, week begins on Sunday
Which week do you consider as first week of the year? This you did
not tell us.
The second definition may overrule the first one, e.g. "week 1 starts on the first day of the year and continues to the seventh day of the year." as used in Oracle TO_CHAR(..., 'WW')

What is the extra data when I inspect a Date object?

What is the extra data included in a date object? Given the following example:
time = Time.at(1392328830)
# => 2014-02-13 15:00:30 -0700
date = time.to_date
# => #<Date: 2014-02-13 ((2456702j,0s,0n),+0s,2299161j)>
What does all this represent? It's not clear from looking at the Ruby Date documentation.
((2456702j,0s,0n),+0s,2299161j)
What you're seeing is the output from Object.inspect which is a human-readable representation of an object. In the case of the Date class:
From date.rb:
# Return internal object state as a programmer-readable string.
def inspect() format('#<%s: %s,%s,%s>', self.class, #ajd, #of, #sg) end
# Return the date as a human-readable string.
#
# The format used is YYYY-MM-DD.
def to_s() strftime end
The instance variables are:
#ajd is an Astronomical Julian Day Number
#of is an offset or fraction of a day from UTC
#sg is the Day of Calendar Reform
But what do these terms mean?
1. What is an Astronomical Julian Day Number? (#ajd)
For scientific purposes, it is convenient to refer to a date simply as a day count, counting from an arbitrary initial day. The date first chosen for this was January 1, 4713 BCE. A count of days from this date is the Julian *Day* Number or Julian *Date*. This is in local time, and counts from midnight on the initial day. The stricter usage is in UTC, and counts from midday on the initial day. This is referred to in the Date class as the Astronomical *Julian* Day *Number*. In the Date class, the Astronomical Julian Day Number includes fractional days.
2. Offset from what? (#offset)
Time zones are represented as an offset from UTC, as a fraction of a day. This offset is the how much local time is later (or earlier) than UTC. UTC offset 0 is centered on England (also known as GMT). As you travel east, the offset increases until you reach the dateline in the middle of the Pacific Ocean; as you travel west, the offset decreases.
3. What is the Day of Calendar Reform? (#sg)
The Gregorian Calendar was introduced at different times in different regions. The day on which it was introduced for a particular region is the Day *of* Calendar *Reform* for that region. This is abbreviated as sg (for Start of Gregorian calendar) in the Date class.
From what I can tell, the Gregorian Calendar is calendar that self-corrects via leap years.

Writing a weekly recurring time interval (ISO 8601)

i need a correct weekly ISO 8601 recurring time interval.
For example, repeat:
Each Monday, 19:00.
I already tried a lot of examples, but nothing worked correctly :-(
Thx for any help!
R/2014-W01-1T19:00:00/P1W
Represents a unbounded recurrence with a start date and a duration of one week. The recurrence starts at the first day (Monday) of the week number 01 in the week year 2014. I have used a week date, but you can substitute it with a calendar date or ordinal date, just make sure the date falls on a Monday.

Resources