How to validate dates in Hash format in MySQL or C++ - c++

I need to validate dates in Hash (SHA256) format in MySQL or C++.
For example: date1 < date2 or date1 > date2.
I have this query in MySQL:
SELECT SHA1(CURDATE()) -->'2017-09-06'
and:
SELECT SHA1('2017-09-06') --> '34152f3661d73490ac89b0fe15cb3170aac06bb8'
SELECT SHA1('2017-09-07') --> '0b10f03fb245a6486d6ab5b25a2f050bf87093a5'
But, if I use:
SELECT IF (SHA1('2017-09-06') <= SHA1('2017-09-07') ,'True','False') AS Test;
the result is False, therefore, incorrect!

Don't know why you'd do this, but here's a solution for you.
Here's some assumptions.
You were given two date hashes (two inputs)
You know exactly how the date hashes were created (EG: always in the format of YYYY-MM-DD)
You know the range of your target date (So you can reverse the hash back to the original date)
So in psudocode, you'd do something like this.
date getDateFromHash(string inputHash) {
date startDate = '1911-01-01';
date endDate = '2017-12-31';
for(date checkDate = startDate; checkDate < endDate; checkDate + 1 day) {
if (sha1(checkDate) = inputHash) { return checkDate }
}
return null;
}
date firstDate = getDateFromHash("0b10f03fb245a6486d6ab5b25a2f050bf87093a5"); // 2017-09-07
date secondDate = getDateFromHash("34152f3661d73490ac89b0fe15cb3170aac06bb8"); // 2017-09-06
if (firstDate > secondDate) {
return true;
} else {
return false;
} // compare using default date operators
But honestly speaking, like the comment says, there's no point of hashing date values. Hence why I'm only dropping a quich psudo code for you just to point you in the right direction, if you seriously want to tread down this path.

Related

Run SUM(*) on Supabase

I'm starting with Supabase and would like to understand how I can pass the SUM() aggregator in a SELECT.
I noticed that for COUNT we use:
const { data, error, range, count } = supabase
.from('table')
.select('*', { count: 'exact' })
Is there anything similar to SUM that I haven't noticed?
My query is this:
select
"GE_PRODUCTS",
sum("GE_QUANTITY") as Quantity,
sum("GE_SALEVALUE") as Revenue
from "GE_SELLS"
where "GE_ENTERPRISE" = 'G.E.'
and "DELETED" <> '*'
group by "GE_PRODUCTS"
order by Revenue desc
limit 3;
Your best bet is to put this into a PostgreSQL function and call it from Supabase using .rpc():
CREATE OR REPLACE FUNCTION get_my_sums()
RETURNS TABLE (
"GE_PRODUCTS" TEXT,
Quantity NUMBER,
Revenue NUMBER
) AS $$
DECLARE
var_r record;
BEGIN
RETURN QUERY
select
"GE_PRODUCTS",
sum("GE_QUANTITY") as Quantity,
sum("GE_SALEVALUE") as Revenue
from "GE_SELLS"
where "GE_ENTERPRISE" = 'G.E.'
and "DELETED" <> '*'
group by "GE_PRODUCTS"
order by Revenue desc
limit 3;
END; $$
LANGUAGE 'plpgsql';

Return NOW if it's the current hour

I am trying to create a column with empty values if it's not the current hour, else return NOW.
I tried this:
CurrentHour = IF(Datatime_long[Date] = YEAR(TODAY()) && Datatime_long[Time] = HOUR(NOW()), "NOW","")
Could somebody spot the mistake or is there another way to tackle this ?
Data looks like this and sample output is CurrentHour with all empty values
One way to tackle this is to use DATEDIFF: using it with HOUR parameter will return zero only if we are in the current hour.
This column should work:
ThisHour = IF(DATEDIFF(DateTable[Datetime], NOW(), HOUR) = 0, NOW(), BLANK())

Convert date format to character string

