Extracting epoch from two different days yields same result - postgresql

If I run this query on my database
SELECT EXTRACT('epoch' FROM age('2021-01-01'::date, '2019-12-01'::date))
The result is 34149600.
But then if I try with 2019-12-02 (one day more)
SELECT EXTRACT('epoch' FROM age('2021-01-01'::date, '2019-12-02'::date))
The result is exactly the same 34149600!
As if the seconds passed from 02 december 2019 to 01 january 2021 are the same as the seconds passed from 01 december 2019 to 01 january 2021.
Why is this? I've already tried the above code with timezones on 00:00:00+00 timezone for all dates (for 1st january 2021, 1st december 2019 and 2nd december 2021), and it gives the same result
Obviously, I would have expected the epoch to be different, around 3600*24 of difference (seconds in a day).

The similar results come from the age() function which returns an interval with years, months, days. In such an interval, 1 month = 30 days, so their conversions in seconds are similar.
You will get the expected result with
SELECT extract(epoch from ('2021-01-01'::timestamp - '2019-12-01'::timestamp)) => 34300800.000000
SELECT extract(epoch from ('2021-01-01'::timestamp - '2019-12-02'::timestamp)) => 34214400.000000

Related

date trunc 6 months

I would like to show 6 months worth of data. For example from the 1st of September to 1st March inclusive.
The code below starts from the 1st of September but finishes on today
date >= date_trunc('month' , now() - ('6 months' :: interval))
I am working on Postgres.

Date/Time range computaion in SSRS

I have sets of date/time range values, each set came from different column ("FROM" and "TO")
examples:
FROM: June 6, 2016 7:00 AM; TO: June 6, 2016 5:00 PM
FROM: June 6, 2016 8:00 PM; TO: June 7, 2016 6:00 AM
I want to get how many minutes of each set falls to 3:00 PM - 2:00 AM (next day) or "OFF PEAK HOURS", like this
for the example above, I want to get the number of minutes between 3PM - 2AM next day.
on the first row, from 7AM-5PM, two hours is included to my 3PM-2AM range criteria, which is 3PM-5PM (2 hours) . Thats why I got 120 mins.
on the second row, from 8PM-6AM next day, two hours is included to my 3PM-2AM range criteria, which is 8PM-2AM (6 hours). Thats why I got 360 mins.
see this graph for reference:
I want to count the number of minutes falls during "OFF PEAK HOURS"-shaded in orange/tan.
Is it doable in expression?
Thank you in advance.
Please note that I am using fetchXML for the query.
Supposing the table you put in the question is wrong and it is only an example, I think you can get the spent minutes between two dates by using this expression:
=DateDiff(DateInterval.Minute,
Fields!From.Value,
Fields!To.Value
)
Example:
=DateDiff(DateInterval.Minute,
CDATE("2016-06-06 07:00:00 AM"),
CDATE("2016-06-06 05:00:00 PM")
)
The above example returns 600. 10 hours by 60 minutes. DateDiff function returns the specified date/time interval of time between two specified dates.
Let me know if this helps.

Summer time calculation

Currently i have missing understand on summertime, below is my context:
start summer time: June/1/2014 0h
end summer time: November/29/2014 0h
assume at the time 23h59 of November/29/2014 after passing => 0h00 November/30/2014 => the summertime will be minus 1 (-1) => 23h00 of November/29/2014 => it still under summer time (June/1/2014 0h - November/29/2014 0h)
So is my calculation above correct ?
Your misunderstanding is ignoring the fact that the time zone is part of the timestamp, and is necessary to make a timestamp in local time unambiguous.
If summer time ends at 00:00 at the beginning of November 30th, then the series of timestamps is:
2014-11-29 23:59:58 XST
2014-11-29 23:59:59 XST
2014-11-29 23:00:00 XWT
2014-11-29 23:01:01 XWT
[...]
2014-11-29 23:59:59 XWT
2014-11:30 00:00:00 XWT
(where "XST" is the summer timezone and "XWT" is the winter timezone). "2014-11-29 23:00:00 XST" and "2014-11-29 23:00:00 XWT" are two different times, one hour apart.
If you are given a local time between 2014-11-29 23:00 and 2014-11-30 00:00 without an indicator of whether it's XST or XWT, then it's impossible to turn it into a unique timestamp.

How to subtract two dates in Oracle and extract the year part from the difference?

