How do you use Unix timestamps inside discord messages - text

I'm trying to use Unix Timestamps within a Discord Message, can't find any information about it.
Thank you!

Go to https://www.epochconverter.com/ and get the epoch timestamp for the date and time you need using the Human date to Timestamp button. (eg. 1585890376)
Inside your discord message, format your message as such:
<t:########:#>
Replace ######## with your epoch timestamp,
Replace # with various letters for different options: (f, F, D, t, T or R)
Example:
For the current date, 2 years ago:
<t:1585890376:f>
April 3, 2020 1:06 AM
<t:1585890376:F>
Friday, April 3, 2020 1:06 AM
<t:1585890376:D>
April 3, 2020
<t:1585890376:t>
1:06 AM
<t:1585890376:T>
1:06:16 AM
<t:1585890376:R>
2 years ago

See format_dt in the docs.
Example:
from nextcord.utils import format_dt
from datetime import datetime
dt = datetime.now()
# Short Time (9:18 AM)
short_time = format_dt(dt, "t")
# Long Time (9:18:58 AM)
long_time = format_dt(dt, "T")
# Short Date (04/03/2022)
short_date = format_dt(dt, "d")
# Long Date (April 3, 2022)
long_date = format_dt(dt, "D")
# Default (April 3, 2022 9:18 AM)
default_date_time = format_dt(dt, "f")
# Long Date Time (Sunday, April 3, 2022 9:18 AM)
long_date_time = format_dt(dt, "F")
# Relative Time (in 5 minutes / 1 day ago)
relative = format_dt(dt, "R")
...
# To send this in a command:
await ctx.send(f"{default_date_time} ({relative})")

Related

Last trading day of each month from a given list using python

date = ['2010-01-11' '2010-01-12' '2010-01-13' '2010-01-14' '2010-01-15'
'2010-01-16' '2010-01-17' '2010-01-18' '2010-01-19' '2010-01-20'
'2010-01-21' '2010-01-22' '2010-01-23' '2010-01-24' '2010-01-25'
'2010-01-26' '2010-01-27' '2010-01-28' '2010-01-29' '2010-01-30'
'2010-01-31' '2010-02-01' '2010-02-02' '2010-02-03' '2010-02-04'
'2010-02-05' '2010-02-06' '2010-02-07' '2010-02-08' '2010-02-09'
'2010-02-10' '2010-02-11' '2010-02-12' '2010-02-13' '2010-02-14'
'2010-02-15' '2010-02-16' '2010-02-17' '2010-02-18' '2010-02-19'
'2010-02-20' '2010-02-21' '2010-02-22' '2010-02-23' '2010-02-24'
'2010-02-25' '2010-02-26' '2010-02-27' '2010-02-28' '2010-03-01'
'2010-03-02' '2010-03-03' '2010-03-04' '2010-03-05' '2010-03-06'
'2010-03-07' '2010-03-08' '2010-03-09' '2010-03-10' '2010-03-11'
'2010-03-12' '2010-03-13' '2010-03-14' '2010-03-15' '2010-03-16'
'2010-03-17' '2010-03-18' '2010-03-19' '2010-03-20' '2010-03-21'
'2010-03-22' '2010-03-23' '2010-03-24' '2010-03-25' '2010-03-26'
'2010-03-27' '2010-03-28' '2010-03-29' '2010-03-30' '2010-03-31'
'2010-04-01' '2010-04-02' '2010-04-03' '2010-04-04' '2010-04-05'
'2010-04-06' '2010-04-07' '2010-04-08' '2010-04-09' '2010-04-10'
'2010-04-11' '2010-04-12' '2010-04-13' '2010-04-14' '2010-04-15'
'2010-04-16' '2010-04-17' '2010-04-18' '2010-04-19' '2010-04-20'
'2010-04-21' '2010-04-22' '2010-04-23' '2010-04-24' '2010-04-25'
'2010-04-26' '2010-04-27' '2010-04-28' '2010-04-29' '2010-04-30'
'2010-05-01' '2010-05-02' '2010-05-03' '2010-05-04' '2010-05-05'
'2010-05-06' '2010-05-07' '2010-05-08' '2010-05-09' '2010-05-10'
'2010-05-11' '2010-05-12' '2010-05-13' '2010-05-14' '2010-05-15'
'2010-05-16' '2010-05-17' '2010-05-18' '2010-05-19' '2010-05-20'
'2010-05-21' '2010-05-22' '2010-05-23' '2010-05-24' '2010-05-25'
'2010-05-26' '2010-05-27' '2010-05-28' '2010-05-29' '2010-05-30'
'2010-05-31' '2010-06-01' '2010-06-02' '2010-06-03' '2010-06-04'
'2010-06-05' '2010-06-06' '2010-06-07' '2010-06-08' '2010-06-09'
'2010-06-10' '2010-06-11' '2010-06-12' '2010-06-13' '2010-06-14'
'2010-06-15' '2010-06-16' '2010-06-17' '2010-06-18' '2010-06-19'
'2010-06-20' '2010-06-21' '2010-06-22' '2010-06-23' '2010-06-24'
'2010-06-25' '2010-06-26' '2010-06-27' '2010-06-28' '2010-06-29'
'2010-06-30']
cant seem to figure out the coding to extract the last day of each month in the above list. please note that the last day of each month in the above list does not necessary equivalent to the last day of each calender month.
Expected output:
['2010-01-29', '2010-02-26', '2010-03-31', '2010-04-30', '2010-05-28', '2010-06-30']
saw some solution as follows but it does not return to an valid outcome:
date = date - pd.tseries.offsets.MonthEnd()
previous_month = '01'
last_trading_days = []
for index, day in enumerate(date):
# Extract month from date
month = day[5:7]
# If this is the first day of the new month, append the day that came before it
if month != previous_month:
previous_month = month
last_trading_days.append(date[index - 1])
# Also append the last day
if index == len(date) - 1:
last_trading_days.append(day)
print(last_trading_days)
This is if you know the first month will be January, otherwise you can use previous_month = date[0][5:7] to start on the month of the first date in the list.
Download stock data for each last trading day of the month till date:
df = yf.download(symbol,period='max')
df = df.groupby(df.index.strftime('%Y-%m')).tail(1)

