Created
December 17, 2021 18:52
-
-
Save realFranco/b3d21e946c1fb53644b7461a6a49f2c0 to your computer and use it in GitHub Desktop.
Datetime Benchmark - Given a timestamp in seconds (epoch), return the day of the week
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil | |
import random | |
import pytz | |
from datetime import date | |
import pandas as pd | |
def dates_gen(cases: int = 1000) -> list: | |
return [random.randint(0, 1639758915) for _ in range(0, cases)] | |
# dates = [pd.Timestamp] | |
def get_week_of_month_vj(tss: list): | |
# la suma es como un gap, el día 1 del més es el día x de la semana en curso | |
dates = [pd.to_datetime(ts, unit='s') for ts in tss] | |
return [int(ceil((date.day + date.replace(day=1).day_of_week) / 7.0)) for date in dates] | |
# dates = [datetime.date] | |
def get_week_of_month_vf(tss: list) -> list: | |
gmt = pytz.timezone("GMT") | |
dates = [datetime.datetime.fromtimestamp(ts, gmt) for ts in tss] | |
return [int(ceil((date.day + date.replace(day=1).weekday()) / 7.0)) for date in dates] | |
date_cases = dates_gen(10) | |
pd_dates = get_week_of_month_vj(tss=date_cases) | |
dt_dates = get_week_of_month_vf(tss=date_cases) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment