I have a timestamp field on a mysql table , i want to know if that timestamp is 24hrs old or more. What would be the best way to do that in Perl?
SQL that would return the timestamp 24 hours ago.
SELECT UNIX_TIMESTAMP(NOW() - INTERVAL 24 HOUR)
Now if your timestamp is < the timestamp returned by the above SQL , 24 hours have passed.
The best thing is to let the database do the work. See SQL statement to select all rows from previous day for an example.
By timestamp I am assuming it's Unix timestamp, i.e. seconds since epoch.
my $ts = 1393662619;
my $day_24hr = 24 * 60 * 60; ## seconds in 24 hrs
my $prev_time = time() - $day_24hr; ## 24hours ago
if ( $ts < $prev_time ) {
print "timestamp is 24 hour old";
}
You can use localtime for that.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($unix_timestamp);
Related
I am working recently with postgres and I have to make several calculations. However I have not been able to imitate the HOUR () function of Excel, I read the official information but it did not help me much.
The function receives a decimal and obtains the hour, minutes and seconds of the same, example the decimal 0,99988426 returns 11:59:50. Try doing this in postgres (i use PostgreSQL 10.4) with the to_timestamp function: select to_char (to_timestamp (0.99988426), 'HH24: MI: SS'); this return 19:00:00. Surely I am omitting something, some idea of how to solve this?
24:00:00 or 86400 seconds = 1
Half day(12:00 noon) or 43200 seconds = 43200/86400 = 0.5
11:59:50 or 86390 seconds = 86390/86400 = 0.99988426
So to convert your decimal value to time, all you have to do is multiply it with 86400 which will give you seconds and convert it to your format in following ways:
SELECT TO_CHAR((0.99988426 * 86400) * '1 second'::interval, 'HH24:MI:SS');
SELECT (0.99988426 * 86400) * interval '1 sec';
There are two major differences to handle:
Excel does not consider the time zone. The serial date 0 starts at 0h00, but Postgres uses the time zone so it becomes 19h. You would need to use UTC in Postgres result to have the same as in Excel.
select to_char (to_timestamp (0), 'HH24: MI: SS'),to_char (to_timestamp (0) AT TIME ZONE 'UTC', 'HH24: MI: SS');
to_char | to_char
------------+------------
19: 00: 00 | 00: 00: 00
Excel considers that 1 is one day, while Postgres considers 1 as 1 second. To get the same behavior, multiply your number by the 86400, i.e. the number of seconds in a day
select to_char (to_timestamp (0.99988426*86400) AT TIME ZONE 'UTC', 'HH24: MI: SS');
to_char
------------
23: 59: 50
(1 row)
I try to understand why
print(pd.Timestamp("2015-01-01") - pd.DateOffset(day=1))
does not result in
pd.Timestamp("2014-12-31")
I am using Pandas 0.18. I run within the CET timezone.
You can check pandas.tseries.offsets.DateOffset:
*kwds
Temporal parameter that add to or replace the offset value.
Parameters that add to the offset (like Timedelta):
years
months
weeks
days
hours
minutes
seconds
microseconds
nanoseconds
Parameters that replace the offset value:
year
month
day
weekday
hour
minute
second
microsecond
nanosecond
print(pd.Timestamp("2015-01-01") - pd.DateOffset(days=1))
2014-12-31 00:00:00
Another solution:
print(pd.Timestamp("2015-01-01") - pd.offsets.Day(1))
2014-12-31 00:00:00
Also it is possible to subtract Timedelta:
print(pd.Timestamp("2015-01-01") - pd.Timedelta(1, unit='d'))
pd.DateOffset(day=1) works (ie no error is raised) because "day" is a valid parameter, as is "days".
Look at the below one: "day" resets the actual day, "days" adds to the original day.
pd.Timestamp("2019-12-25") + pd.DateOffset(day=1)
Timestamp('2019-12-01 00:00:00')
pd.Timestamp("2019-12-25") + pd.DateOffset(days=1)
Timestamp('2019-12-26 00:00:00')
Day(d) and DateOffset(days=d) do not behave exactly the same when used on timestamps with timezone information (at least on pandas 0.18.0). It looks like DateOffset add 1 day while keeping the hour information while Day adds just 24 hours of elapsed time.
>>> # 30/10/2016 02:00+02:00 is the hour before the DST change
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.offsets.Day(1))
2016-10-31 01:00:00+01:00
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.DateOffset(days=1))
2016-10-31 02:00:00+01:00
in my DB2 database, I have the UTC data in the format 1160307144500000 and a field which indicates the time difference respect to local date, for example 3600 in seconds.
I need sum to UTC date the 3600 secs so I can get 1160307154500000.
Here is some DB2 code snippet
TIMESTAMP('1900-01-01 00:00:00')
+ INT(SUBSTR(utc_data,1,3)) YEAR
+ INT(SUBSTR(utc_data,4,2)) + 1 MONTH
+ INT(SUBSTR(utc_data,6,2)) DAY
+ INT(SUBSTR(utc_data,8,2)) HOUR
+ INT(SUBSTR(utc_data,10,2)) MINUTE
+ INT(SUBSTR(utc_data,12,2)) SECOND
+ 1000*INT(SUBSTR(utc_data,15,3)) MICROSECOND
+ diff_data SECOND
I'll leave the code to transform the timestamp back to the original format up to you :)
I am new to lua scripting. I have a startDate ("03-05-2014" as "dd-mm-yyyy") and a span in days (2) Can anyone help me how to get the endDate based on the startDate and span?.
Example startDate span endDate
--------- ---- -------
03-05-2014 2 05-05-2014
(dd-mm-yyyy) (dd-mm-2014)
You don't need to do any math here. os.time and os.date will do it for you.
local day, month, year = ("03-05-2014"):match("(%d%d)-(%d%d)-(%d%d%d%d)")
local span = 64
local endtime = os.time({day = day + span, month = month, year = year})
print(os.date("%c", endtime))
I'm not going to write the whole program for you, but here's something you can start with:
Get the day, month and year from the string:
local day, month, year = string.match('03-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
day, month, year = tonumber(day), tonumber(month), tonumber(year)
Use os.time to get the timestamp of a start time. You can
then add 3600 * 24 * 2 seconds (2 days) to get the timestamp of the end time.
Use os.date to formats the string from a timestamp.
This could help you
local dayValue, monthValue, yearValue = string.match('31-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
dayValue, monthValue, yearValue = tonumber(dayValue), tonumber(monthValue), tonumber(yearValue)
now = os.time{year = yearValue, month = monthValue, day = dayValue}
numberOfDays = now + 2 * 24 * 3600
print(os.date("%c",numberOfDays))
dateAfterNumberOfDays = os.date("%a %d %B %Y, %H%p%M",numberOfDays)
print ("\nafter number of days "..dateAfterNumberOfDays) -- give you date after number of days
I need a time in minutes and seconds. If minutes is greater than 60 I then add to minutes, but do not convert hours
Query :
TO_CHAR((sum(seconds)|| ' second')::interval,'HH24:MI:SS')
as duration from table;
Output:
DURATION
02:50:21
Required Output:
DURATION
170:21
even i have tried another query (without HH24) but i get below out put:
Query :
TO_CHAR((sum(seconds)|| ' second')::interval,'MI:SS') as duration from table;
Output:
DURATION
50:21
Here we can see from 1st query output 02 hrs means 120 minutes + 50minutes =170 minutes and seconds same as it is.
is it possible directly getting from query or not?
you don't need to_char here:
select (sum(seconds) / 60)::text || ':' || (sum(seconds) % 60) ...;