Why is time conversion between epoch seconds and string changing time by 1 calendar year?

I am using the time module of python3 to convert time between seconds and formatted string. Python functions used to generate string are localtime and strftime. To generate the time in seconds, I use string splicing followed by mktime. As I call these repeatedly on each result, only the year changes, always incrementing the seconds by a full year.
Code used is as below:
import time
def time_string(t):
#t is second obtained by time.mktime((yr, mn, dy, hr, mn, sec, 0, 0, 0))
time_struct = time.localtime(t)
time_string = time.strftime("%Y-%m-%d %H:%M:%S", time_struct)
return time_string
def string_time(t_string):
#t_string has format '2020-01-31 08:23:35'
yr = int(t_string[:4])
mn = int(t_string[5:7])
dy = int(t_string[8:10])
hr = int(t_string[11:13])
mn = int(t_string[14:16])
se = int(t_string[17:])
t=int(time.mktime((yr, mn, dy, hr, mn, se, 0, 0, 0)))
return t
t = int(time.mktime((2020, 3, 19, 18, 15, 20, 0, 0, 0)))
print (t)
for x in range(5):
t_st = time_string(t)
print(t_st)
t = string_time(t_st)
print(t)
sys.exit("stopping..")
The results I get from above code execution is as follows:
1584621920
2020-03-19 18:15:20
1616157920
2021-03-19 18:15:20
1647693920
2022-03-19 18:15:20
1679229920
2023-03-19 18:15:20
1710852320
2024-03-19 18:15:20
1742388320
SystemExit: stopping..
What am I doing wrong? Why does this happen?
What is a better way of converting time-string to seconds?
I do not get the purpose of the question, so what you're actually trying to do, however if you have a string of a time, and you want to have the seconds of it, try using datetime.timestamp() instead of a time-string-splicing...
Your code is increasing in the year by one beacuse in your method string_time(t_string) you set the variable mn twice! One time at mn = int(t_string[5:7]) and once at mn = int(t_string[14:16]) which will result in a month of 15 which will adapt the year by 1 year and 3 month which will result in the one year for you
Found time.strptime to solve the problem of converting back from string using the right formatters. The following code eliminated the need to do string splicing
def string_time(t_string):
#t_string has format '2020-01-31 08:23:35'
t_struct = time.strptime(t_string,"%Y-%m-%d %H:%M:%S")
t = int(time.mktime(t_struct))
return t
Korbinian had already found the error in my code. Is there a reason why I should use the datetime module instead of the date module?

How can I Change the TimeZone in my Python enviornment