I have a column of format DATETIME23. like this:
14.02.2017 13:00:25
I want to conver it to a string, so later, i would be able to modern it, so, for example, the final version would look like:
2017-02-14 13:00:25.000
Problem occures, when i try to convert date to char format: in result i have a string of smth like 1802700293 - which is the number of seconds.
I tried:
format date $23.0
or
date = put(date, $23.0)
P.S This is nother try:
data a;
format d date9.;
d = '12jan2016'd;
dtms = cat(day(d),'-',month(d),'-',year(d),' 00:00:00.000');
/* если нужно обязательно двухзначные день и месяц, то такой колхоз: */
if day(d) < 10 then dd=cat('0',put(day(d),$1.));
else ddday=put(day(d),$2.);
if month(d) < 10 then mm=cat('0',put(month(d),$1.));
else mm=put(month(d),$2.);
yyyy=put(year(d),$4.);
/*dtms2 = cat(dd,'-',mm,'-',yyyy,' 00:00:00.000');*/
dtms2 = cat(dd,'-',mm,'-',yyyy,' 00:00:00.000');
dtms = cat(day(d),'-',month(d),'-',year(d),' 00:00:00.000');
run;
BUT, abnormally, the dtms2 concat destroys the zero in the month element
If your datetime is stored as a SAS datetime, just use the appropriate format :
data test ;
dt = '09feb2017:13:53:26'dt ; /* specify a datetime constant */
new_dt = put(dt,E8601DT23.3) ; /* ISO datetime format */
run ;
Output
dt new_dt
1802267606 2017-02-09T13:53:26.000
If you need to replace the 'T' with a space, simply add a translate function around the put().
For your dtms solution you can use put and the Z2. format to keep the leading zero when you concatenate:
dtms = cat(day(d),'-', put(month(d),z2.),'-',year(d),' 00:00:00.000');
You should be able to just use put(date, datetime23.) for your problem though instead of $23, which is converting the number of seconds to a string with length 23. However, as a comment has mentioned datetime23. is not the format from your example.

How to use if-else with String dates

I am trying to use if-else statements to get the date entered by the user and display a constant for that particular season.
So for example if I were to enter a date of 04/25/2015 I would want it to show the price for Spring. When I print the price it displays zero instead.
Any help will be great. Method will be shown below
public double determinePrice(){
double price = 0;
if(rentalDate.substring(1, 3).equals(3) || rentalDate.substring(1, 3).equals(4)|| rentalDate.substring(1, 3).equals(5))
price = SPRING;
else if(rentalDate.substring(1, 3).equals(6) || rentalDate.substring(1, 3).equals(7)|| rentalDate.substring(1, 3).equals(8))
price = SUMMER;
else if(rentalDate.substring(1, 3).equals(9) || rentalDate.substring(0, 3).equals(10)|| rentalDate.substring(0, 3).equals(11))
price = FALL;
else if(rentalDate.substring(0, 3).equals(12) || rentalDate.substring(0, 3).equals(01)|| rentalDate.substring(1, 3).equals(02))
price = WINTER;
return price;
}
You maybe should try to parse your string into a LocalDate and then work with these (1). With the usage of LocalDate you can define the 3 dates determining the start of summer,autumn and winter and simply check your dates like:
givenDate.isBefore(summerStartDate)
for dates in spring or
givenDate.isAfter(summerStartDate) && givenDate.isBefore(autumnStartDate)
for dates in summer...
Another possibility with the use of LocalDate would be to write a class describing timeframes with a method isIn(LocalDate) that checks whether the given date lies between start and end.
edit:
Just saw that you're only testing for the month belonging to a specific month. After parsing your original date into a LocalDate you could use the methods getMonth (gives you the month in the form of an enum value) or getMonthValue (gives you a numerical representation 1-12).
(1) Howto convert: How to parse/format dates with LocalDateTime? (Java 8)

Why is day of pandas date_range not what I specified?

Why is the first date not 4/19/1965? Why day 30 instead of 19?
dates = pd.date_range('1965-04-19', freq='6M', periods=3)
dates[0]
Timestamp('1965-04-30 00:00:00', offset='6M')
It looks like the default behavior for freq='M' is MonthEnd(), so
dates = pd.date_range('1965-04-19', periods=3, freq='6M')
dates
DatetimeIndex(['1965-04-30', '1965-10-31', '1966-04-30'], dtype='datetime64[ns]', freq='6M', tz=None)
This can be changed by setting freq = pd.tseries.offsets.DateOffset(months=6).
dates = pd.date_range('1965-04-19', periods=3, freq=pd.tseries.offsets.DateOffset(months=6))
dates
DatetimeIndex(['1965-04-19', '1965-10-19', '1966-04-19'], dtype='datetime64[ns]', freq='<DateOffset: kwds={'months': 6}>', tz=None)