Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: How to use a different epoch with DateTime

by anita2R (Scribe)
on Feb 14, 2017 at 21:37 UTC ( #1182007=note: print w/replies, xml ) Need Help??

in reply to How to use a different epoch with DateTime

Does the system you work on have its epoch set to zero for 01 January 2000, or does it use the standard Linux epoch of 01 January 1970?

(I am ignoring the issue of whether your year 2000 epoch starts at midnight or at noon!)

Try this to see what it's set at:

#!/usr/bin/perl # use Time::Local; # use strict; use warnings; # note months are 0 to 11 - hence the '-1' # timegm is seconds, minutes, hours, day, month -1, year my $two_k_epoch = timegm( 0, 0, 0, 1, 0, 2000 ); # midnight 01 January + 2000 - change to noon if required my $linux_epoch = timegm( 0, 0, 0, 1, 0, 1970 ); # midnight 01 January + 1970 print "2K epoch: $two_k_epoch\tLinux epoch: $linux_epoch\n";

If the 2K epoch is zero, then you shouldn't have to do anything, as all calculations will be using that epoch. As long as the calculated seconds are not transferred to another system that is using the Linux epoch there shouldn't be any problems, but it's not something I can test.

If however your system is using standard Linux epoch ($linux_epoch is zero) the you will have to subtract the year 2000 value from all calculated seconds and add it back again before converting seconds back to eye-readable values.

You will still need to know whether you are working in UTC or local time.

When you used 'DateTime' in your question title, did you specifically mean the perl DateTime module, or just any date and time module. From what I have just read, DateTime uses an epoch based on year 1 (long before the Gregorian calendar was invented),

 In this calendar, the first day of the calendar (the epoch), is the first day of year 1

As an aside, I like Date::Calc as I do a fair bit of adding/subtracting days or hours from start dates/times

Replies are listed 'Best First'.
Re^2: How to use a different epoch with DateTime
by thimes (Novice) on Feb 15, 2017 at 01:59 UTC
    Yes I was referring to the Perl DateTime module. But I'm not stuck on using that. My problem is this. 1. I must use the epoch 2000 Jan 1, 12:00:00 (UTC) That's 12noon. 2. From that epoch I will be given both beginning and end time boundaries in the future. 3. I will need to add/sub seconds from either of those boundaries. 4. Then convert back to readable form in UTC.

      DateTime is probably not the best module to use, particularly as you are going forward and don't need dates before the start of the Gregorian calendar. The man page for DateTime notes that it does not account for leap seconds, so your results may be out.

      The default time zone for new DateTime objects, except where stated otherwise, is the "floating" time zone. This concept comes from the iCal standard. A floating datetime is one which is not anchored to any particular time zone. In addition, floating datetimes do not include leap seconds, since we cannot apply them without knowing the datetime's time zone. The results of date math and comparison between a floating datetime and one with a real time zone are not really valid, because one includes leap seconds and the other does not. Similarly, the results of datetime math between two floating datetimes and two datetimes with time zones are not really comparable.

      As suggested by thomas895 create your new epoch seconds for the new dates given, calculate the epoch seconds for 01 January 2000, noon, and subtract it from the epoch seconds for new dates given.

      The number of seconds difference between future dates will be the same whatever the base epoch date is.

        Hi anita2R,

        The man page for DateTime notes that it does not account for leap seconds, so your results may be out.

        The section of the doc you are quoting refers only to the special "floating" time zone (Update: I consider the "floating" time zone to be the equivalent of "no time zone"). When you specify any time zone (including UTC), DateTime does handle leap seconds.

        -- Hauke D

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1182007]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-11-23 17:25 GMT
Find Nodes?
    Voting Booth?

    No recent polls found