Im building a bots for some clients but want the bots to activate at the same time based on Eastern time only, since Im in New York, and thats the only time I do know by heart. I don't know what timezone any of my future clients will be in so I'd rather permanently set it contingent upon eastern time. I want them all to be timed to start at 11:00AM EST - US/Eastern
from pytz import timezone
import time, datetime
def Schedule():
tz = timezone('US/Eastern')
today = datetime.datetime.now(tz)
#today = datetime.datetime.now()
Activate = (datetime.datetime(today.year, today.month, today.day, 0, 50, 0) - today).seconds
#Uses Military Time 23, 25, 0 = 11:25:00PM
print('Waiting for ' + str(datetime.timedelta(seconds=Activate)))
time.sleep(Activate)
#Rest of the code will activate at the correct time that you set it
Schedule() #Uncomment this

How to change a given date in "yyyy-MM-dd HH:mm:ss.SSS" format to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" format in groovy

How to convert a given date in yyyy-MM-dd HH:mm:ss.SSS format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format in groovy
For example, the given date is 2019-03-18 16:20:05.6401383. I want it to converted to 2019-03-18T16:20:05.6401383Z
This is the code Used:
def date = format1.parse("2019-03-18 16:20:05.6401383");
String settledAt = format2.format(date)
log.info ">>> "+*date*+" "+*settledAt*
The result, where the date is getting changed somehow: Mon Mar 18 18:06:46 EDT 2019 & 2019-03-18T18:06:46.383Z
Thanks in advance for all the answers.
If you're on Java 8+ and Groovy 2.5+, I would use the new Date/Time API:
import java.time.*
def date = LocalDateTime.parse('2019-03-18 16:20:05.6401383', 'yyyy-MM-dd HH:mm:ss.nnnnnnn')
String settledAt = date.format(/yyyy-MM-dd'T'HH:mm:ss.nnnnnnn'Z'/)
This is presuming the input date has a "Zulu" time zone.
it's a feature of java
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS","2019-03-18 16:20:05.6401383")
returns
Mon Mar 18 18:06:46 EET 2019
the problem that java handles only milliseconds SSS (3 digits after seconds)
but you are providing 7 digits for milliseconds 6401383
as workaround remove extra digits with regexp:
def sdate1 = "2019-03-18 16:20:05.6401383"
sdate1 = sdate1.replaceAll( /\d{3}(\d*)$/, '$1') //keep only 3 digits at the end
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",sdate1)
def sdate2 = date.format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

Remove certain dates in list. Python 3.4