My typical requirement is that I want to subtract two Dates cast as timestamp. The Minuend (First parameter) is the current date and the Subtrahend(second parameter) is stored separately as DD, MM and YYYY in three columns. The final output should be a discrete year as number. I am playing with something like :
SELECT (TO_DATE('05-DEC-2013') -
CASE LENGTH(CAND_DOB_DD)
WHEN 1 THEN
CAST(TO_DATE('0'||CAND_DOB_DD||'-'||CAND_DOB_MM||'- '||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP)
ELSE
CAST(TO_DATE(CAND_DOB_DD||'-'||CAND_DOB_MM||'-'||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP) / 365 END YEAR
FROM CANDIDATE
The Year part as Integer will be used for a very sensitive calculation. Please suggest if the above piece of SCRIPT will yield the desired result. Thanks in advance.
For most purposes you could just use the Months_Between() function to determine the number of months between two dates, and then divide by 12 etc.. Note that the number of months is an integer when comparing two dates that have the same day of the month or are both the last day of the month.
This is tricky when it comes to leap years.
Do you count 28th Feb 2015 to 28th Feb 2016 as exactly one year, when 28th Feb 2015 to 29th Feb 2016 is one day longer but plainly is a year?
What about 29th Feb 2016 to 28th Feb 2017, or 28th Feb 2016 to 28th Feb 2017?
Think carefully about these boundary cases, but Months_Between() is likely to be your best choice.

FQL and business hours

I'm querying information from a Facebook page for a small business using FQL and I'm trying to parse the business hours. The numbers I am getting back seem to represent seconds but I'm not sure when the epoch is. Wednesday and Thursday are the most confusing - open on Thursday is "57600" which would be 16 hours in seconds which would make 4pm Wednesday the epoch, but the closing hours on Wednesday - far past 4- are in the 600,000+ range.
Mon: 8:15am-12pm and 1pm - 5pm Tue: 8am-12pm and 1pm - 5pm Wed: 8am-12pm and 1pm - 9pm Thur:8am-12pm and 1pm - 5pm Fri:8am-12pm and 1pm - 5pm Sat:8am-12pm and 1pm - 5pm
<hours>
<mon_1_open>404100</mon_1_open>
<mon_1_close>417600</mon_1_close>
<tue_1_open>489600</tue_1_open>
<tue_1_close>504000</tue_1_close>
<wed_1_open>576000</wed_1_open>
<wed_1_close>590400</wed_1_close>
<thu_1_open>57600</thu_1_open>
<thu_1_close>72000</thu_1_close>
<fri_1_open>144000</fri_1_open>
<fri_1_close>158400</fri_1_close>
<sat_1_open>230400</sat_1_open>
<sat_1_close>244800</sat_1_close>
<sun_1_open>0</sun_1_open>
<sun_1_close>0</sun_1_close>
<mon_2_open>421200</mon_2_open>
<mon_2_close>435600</mon_2_close>
<tue_2_open>507600</tue_2_open>
<tue_2_close>522000</tue_2_close>
<wed_2_open>594000</wed_2_open>
<wed_2_close>622800</wed_2_close>
<thu_2_open>75600</thu_2_open>
<thu_2_close>90000</thu_2_close>
<fri_2_open>162000</fri_2_open>
<fri_2_close>176400</fri_2_close>
<sat_2_open>248400</sat_2_open>
<sat_2_close>262800</sat_2_close>
<sun_2_open>0</sun_2_open>
<sun_2_close>0</sun_2_close>
</hours>
If I change it to simply 8am-5pm Monday to Saturday I get an equally confusing response from FB
<hours>
<mon_1_open>403200</mon_1_open>
<mon_1_close>435600</mon_1_close>
<tue_1_open>489600</tue_1_open>
<tue_1_close>522000</tue_1_close>
<wed_1_open>576000</wed_1_open>
<wed_1_close>608400</wed_1_close>
<thu_1_open>57600</thu_1_open>
<thu_1_close>90000</thu_1_close>
<fri_1_open>144000</fri_1_open>
<fri_1_close>176400</fri_1_close>
<sat_1_open>230400</sat_1_open>
<sat_1_close>262800</sat_1_close>
<sun_1_open>0</sun_1_open>
<sun_1_close>0</sun_1_close>
<mon_2_open>0</mon_2_open>
<mon_2_close>0</mon_2_close>
<tue_2_open>0</tue_2_open>
<tue_2_close>0</tue_2_close>
<wed_2_open>0</wed_2_open>
<wed_2_close>0</wed_2_close>
<thu_2_open>0</thu_2_open>
<thu_2_close>0</thu_2_close>
<fri_2_open>0</fri_2_open>
<fri_2_close>0</fri_2_close>
<sat_2_open>0</sat_2_open>
<sat_2_close>0</sat_2_close>
<sun_2_open>0</sun_2_open>
<sun_2_close>0</sun_2_close>
</hours>
Am I missing some defacto standard time representation? How would someone go about parsing this as a legitimate time of day?
The Unix epoch is the time 00:00:00 UTC on 1 January 1970. Any time you see the term "epoch" used in relation to computer-based time, that's usually what it means.
In UTC, 404100 is Mon, 05 Jan 1970 16:15:00 GMT. Or, in the PST timezone, Mon, 05 Jan 1970 08:15:00 PST, which is the time you're expecting. Ignore the date; it's irrelevant, anyways.
You can test what I'm describing using this Epoch Converter.