Best Python code snippet using freezegun
email_subs.py
Source:email_subs.py
1import os2from datetime import datetime, timedelta, timezone3from flask import Blueprint, request4from prometheus import metrics5routes = Blueprint('email-subs', __name__, url_prefix='/sub')6# noinspection PyPep87from sqlalchemy.exc import IntegrityError8from werkzeug.exceptions import Forbidden9from db import db10from models.caseDevelopments import CaseDevelopments11from models.email_subs import EmailSub, EmailSubsSchema, email_subs_schema, SubscribedCounties12from services.crypt_service import decrypt_message13# noinspection PyUnresolvedReferences14from psycopg2.errors import UniqueViolation15from models.risklayerPrognosis import RisklayerPrognosis16# noinspection PyPep817@routes.route('/', methods=['POST'])18def subscribe_new():19 """20 Add a new subscription21 """22 schema = EmailSubsSchema(exclude=['id', 'token'])23 try:24 new_sub = EmailSub(25 email=request.json['email'],26 email_hash=request.json['email'],27 lang=request.json['lang'],28 last_email_sent=datetime.now(timezone.utc)29 )30 new_sub.update_token()31 db.session.add(new_sub)32 db.session.flush()33 counties = []34 if 'counties' in request.json:35 counties = request.json['counties']36 for cjson in counties:37 c = SubscribedCounties()38 c.sub_id = new_sub.id39 c.ags = cjson['ags']40 db.session.add(c)41 db.session.commit()42 new_sub.send_email(43 subject='[CoronaVis] Verifiziere deine E-Mail-Adresse',44 sender='coronavis@dbvis.inf.uni-konstanz.de',45 template='mail/email_verification',46 id=new_sub.id,47 token=new_sub.token48 )49 return schema.dump(new_sub), 20150 except IntegrityError as ex:51 print(ex)52 assert isinstance(ex.orig, UniqueViolation) # proves the original exception53 db.session.rollback()54 # noinspection PyPep855 sub = db.session.query(EmailSub) \56 .filter(EmailSub.verified == False, EmailSub.email_hash == request.json['email']) \57 .first()58 # resend verification59 if sub is not None:60 sub.send_email(61 subject='[CoronaVis] Verifiziere deine E-Mail-Adresse',62 sender='coronavis@dbvis.inf.uni-konstanz.de',63 template='mail/email_verification',64 id=sub.id,65 token=sub.token66 )67 return schema.dump(sub), 20168@routes.route('/<sub_id>/<token>', methods=['GET'])69def get(sub_id, token):70 sub = __get_and_verify(sub_id, token)71 return email_subs_schema.dump(sub)72@routes.route('/<sub_id>/<token>', methods=['PATCH'])73def update(sub_id, token):74 sub = __get_and_verify(sub_id, token)75 if 'email' in request.json:76 sub.email = request.json['email']77 if 'lang' in request.json:78 sub.lang = request.json['lang']79 if 'verified' in request.json:80 sub.verified = request.json['verified']81 counties = []82 if 'counties' in request.json:83 counties = request.json['counties']84 db.session.query(SubscribedCounties).filter_by(sub_id=sub_id).delete()85 db.session.commit()86 for cjson in counties:87 c = SubscribedCounties()88 c.sub_id = sub_id89 c.ags = cjson['ags']90 db.session.add(c)91 db.session.commit()92 return email_subs_schema.dump(sub)93@routes.route('/<sub_id>/<token>', methods=['DELETE'])94def delete(sub_id, token):95 sub = __get_and_verify(sub_id, token)96 db.session.delete(sub)97 db.session.commit()98 return '', 20099@routes.route('/send-notifications', methods=['POST'])100@metrics.do_not_track()101def send_notifications():102 if not request.headers.get('X-API-KEY') == os.getenv('API_KEY'):103 f = Forbidden()104 f.description = 'Invalid API key'105 raise f106 __delete_unverified_emails()107 __rotate_tokens()108 c = CaseDevelopments('cases_per_county_and_day_risklayer')109 sevendaysago = (datetime.now(timezone.utc) - timedelta(days=8)).strftime('%Y-%m-%d')110 tomorrow = (datetime.now(timezone.utc) + timedelta(days=1)).strftime('%Y-%m-%d')111 de = c.get_country(from_time=sevendaysago, to_time=tomorrow, id_country='de', want_age_groups=False,112 want_geom=False)113 de_developments = de['properties']['developments']114 de_today = de_developments[-1]115 de_24h = de_developments[-2]116 de_72h = de_developments[-4]117 de_7d = de_developments[-8]118 prognosis = RisklayerPrognosis.query.order_by(RisklayerPrognosis.datenbestand.desc()).limit(1)[0]119 sql_result = db.engine.execute('''120 SELECT id, email, token, lang, c.ags, MAX(c.updated_at), 121 (CASE WHEN c.updated_at::date = e.last_email_sent::date THEN true ELSE false END) AS second_email122 FROM email_subs e123 JOIN email_subs_counties esc on e.id = esc.sub_id124 JOIN cases_lk_risklayer c ON esc.ags = c.ags125 WHERE c.updated_today = true AND c.date = now()::date126 AND c.updated_at > e.last_email_sent AND e.verified = true127 GROUP BY id, email, token, lang, c.ags, c.updated_at::date, e.last_email_sent::date128 ''')129 num_emails = 0130 for row in sql_result:131 lk = c.get_county(from_time=sevendaysago, to_time=tomorrow, id_county=row['ags'], want_age_groups=False,132 want_geom=False)133 lk_developments = lk['properties']['developments']134 lk_today = lk_developments[-1]135 lk_24h = lk_developments[-2]136 lk_72h = lk_developments[-4]137 lk_7d = lk_developments[-8]138 desc = lk['properties']['description']139 name = lk['properties']['name']140 sub: EmailSub = EmailSub.query.get_or_404(row['id'])141 sub.send_email(142 subject=f'[CoronaVis] Neue Daten für {desc} {name}',143 sender='coronavis@dbvis.inf.uni-konstanz.de',144 template='mail/county_notification_' + sub.lang,145 sub_id=row['id'],146 token=decrypt_message(row['token'].tobytes()),147 second_email=row['second_email'],148 county_id=row['ags'],149 county_desc=desc,150 county_name=name,151 county_population=round(lk_today['population']),152 county_last_updated=lk_today['last_updated'],153 county_cases_total=lk_today['cases'],154 county_cases_total_100k=round(__100k_change(lk_today['cases'], 0, lk_today['population']), 2),155 county_cases_24=__diff_str(lk_today['cases'] - lk_24h['cases']),156 county_cases_24_prc=__diff_str(__prc_change(lk_today['cases'], lk_24h['cases'])),157 county_cases_24_100k=__diff_str(__100k_change(lk_today['cases'], lk_24h['cases'], lk_today['population'])),158 county_cases_72=__diff_str(lk_today['cases'] - lk_72h['cases']),159 county_cases_72_prc=__diff_str(__prc_change(lk_today['cases'], lk_72h['cases'])),160 county_cases_72_100k=__diff_str(__100k_change(lk_today['cases'], lk_72h['cases'], lk_today['population'])),161 county_cases_7=__diff_str(lk_today['cases'] - lk_7d['cases']),162 county_cases_7_prc=__diff_str(__prc_change(lk_today['cases'], lk_7d['cases'])),163 county_cases_7_100k=__diff_str(__100k_change(lk_today['cases'], lk_7d['cases'], lk_today['population'])),164 county_deaths_total=lk_today['deaths'],165 county_deaths_total_100k=round(__100k_change(lk_today['deaths'], 0, lk_today['population']), 2),166 county_deaths_24=__diff_str(lk_today['deaths'] - lk_24h['deaths']),167 county_deaths_24_prc=__diff_str(__prc_change(lk_today['deaths'], lk_24h['deaths'])),168 county_deaths_24_100k=__diff_str(169 __100k_change(lk_today['deaths'], lk_24h['deaths'], lk_today['population'])),170 county_deaths_72=__diff_str(lk_today['deaths'] - lk_72h['deaths']),171 county_deaths_72_prc=__diff_str(__prc_change(lk_today['deaths'], lk_72h['deaths'])),172 county_deaths_72_100k=__diff_str(173 __100k_change(lk_today['deaths'], lk_72h['deaths'], lk_today['population'])),174 county_deaths_7=__diff_str(lk_today['deaths'] - lk_7d['deaths']),175 county_deaths_7_prc=__diff_str(__prc_change(lk_today['deaths'], lk_7d['deaths'])),176 county_deaths_7_100k=__diff_str(__100k_change(lk_today['deaths'], lk_7d['deaths'], lk_today['population'])),177 # Covid-19 Patients178 county_patients_total=lk_today['cases_covid'],179 county_patients_total_100k=round(__100k_change(lk_today['cases_covid'], 0, lk_today['population']), 2),180 county_patients_24=__diff_str(lk_today['cases_covid'] - lk_24h['cases_covid']),181 county_patients_24_prc=__diff_str(__prc_change(lk_today['cases_covid'], lk_24h['cases_covid'])),182 county_patients_24_100k=__diff_str(183 __100k_change(lk_today['cases_covid'], lk_24h['cases_covid'], lk_today['population'])),184 county_patients_72=__diff_str(lk_today['cases_covid'] - lk_72h['cases_covid']),185 county_patients_72_prc=__diff_str(__prc_change(lk_today['cases_covid'], lk_72h['cases_covid'])),186 county_patients_72_100k=__diff_str(187 __100k_change(lk_today['cases_covid'], lk_72h['cases_covid'], lk_today['population'])),188 county_patients_7=__diff_str(lk_today['cases_covid'] - lk_7d['cases_covid']),189 county_patients_7_prc=__diff_str(__prc_change(lk_today['cases_covid'], lk_7d['cases_covid'])),190 county_patients_7_100k=__diff_str(191 __100k_change(lk_today['cases_covid'], lk_7d['cases_covid'], lk_today['population'])),192 # Covid-19 Patients Ventilated193 county_patients_ventilated_total=lk_today['cases_covid_ventilated'],194 county_patients_ventilated_total_100k=round(195 __100k_change(lk_today['cases_covid_ventilated'], 0, lk_today['population']), 2),196 county_patients_ventilated_24=__diff_str(197 lk_today['cases_covid_ventilated'] - lk_24h['cases_covid_ventilated']),198 county_patients_ventilated_24_prc=__diff_str(199 __prc_change(lk_today['cases_covid_ventilated'], lk_24h['cases_covid_ventilated'])),200 county_patients_ventilated_24_100k=__diff_str(201 __100k_change(202 lk_today['cases_covid_ventilated'], lk_24h['cases_covid_ventilated'], lk_today['population'])),203 county_patients_ventilated_72=__diff_str(204 lk_today['cases_covid_ventilated'] - lk_72h['cases_covid_ventilated']),205 county_patients_ventilated_72_prc=__diff_str(206 __prc_change(lk_today['cases_covid_ventilated'], lk_72h['cases_covid_ventilated'])),207 county_patients_ventilated_72_100k=__diff_str(208 __100k_change(209 lk_today['cases_covid_ventilated'], lk_72h['cases_covid_ventilated'], lk_today['population'])),210 county_patients_ventilated_7=__diff_str(211 lk_today['cases_covid_ventilated'] - lk_7d['cases_covid_ventilated']),212 county_patients_ventilated_7_prc=__diff_str(213 __prc_change(lk_today['cases_covid_ventilated'], lk_7d['cases_covid_ventilated'])),214 county_patients_ventilated_7_100k=__diff_str(215 __100k_change(216 lk_today['cases_covid_ventilated'], lk_7d['cases_covid_ventilated'], lk_today['population'])),217 # Bed Occupancy218 county_bed_occupancy_total=f"{lk_today['beds_occupied']}/{lk_today['beds_total']}",219 county_bed_occupancy_total_prc=round((lk_today['beds_occupied'] / lk_today['beds_total']) * 10000) / 100,220 county_bed_occupancy_24=f"{__diff_str(lk_today['beds_occupied'] - lk_24h['beds_occupied'])}/{__diff_str(lk_today['beds_total'] - lk_24h['beds_total'])}",221 county_bed_occupancy_24_prc=__diff_str(__prc_change(lk_today['beds_occupied'] / lk_today['beds_total'],222 lk_24h['beds_occupied'] / lk_24h['beds_total'])),223 county_bed_occupancy_72=f"{__diff_str(lk_today['beds_occupied'] - lk_72h['beds_occupied'])}/{__diff_str(lk_today['beds_total'] - lk_72h['beds_total'])}",224 county_bed_occupancy_72_prc=__diff_str(__prc_change(lk_today['beds_occupied'] / lk_today['beds_total'],225 lk_72h['beds_occupied'] / lk_72h['beds_total'])),226 county_bed_occupancy_7=f"{__diff_str(lk_today['beds_occupied'] - lk_7d['beds_occupied'])}/{__diff_str(lk_today['beds_total'] - lk_7d['beds_total'])}",227 county_bed_occupancy_7_prc=__diff_str(__prc_change(lk_today['beds_occupied'] / lk_today['beds_total'],228 lk_7d['beds_occupied'] / lk_7d['beds_total'])),229 num_counties_reported=de_today['num_counties_reported'],230 num_counties_total=de_today['num_counties_total'],231 prognosis=round(prognosis.prognosis),232 country_population=round(de_today['population']),233 country_last_updated=de_today['last_updated'],234 country_cases_total=de_today['cases'],235 country_cases_total_100k=round(__100k_change(de_today['cases'], 0, de_today['population']), 2),236 country_cases_24=__diff_str(de_today['cases'] - de_24h['cases']),237 country_cases_24_prc=__diff_str(__prc_change(de_today['cases'], de_24h['cases'])),238 country_cases_24_100k=__diff_str(__100k_change(de_today['cases'], de_24h['cases'], de_today['population'])),239 country_cases_72=__diff_str(de_today['cases'] - de_72h['cases']),240 country_cases_72_prc=__diff_str(__prc_change(de_today['cases'], de_72h['cases'])),241 country_cases_72_100k=__diff_str(__100k_change(de_today['cases'], de_72h['cases'], de_today['population'])),242 country_cases_7=__diff_str(de_today['cases'] - de_7d['cases']),243 country_cases_7_prc=__diff_str(__prc_change(de_today['cases'], de_7d['cases'])),244 country_cases_7_100k=__diff_str(__100k_change(de_today['cases'], de_7d['cases'], de_today['population'])),245 country_deaths_total=de_today['deaths'],246 country_deaths_total_100k=round(__100k_change(de_today['deaths'], 0, de_today['population']), 2),247 country_deaths_24=__diff_str(de_today['deaths'] - de_24h['deaths']),248 country_deaths_24_prc=__diff_str(__prc_change(de_today['deaths'], de_24h['deaths'])),249 country_deaths_24_100k=__diff_str(250 __100k_change(de_today['deaths'], de_24h['deaths'], de_today['population'])),251 country_deaths_72=__diff_str(de_today['deaths'] - de_72h['deaths']),252 country_deaths_72_prc=__diff_str(__prc_change(de_today['deaths'], de_72h['deaths'])),253 country_deaths_72_100k=__diff_str(254 __100k_change(de_today['deaths'], de_72h['deaths'], de_today['population'])),255 country_deaths_7=__diff_str(de_today['deaths'] - de_7d['deaths']),256 country_deaths_7_prc=__diff_str(__prc_change(de_today['deaths'], de_7d['deaths'])),257 country_deaths_7_100k=__diff_str(258 __100k_change(de_today['deaths'], de_7d['deaths'], de_today['population'])),259 # Covid-19 Patients260 country_patients_total=de_today['cases_covid'],261 country_patients_total_100k=round(__100k_change(de_today['cases_covid'], 0, de_today['population']), 2),262 country_patients_24=__diff_str(de_today['cases_covid'] - de_24h['cases_covid']),263 country_patients_24_prc=__diff_str(__prc_change(de_today['cases_covid'], de_24h['cases_covid'])),264 country_patients_24_100k=__diff_str(265 __100k_change(de_today['cases_covid'], de_24h['cases_covid'], de_today['population'])),266 country_patients_72=__diff_str(de_today['cases_covid'] - de_72h['cases_covid']),267 country_patients_72_prc=__diff_str(__prc_change(de_today['cases_covid'], de_72h['cases_covid'])),268 country_patients_72_100k=__diff_str(269 __100k_change(de_today['cases_covid'], de_72h['cases_covid'], de_today['population'])),270 country_patients_7=__diff_str(de_today['cases_covid'] - de_7d['cases_covid']),271 country_patients_7_prc=__diff_str(__prc_change(de_today['cases_covid'], de_7d['cases_covid'])),272 country_patients_7_100k=__diff_str(273 __100k_change(de_today['cases_covid'], de_7d['cases_covid'], de_today['population'])),274 # Covid-19 Patients Ventilated275 country_patients_ventilated_total=de_today['cases_covid_ventilated'],276 country_patients_ventilated_total_100k=round(277 __100k_change(de_today['cases_covid_ventilated'], 0, de_today['population']), 2),278 country_patients_ventilated_24=__diff_str(279 de_today['cases_covid_ventilated'] - de_24h['cases_covid_ventilated']),280 country_patients_ventilated_24_prc=__diff_str(281 __prc_change(de_today['cases_covid_ventilated'], de_24h['cases_covid_ventilated'])),282 country_patients_ventilated_24_100k=__diff_str(283 __100k_change(284 de_today['cases_covid_ventilated'], de_24h['cases_covid_ventilated'], de_today['population'])),285 country_patients_ventilated_72=__diff_str(286 de_today['cases_covid_ventilated'] - de_72h['cases_covid_ventilated']),287 country_patients_ventilated_72_prc=__diff_str(288 __prc_change(de_today['cases_covid_ventilated'], de_72h['cases_covid_ventilated'])),289 country_patients_ventilated_72_100k=__diff_str(290 __100k_change(291 de_today['cases_covid_ventilated'], de_72h['cases_covid_ventilated'], de_today['population'])),292 country_patients_ventilated_7=__diff_str(293 de_today['cases_covid_ventilated'] - de_7d['cases_covid_ventilated']),294 country_patients_ventilated_7_prc=__diff_str(295 __prc_change(de_today['cases_covid_ventilated'], de_7d['cases_covid_ventilated'])),296 country_patients_ventilated_7_100k=__diff_str(297 __100k_change(298 de_today['cases_covid_ventilated'], de_7d['cases_covid_ventilated'], de_today['population'])),299 # Bed Occupancy300 country_bed_occupancy_total=f"{de_today['beds_occupied']}/{de_today['beds_total']}",301 country_bed_occupancy_total_prc=round((de_today['beds_occupied'] / de_today['beds_total']) * 10000) / 100,302 country_bed_occupancy_24=f"{__diff_str(de_today['beds_occupied'] - de_24h['beds_occupied'])}/{__diff_str(de_today['beds_total'] - de_24h['beds_total'])}",303 country_bed_occupancy_24_prc=__diff_str(__prc_change(de_today['beds_occupied'] / de_today['beds_total'],304 de_24h['beds_occupied'] / de_24h['beds_total'])),305 country_bed_occupancy_72=f"{__diff_str(de_today['beds_occupied'] - de_72h['beds_occupied'])}/{__diff_str(de_today['beds_total'] - de_72h['beds_total'])}",306 country_bed_occupancy_72_prc=__diff_str(__prc_change(de_today['beds_occupied'] / de_today['beds_total'],307 de_72h['beds_occupied'] / de_72h['beds_total'])),308 country_bed_occupancy_7=f"{__diff_str(de_today['beds_occupied'] - de_7d['beds_occupied'])}/{__diff_str(de_today['beds_total'] - de_7d['beds_total'])}",309 country_bed_occupancy_7_prc=__diff_str(__prc_change(de_today['beds_occupied'] / de_today['beds_total'],310 de_7d['beds_occupied'] / de_7d['beds_total']))311 )312 db.session.add(sub)313 db.session.commit()314 num_emails += 1315 return f'emails sent {num_emails}', 200316def __prc_change(new, old) -> float:317 if old == 0:318 return 0319 return ((new - old) / old) * 100320def __100k_change(new, old, pop) -> float:321 return ((new - old) / pop) * 100000322def __diff_str(val) -> str:323 if val > 0:324 return '+' + str(round(val, 2))325 return str(round(val, 2))326def __get_and_verify(sub_id, token) -> EmailSub:327 sub = EmailSub.query.get_or_404(sub_id)328 if not sub.verify_token(token):329 b = Forbidden()330 b.description = 'invalid token'331 raise b332 return sub333def __rotate_tokens():334 since = datetime.now(timezone.utc) - timedelta(hours=72)335 subs = db.session.query(EmailSub).filter(EmailSub.token_updated < since).all()336 for s in subs:337 s.update_token()338 db.session.commit()339def __delete_unverified_emails():340 since = datetime.now(timezone.utc) - timedelta(hours=1)341 db.session.query(EmailSub).filter(EmailSub.last_email_sent < since, EmailSub.verified == False).delete()...
daterange.js
Source:daterange.js
...284 switch (dateRangeKey) {285 case goog.date.DateRange.StandardDateRangeKeys.YESTERDAY:286 return goog.date.DateRange.yesterday(opt_today);287 case goog.date.DateRange.StandardDateRangeKeys.TODAY:288 return goog.date.DateRange.today(opt_today);289 case goog.date.DateRange.StandardDateRangeKeys.LAST_7_DAYS:290 return goog.date.DateRange.last7Days(opt_today);291 case goog.date.DateRange.StandardDateRangeKeys.THIS_MONTH:292 return goog.date.DateRange.thisMonth(opt_today);293 case goog.date.DateRange.StandardDateRangeKeys.LAST_MONTH:294 return goog.date.DateRange.lastMonth(opt_today);295 case goog.date.DateRange.StandardDateRangeKeys.THIS_WEEK:296 return goog.date.DateRange.thisWeek(opt_today);297 case goog.date.DateRange.StandardDateRangeKeys.LAST_WEEK:298 return goog.date.DateRange.lastWeek(opt_today);299 case goog.date.DateRange.StandardDateRangeKeys.LAST_BUSINESS_WEEK:300 return goog.date.DateRange.lastBusinessWeek(opt_today);301 case goog.date.DateRange.StandardDateRangeKeys.ALL_TIME:302 return goog.date.DateRange.allTime(opt_today);...
todayTime.js
Source:todayTime.js
1function getYearOne(){//è·å¾å½å¹´ç¬¬ä¸å¤©2 var today=new Date();3 year = today.getFullYear();4 5 var iniStartTime = year+"-01-01";6 return iniStartTime;7}89//è·å¾å½æ第ä¸å¤©10function getMonthOne(){//è·å¾ä»å¤©11 var today=new Date();12 year = today.getFullYear();13 14 month = today.getMonth()+1;15 if(month<10){month="0"+month;}16 17 var iniEndTime = year+"-"+month+"-01";18 return iniEndTime;19}2021function getToday(){//è·å¾ä»å¤©22 23 var today=new Date();24 year = today.getFullYear();25 26 month = today.getMonth()+1;27 if(month<10){month="0"+month;}28 29 date = today.getDate();30 if(date<10){date="0"+date;}31 32 var iniEndTime = year+"-"+month+"-"+date;33 return iniEndTime;34}3536function getMonthSelect(monthId){//è·å¾å½æ37 var today=new Date();38 month = today.getMonth()+1;39/* if(month<10){month="0"+month;}40 $(monthId).val(month);*/4142 43 $(monthId).find("option").each(function(){44 if( month == $(this).val() ){45 $(this).attr({selected:"selected"})46 }47 })4849 return month;50}5152function getYearSelect(yearId){53 var today=new Date();54 var year = today.getFullYear();55 for(var i=year;i>=2012;i--){56 $(yearId).append("<option value='"+i+"'>"+i+"</option>");57 }58}5960function getYearSelect2017over(yearId){61 var today=new Date();62 var year = today.getFullYear();63 for(var i=year;i>=2017;i--){64 $(yearId).append("<option value='"+i+"'>"+i+"</option>");65 }66}6768function getSelectToday(todayId){//éä¸ä»å¤©69 var today=new Date();70 year = today.getFullYear();71 72 month = today.getMonth()+1;73 if(month<10){month="0"+month;}74 75 date = today.getDate();76 if(date<10){date="0"+date;}77 78 var iniEndTime = year+"-"+month+"-"+date;79 $(todayId).val(iniEndTime); //ç»ææ¶é´èµå¼80}8182function getYearFlagOrNo(startTime,endTime){83 startTime = startTime.substr(0, 4);84 endTime = endTime.substr(0, 4);85 if(startTime!=endTime){86 alert("æ¶é´éåä¸è½è·¨å¹´ï¼");87 return false;88 }else{89 return true;90 }91}9293function showtime(){94 var today,hour,second,minute,year,month,date;95 var strDate ;96 today=new Date();97 var n_day = today.getDay();98 switch (n_day){99 case 0:{100 strDate = "æææ¥"101 }break;102 case 1:{103 strDate = "ææä¸"104 }break;105 case 2:{106 strDate ="ææäº"107 }break;108 case 3:{109 strDate = "ææä¸"110 }break;111 case 4:{112 strDate = "ææå"113 }break;114 case 5:{115 strDate = "ææäº"116 }break;117 case 6:{118 strDate = "ææå
"119 }break;120 case 7:{121 strDate = "æææ¥"122 }break;123 }124 year = today.getFullYear();125 month = today.getMonth()+1;126 date = today.getDate();127 hour = today.getHours();128 minute =today.getMinutes();129 second = today.getSeconds();130 document.getElementById('time').innerHTML = year + "å¹´" + month + "æ" + date + "æ¥ " + strDate +" " + hour + ":" + minute + ":" + second; //æ¾ç¤ºæ¶é´131 setTimeout("showtime();", 1000); //设å®å½æ°èªå¨æ§è¡æ¶é´ä¸º 1000 ms(1 s)
...
Helperfunctions.js
Source:Helperfunctions.js
1export function first2(date){2 return ('0' + date).split('').reverse().slice(0,2).reverse().join('')3}4export function lessThanDate(date,today){5 let [dateYear,dateMonth,dateDay] = date.split('-').map(el => Number(el))6 let [todayYear,todayMonth,todayDay] = today.split('-').map(el => Number(el))7 if(todayYear > dateYear) return true8 if(todayYear == todayYear){9 if(todayMonth > dateMonth) return true10 if(todayMonth == dateMonth){11 if(todayDay > dateDay) return true12 }13 }14 return false15}16export function InThisWeek(date){17 let noOfDays = [31,28,31,30,31,30,31,31,30,31,30,31]18 let todayWeekDate = new Date().getDay()19 let [year,month,day] = [new Date().getFullYear(),new Date().getMonth() + 1,new Date().getDate()]20 let sday = (noOfDays[month - 2] + day-todayWeekDate) % noOfDays[month - 2]21 let smonth = sday == day-todayWeekDate ? month : month - 122 let syear = smonth < 0 ? year - 1 : year23 let stday = (noOfDays[month - 1] + (day + (6 - todayWeekDate))) % noOfDays[month - 1]24 let stmonth = stday == day + (6 - todayWeekDate) ? month : month + 125 let styear = stmonth > 12 ? year + 1 : year26 console.log('sunday',[sday,smonth,syear])27 console.log('saturday',[stday,stmonth,styear])28 let [dateYear,dateMonth,dateDay] = date.split('-').map(el => Number(el))29 if(dateYear > syear && dateYear < styear) return true30 if(dateYear == syear && dateYear == styear){31 if(dateMonth > smonth && dateMonth < stmonth) return true32 if(dateMonth == stmonth && dateMonth == smonth){33 if(dateDay >= sday && dateDay <= stday) return true34 }35 if(dateMonth == smonth && dateDay > sday) return true36 if(dateMonth == stmonth && dateDay < stday) return true37 }38 return false39}40export function InThisMonth(date){41 return date.split('-')[1] == (new Date().getMonth() + 1)42}43export function AfterToday(date){44 let [dateYear,dateMonth,dateDay] = date.split('-').map(el => Number(el))45 let [todayYear,todayMonth,todayDay] = `${new Date().getFullYear()}-${first2(new Date().getMonth() + 1)}-${first2(new Date().getDate())}`.split('-').map(el => Number(el))46 if(todayYear < dateYear) return true47 if(todayYear == dateYear){48 if(todayMonth < dateMonth) return true49 if(todayMonth == dateMonth){50 if(todayDay < dateDay) return true51 }52 }53 return false...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!