I have a list that has several days in it. Each day have several timestamps. What I want to do is to make a new list that only takes the start time and the end time in the list for each date.
I also want to delete the Character between the date and the time on each one, the char is always the same type of letter.
the time stamps can vary in how many they are on each date.
Since I'm new to python it would be preferred to use a lot of simple to understand codes. I've been using a lot of regex so pleas if there is a way with this one.
the list has been sorted with the command list.sort() so it's in the correct order.
code used to extract the information was the following.
file1 = open("test.txt", "r")
for f in file1:
list1 += re.findall('20\d\d-\d\d-\d\dA\d\d\:\d\d', f)
listX = (len(list1))
list2 = list1[0:listX - 2]
list2.sort()
here is a list of how it looks:
2015-12-28A09:30
2015-12-28A09:30
2015-12-28A09:35
2015-12-28A09:35
2015-12-28A12:00
2015-12-28A12:00
2015-12-28A12:15
2015-12-28A12:15
2015-12-28A14:30
2015-12-28A14:30
2015-12-28A15:15
2015-12-28A15:15
2015-12-28A16:45
2015-12-28A16:45
2015-12-28A17:00
2015-12-28A17:00
2015-12-28A18:15
2015-12-28A18:15
2015-12-29A08:30
2015-12-29A08:30
2015-12-29A08:35
2015-12-29A08:35
2015-12-29A10:45
2015-12-29A10:45
2015-12-29A11:00
2015-12-29A11:00
2015-12-29A13:15
2015-12-29A13:15
2015-12-29A14:00
2015-12-29A14:00
2015-12-29A15:30
2015-12-29A15:30
2015-12-29A15:45
2015-12-29A15:45
2015-12-29A17:15
2015-12-29A17:15
2015-12-30A08:30
2015-12-30A08:30
2015-12-30A08:35
2015-12-30A08:35
2015-12-30A10:45
2015-12-30A10:45
2015-12-30A11:00
2015-12-30A11:00
2015-12-30A13:00
2015-12-30A13:00
2015-12-30A13:45
2015-12-30A13:45
2015-12-30A15:15
2015-12-30A15:15
2015-12-30A15:30
2015-12-30A15:30
2015-12-30A17:15
2015-12-30A17:15
And this is how I want it to look like:
2015-12-28 09:30
2015-12-28 18:15
2015-12-29 08:30
2015-12-29 17:15
2015-12-30 08:30
2015-12-30 17:15
First of all, you should convert all your strings into proper dates, Python can work with. That way, you have a lot more control on it, also to change the formatting later. So let’s parse your dates using datetime.strptime in list2:
from datetime import datetime
dates = [datetime.strptime(item, '%Y-%m-%dA%H:%M') for item in list2]
This creates a new list dates that contains all your dates from list2 but as parsed datetime object.
Now, since you want to get the first and the last date of each day, we somehow have to group your dates by the date component. There are various ways to do that. I’ll be using itertools.groupby for it, with a key function that just looks at the date component of each entry:
from itertools import groupby
for day, times in groupby(dates, lambda x: x.date()):
first, *mid, last = times
print(first)
print(last)
If we run this, we already get your output (without date formatting):
2015-12-28 09:30:00
2015-12-28 18:15:00
2015-12-29 08:30:00
2015-12-29 17:15:00
2015-12-30 08:30:00
2015-12-30 17:15:00
Of course, you can also collect that first and last date in a list first to process the dates later:
filteredDates = []
for day, times in groupby(dates, lambda x: x.date()):
first, *mid, last = times
filteredDates.append(first)
filteredDates.append(last)
And you can also output your dates with a different format using datetime.strftime:
for date in filteredDates:
print(date.strftime('%Y-%m-%d %H:%M'))
That would give us the following output:
2015-12-28 09:30
2015-12-28 18:15
2015-12-29 08:30
2015-12-29 17:15
2015-12-30 08:30
2015-12-30 17:15
If you don’t want to go the route through parsing those dates, of course you could also do this simply by working on the strings. Since they are nicely formatted (i.e. they can be easily compared), you can do that as well. It would look like this then:
for day, times in groupby(list2, lambda x: x[:10]):
first, *mid, last = times
print(first)
print(last)
Producing the following output:
2015-12-28A09:30
2015-12-28A18:15
2015-12-29A08:30
2015-12-29A17:15
2015-12-30A08:30
2015-12-30A17:15
Because your data is ordered you just need to pull the first and last value from each group, you can use re.sub to remove the single letter replacing it with a space then split each date string just comparing the dates:
from re import sub
def grp(l):
it = iter(l)
prev = start = next(it).replace("A"," ")
for dte in it:
dte = dte.replace("A"," ")
# if we have a new date, yield that start and end
if dte.split(None, 1)[0] != prev.split(None,1)[0]:
yield start
yield prev
start = dte
prev = dte
yield start, prev
l=["2015-12-28A09:30", "2015-12-28A09:30", .....................
l[:] = grp(l)
This could also certainly be done as your process the file without sorting by using a dict to group:
from re import findall
from collections import OrderedDict
with open("dates.txt") as f:
od = defaultdict(lambda: {"min": "null", "max": ""})
for line in f:
for dte in findall('20\d\d-\d\d-\d\dA\d\d\:\d\d', line):
dte, tme = dte.split("A")
_dte = "{} {}".format(dte, tme)
if od[dte]["min"] > _dte:
od[dte]["min"] = _dte
if od[dte]["max"] < _dte:
od[dte]["max"] = _dt
print(list(od.values()))
Which will give you the start and end time for each date.
[{'min': '2016-01-03 23:59', 'max': '2016-01-03 23:59'},
{'min': '2015-12-28 00:00', 'max': '2015-12-28 18:15'},
{'min': '2015-12-30 08:30', 'max': '2015-12-30 17:15'},
{'min': '2015-12-29 08:30', 'max': '2015-12-29 17:15'},
{'min': '2015-12-15 08:41', 'max': '2015-12-15 08:41'}]
The start for 2015-12-28 is also 00:00 not 9:30.
if you dates are actually as posted one per line you don't need a regex either:
from collections import defaultdict
with open("dates.txt") as f:
od = defaultdict(lambda: {"min": "null", "max": ""})
for line in f:
dte, tme = line.rstrip().split("A")
_dte = "{} {}".format(dte, tme)
if od[dte]["min"] > _dte:
od[dte]["min"] = _dte
if od[dte]["max"] < _dte:
od[dte]["max"] = _dte
print(list(od.values()
Which would give you the same output.

Resources