Is current date between 2 dates

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Is current date between 2 dates

Postby decompile » Mon Apr 24, 2017 8:58 pm

Hey Guys,

I tried to google my question but sadly havent found it, probably cause Im asking it wrong, but I want to do special stuff (like over holidays) if the time is between 2 date ranges.

Can you please help me?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Is current date between 2 dates

Postby satoon101 » Mon Apr 24, 2017 9:08 pm

Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Is current date between 2 dates

Postby decompile » Mon Apr 24, 2017 9:13 pm

Exactly.

I havent tested it out but could I skip the year parameter to make it every year the same without changing the code?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Is current date between 2 dates

Postby satoon101 » Tue Apr 25, 2017 12:55 am

datetime objects require year, month, and day, so no you couldn't. Plus, if you wanted to go something like December 14 - January 7, without the year it would not work. It's easy enough to get the current year anyway:

Syntax: Select all

from datetime import datetime

current_year = datetime.now().year
Image
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: Is current date between 2 dates

Postby Predz » Tue Apr 25, 2017 7:36 am

How about something like this?

Syntax: Select all

def is_between(first, second, between):
if not isinstance(between, datetime.datetime):
raise TypeError('Argument between must be of type <datetime.datetime>.')

## ORDER THE DATES
timestamps = [first.timestamp(), second.timestamp()]
highest = max(*timestamps)
lowest = min(*timestamps)

return True if between.timestamp() > lowest and between.timestamp() < highest else False


Syntax: Select all

january = datetime.datetime(2017, 1, 7)
december = datetime.datetime(2016, 12, 15)

is_between(january, december, datetime.datetime(2016, 12, 29))
# > returns True

is_between(january, december, datetime.datetime(2017, 1, 29))
# > returns False
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Is current date between 2 dates

Postby iPlayer » Tue Apr 25, 2017 9:51 am

My approach

Syntax: Select all

from time import struct_time


def _build_struct(year, month, day, hour, minute, second):
return struct_time((year, month, day, hour, minute, second, 0, 0, 0))


def build_yearless_struct(month, day, hour, minute, second):
return _build_struct(0, month, day, hour, minute, second)


def is_between(struct_x, struct_min, struct_max):
if struct_min.tm_year or struct_max.tm_year:
raise ValueError(
"struct_min and struct_max should be built using "
"build_yearless_struct function")

if struct_x.tm_year:
struct_x = build_yearless_struct(
struct_x.tm_mon, struct_x.tm_mday, struct_x.tm_hour,
struct_x.tm_min, struct_x.tm_sec)

if struct_min >= struct_max:
struct_max = _build_struct(
struct_max.tm_year + 1, struct_max.tm_mon, struct_max.tm_mday,
struct_max.tm_hour, struct_max.tm_min, struct_max.tm_sec)

struct_x = _build_struct(
struct_x.tm_year + 1, struct_x.tm_mon, struct_x.tm_mday,
struct_x.tm_hour, struct_x.tm_min, struct_x.tm_sec)

return struct_min <= struct_x < struct_max


Example / tests

Syntax: Select all

from time import localtime

# December 31
russian_holidays_start = build_yearless_struct(12, 31, 0, 0, 0)

# January 11
russian_holidays_end = build_yearless_struct(1, 11, 0, 0, 0)

# Asserting that January 1 is in between December 31 - January 11
assert is_between(
build_yearless_struct(1, 1, 12, 30, 0),
russian_holidays_start,
russian_holidays_end
)

# Asserting that January 1 is NOT in between January 11 - December 31
assert not is_between(
build_yearless_struct(1, 1, 12, 30, 0),
russian_holidays_end,
russian_holidays_start
)

if is_between(localtime(), russian_holidays_start, russian_holidays_end):
print("It's New Year holidays in Russia!")
else:
print("No New Year holidays in Russia currently :(")
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Is current date between 2 dates

Postby decompile » Wed Apr 26, 2017 1:12 am

Thanks alot! :)
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: Is current date between 2 dates

Postby Doldol » Thu Apr 27, 2017 7:49 am

This is what I usually do if I only care about day precision

Syntax: Select all

# simple example

from datetime import datetime

start_date = (2, 20)
end_date = (7, 1)

now = (datetime.now().month, datetime.now().year)

if start_date <= now <= end_date:
True # in range
else:
False # not in range


# valid date range covers year transition

from datetime import datetime

start_date = (11, 20)
end_date = (12, 31)

start_date2 = (1, 1)
end_date2 = (7, 1)

now = (datetime.now().month, datetime.now().day)

if start_date <= now <= end_date:
True # in range
elif start_date2 <= now <= end_date2:
True # in range
else:
False # not in range
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Is current date between 2 dates

Postby satoon101 » Thu Apr 27, 2017 12:15 pm

Well, that wouldn't work in the case where you wanted to bridge new years. A simple way to support multiple holidays in the same plugin would be something like this:

Syntax: Select all

from datetime import date

# ====================
# >> CONFIGURATION
# ====================
# Add the holidays
holidays = {
"St. Patrick's Day": {
# March 14 - March 20
'start': (3, 14),
'end': (3, 20),
},
"Easter": {
# March 22 - April 25
'start': (3, 22),
'end': (4, 25),
},
"4th of July": {
# July 1 - July 6
'start': (7, 1),
'end': (7, 6),
},
"Halloween": {
# October 20 - November 2
'start': (10, 20),
'end': (11, 2),
},
"Thanksgiving": {
# November 20 - November 30
'start': (11, 20),
'end': (11, 30),
},
"Christmas": {
# December 20 - December 26
'start': (12, 20),
'end': (12, 26),
},
"New Year's": {
# December 29 - January 3
'start': (12, 29),
'end': (1, 3),
},
}
# ====================
# >> END OF CONFIGURATION
# ====================


def get_holiday():
today = date.today()
current_year = today.year
for holiday, date_range in holidays.items():
start = date_range['start']
end = date_range['end']
if date(current_year, *start) < today < date(current_year, *end):
return holiday

# Does the date range not bridge new year's?
if start < end:
continue

previous_year = current_year - 1
if date(previous_year, *start) < today < date(current_year, *end):
return holiday

next_year = current_year + 1
if date(current_year, *start) < today < date(next_year, *end):
return holiday

return None
Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Is current date between 2 dates

Postby decompile » Thu Apr 27, 2017 4:52 pm

Jackpot, thank you!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 37 guests