def test_to_string(self): now = now_utc_aware() seoul_tzinfo = LocaleInfo.get_tzinfo("Asia/Taipei") actual_str = self.get_data().to_string(self.get_user_model()) expected_str = [ Timer.FUTURE.format( event=TestTimerListResult.TMR_3.title, diff=t_delta_str( TestTimerListResult.TMR_3.get_target_time_diff(now)), time=localtime(TestTimerListResult.TMR_3.target_time, seoul_tzinfo)), Timer.FUTURE.format( event=TestTimerListResult.TMR_4.title, diff=t_delta_str( TestTimerListResult.TMR_4.get_target_time_diff(now)), time=localtime(TestTimerListResult.TMR_4.target_time, seoul_tzinfo)), "", Timer.PAST_CONTINUE.format( event=TestTimerListResult.TMR_2.title, diff=t_delta_str( TestTimerListResult.TMR_2.get_target_time_diff(now)), time=localtime(TestTimerListResult.TMR_2.target_time, seoul_tzinfo)), "", Timer.PAST_DONE.format( event=TestTimerListResult.TMR_1.title, diff=t_delta_str( TestTimerListResult.TMR_1.get_target_time_diff(now)), time=localtime(TestTimerListResult.TMR_1.target_time, seoul_tzinfo)) ] expected_str = "\n".join(expected_str) self.assertEqual(actual_str, expected_str)
def get_member_info(channel_model: ChannelModel) -> List[MemberInfoEntry]: ret = [] prof_conns = ProfileManager.get_channel_members(channel_model.id, available_only=True) user_oids = [mdl.user_oid for mdl in prof_conns] user_name_dict = IdentitySearcher.get_batch_user_name( user_oids, channel_model) last_message_oids = MessageRecordStatisticsManager.get_user_last_message_ts( channel_model.id, user_oids) for prof_conn in prof_conns: user_oid = prof_conn.user_oid user_name = user_name_dict.get(user_oid) or str(user_oid) first_joined = localtime(prof_conn.id.generation_time) last_message_at = last_message_oids.get(user_oid) if last_message_at: last_message_at = localtime(last_message_at) ret.append( MemberInfoEntry(user_oid=user_oid, user_name=user_name, first_joined=first_joined, last_message_at=last_message_at)) return ret
def process_timer_notification( e: TextMessageEventObject) -> List[HandledMessageEvent]: within_secs = min( TimerManager.get_notify_within_secs( MessageRecordStatisticsManager.get_message_frequency( e.channel_oid, Bot.Timer.MessageFrequencyRangeMin)), Bot.Timer.MaxNotifyRangeSeconds) tmr_ntf = TimerManager.get_notify(e.channel_oid, within_secs) tmr_up = TimerManager.get_time_up(e.channel_oid) ret = [] if tmr_up: ret.append(_("**{} timer(s) have timed up!**").format(len(tmr_up))) ret.append("") for tmr in tmr_up: ret.append( _("- {event} has timed up! (at {time})").format( event=tmr.title, time=localtime(tmr.target_time, e.user_model.config.tzinfo))) if tmr_ntf: if ret: ret.append("-------------") now = now_utc_aware() ret.append( _("**{} timer(s) will time up in less than {:.0f} minutes!**"). format(len(tmr_ntf), within_secs / 60)) ret.append("") for tmr in tmr_ntf: ret.append( _("- {event} will time up after [{diff}]! (at {time})").format( event=tmr.title, diff=t_delta_str(tmr.get_target_time_diff(now)), time=localtime(tmr.target_time, e.user_model.config.tzinfo))) if ret and ret[-1] == "": ret = ret[:-1] if ret: return [HandledMessageEventText(content="\n".join(ret))] else: return []
def test_tzinfo(self): start_dt = datetime(2020, 2, 14, 12, 1, 1, tzinfo=pytz.UTC) end_dt = datetime(2020, 2, 16, 12, 1, 1, tzinfo=pytz.UTC) tz = pytz.timezone("America/New_York") tr = TimeRange(start=start_dt, end=end_dt, tzinfo_=tz) self.assertAlmostEquals(0, abs((start_dt - tr.start).total_seconds()), 0) self.assertAlmostEquals(0, abs((start_dt - tr.start_org).total_seconds()), 0) self.assertAlmostEquals(0, abs((tr.end - end_dt).total_seconds()), 0) self.assertEqual(tz.utcoffset(datetime.now()), tr.tzinfo_.utcoffset(datetime.now())) self.assertTrue(tr.expandable) self.assertFalse(tr.expanded) self.assertFalse(tr.is_inf) self.assertAlmostEquals(48, tr.hr_length_org, 0) self.assertAlmostEquals(48, tr.hr_length, 0) self.assertEqual( f"{start_dt.strftime('%m-%d')} ~ {end_dt.strftime('%m-%d')}", tr.expr_period_short) self.assertAlmostEquals(tr.end_time_seconds, time_to_seconds(localtime(end_dt, tz).time()), 0) prd = tr.get_periods() self.assertListEqual([tr], prd)
def __post_init__(self, tz: Optional[tzinfo]): self.timestamp = localtime( ObjectId(self.model.id).generation_time, tz).strftime("%Y-%m-%d %H:%M:%S") self.content_type = self.model.message_type self.content_html = self.model.message_content.replace("\n", "<br>") self.process_time_ms = self.model.process_time_secs * 1000
def get_user_last_message_ts(self, channel_oid: ObjectId, user_oids: List[ObjectId], tzinfo_: tzinfo = None) \ -> Dict[ObjectId, datetime]: ret = {} KEY_TS = "msgts" pipeline = [{ "$match": { MessageRecordModel.ChannelOid.key: channel_oid, MessageRecordModel.UserRootOid.key: { "$in": user_oids } } }, { "$sort": { "_id": pymongo.DESCENDING } }, { "$group": { "_id": "$" + MessageRecordModel.UserRootOid.key, KEY_TS: { "$first": "$" + OID_KEY } } }] for data in self.aggregate(pipeline): ret[data[OID_KEY]] = localtime(data[KEY_TS].generation_time, tzinfo_) return ret
def test_dt_naive(self): self.assertTrue(is_tz_naive(datetime.now())) self.assertTrue(is_tz_naive(datetime.utcnow())) self.assertTrue(is_tz_naive(localtime().replace(tzinfo=None))) self.assertFalse(is_tz_naive(now_utc_aware())) self.assertTrue(is_tz_naive(datetime(2020, 5, 7))) self.assertTrue(is_tz_naive(datetime.min)) self.assertFalse(is_tz_naive( datetime.min.replace(tzinfo=timezone.utc))) self.assertTrue(is_tz_naive(datetime.max)) self.assertFalse(is_tz_naive( datetime.max.replace(tzinfo=timezone.utc)))
def to_string(self, user_model): now = now_utc_aware() tzinfo = user_model.config.tzinfo ret = [] if self.future: for tmr in self.future: ret.append( Timer.FUTURE.format(event=tmr.title, diff=t_delta_str( tmr.get_target_time_diff(now)), time=localtime(tmr.target_time, tzinfo))) ret.append("") # Separator if self.past_continue: for tmr in self.past_continue: ret.append( Timer.PAST_CONTINUE.format( event=tmr.title, diff=t_delta_str(tmr.get_target_time_diff(now)), time=localtime(tmr.target_time, tzinfo))) ret.append("") # Separator if self.past_done: for tmr in self.past_done: ret.append( Timer.PAST_DONE.format(event=tmr.title, time=localtime( tmr.target_time, tzinfo))) # Take out the final separator if ret and ret[-1] == "": ret = ret[:-1] return "\n".join(ret)
def expiry(self): tzinfo = LocaleInfo.get_tzinfo(self.locale_code) return localtime(self.expiry_utc, tzinfo)
def removed_at_expr(self) -> Optional[str]: if self.removed_at: return localtime(self.removed_at).strftime("%Y-%m-%d %H:%M:%S") else: return None
def last_used_expr(self) -> Optional[str]: if self.last_used: return localtime(self.last_used).strftime("%Y-%m-%d %H:%M:%S") else: return None