comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: number of seconds since 1/1/1970
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: number of seconds since 1/1/1970 [message #3757] Mon, 13 March 1995 18:28 Go to next message
Jackel is currently offline  Jackel
Messages: 30
Registered: April 1993
Member
In article <1995Mar13.174212.23087@newsserver.rrzn.uni-hannover.de> knipp@ipi.uni-hannover.de (K Knipp) writes:

> I need a function to convert any given date (not the actual one)
> to the number of seconds elapsed since Jan 1st, 1970.

A while back I put together something like this. It does the
same thing as the UNIX function "gmtime", which seems to
be what you want. I'm including it below, please let me know
if there are any problems.



; Brian Jackel, University of Western Ontario Jackel@Canlon.Physics.UWO.CA
; All comments/bug reports/suggestions cheerfully accepted
;+
; NAME: GMTIME
;
; PURPOSE: This function converts the time in seconds since
; January 1 1970 to something more useful.
;
; CATEGORY: Timing
;
; CALLING SEQUENCE: Result= GMTIME( Timeinseconds )
;
; INPUTS:
; Timeinseconds a scalar value or array containing the number of
; seconds since Jan 1 1970 (This should be a long
; integer) OR a structure or array of structures of
; type {GMTIME} (see below)
;
; KEYWORD PARAMETERS:
; ASCTIME If this keyword is set, then the returned value
; is a string (or array of strings) containing the
; time in standard format (see OUTPUTS).
;
;
; OUTPUTS: The default behaviour for this function is to return
; a structure of the form:
;
; {GMTime, sec:0B, ;seconds 0-59
; min:0B, ;minutes 0-59
; hour:0B, ;hours 0-23
; mday:0B, ;day of month 1-31
; mon:0B, ;month 0-11
; year:0, ;years since 1900
; wday:0B, ;day of week 0-6 ,Sunday is 0
; yday:0, ;day of year 0-356
; long_sec:0L} ;seconds since Jan 1 1970
;
; unless the keyword /ASCTIME was set, in which case the result will be
; of type string, with the form
;
; Thu Jan 01 00:00:00 1970
;
; RESTRICTIONS: Won't work for negative times, ie. nothing before 1970.
; No attempt is made to correct for daylight savings time,
; or offset from UT.
;
; EXAMPLE:
; time0= SYSTIME(0) ;current time, date etc.
; time1= SYSTIME(1) ;time in seconds since Jan 1 1970
; gm= GMTIME( time1,/ASCTIME ) ;this should be within a second of
; ;time0, NOT INCLUDING OFFSET FROM UT!
;
; MODIFICATION HISTORY: Brian Jackel March 27 1994
; University of Western Ontario
;-


function GMTIME,Timeinseconds,ASCTIME=asctime

ON_ERROR,2
IF KEYWORD_SET(ASCTIME) THEN asctime=1 ELSE asctime=0

gm= {gmtime, sec:0B, min:0B, hour:0B, mon:0B, year:0, wday:0B, mday:0B, yday:0, long_sec:0L }

siz= SIZE(Timeinseconds)
CASE siz(siz(0)+1) OF
0:MESSAGE,'Error- input value undefined'
1:MESSAGE,'Warning- input value was of type BYTE',/INFORMATIONAL
6:MESSAGE,'Error- input value was complex'
7:MESSAGE,'Error- input value was a string'
8:BEGIN
structname= TAG_NAMES(Timeinseconds,/STRUCT)
IF (structname EQ 'GMTIME') THEN BEGIN
asctime=1
gm= Timeinseconds
goto,ASCTIME
ENDIF ELSE MESSAGE,'Error- input value was a structure, not of type GMTIME'
END
ELSE:dummy=0
ENDCASE

IF (siz(0) EQ 0) THEN gm={gmtime} ELSE $
gm= MAKE_ARRAY( DIMENSION= siz( 1:siz(0) ), VALUE=gm )

gm.long_sec= LONG(Timeinseconds)
;
;Strip off hours, minutes, seconds, since the start of a day
;
secondsinday= 86400L ;60 seconds/minute *60 minutes/hour *24 hours/day
temp= gm.long_sec MOD secondsinday ;seconds since the start of a day
gm.sec= temp MOD 60L
temp= temp / 60L ;full minutes since the start of a day
gm.min= temp MOD 60L
gm.hour= temp / 60L ;full hours since the start of the day


;
;Strip off days, months, years
;
temp= gm.long_sec / secondsinday ;days since Jan 1 1970
gm.wday= (temp+4) MOD 7 ;day of week (Jan 1 1970 was a Thursday=4)

daysin4years= 1461L ;3 regular years + 1 leap year = 365*3 + 366
gm.year= 4 * FIX(temp / daysin4years) +70 ;70 years since 1900 plus however many 4 year blocks
temp= temp MOD daysin4years ;days since the start of a 4 year period

w= WHERE( temp GE 365 ,nw ) ;first year in a block will always have 365 days
IF (nw GT 0) THEN BEGIN ; ie. 1970, 1974, 1978..
gm(w).year= gm(w).year +1
temp(w)= temp(w) - 365

w= WHERE( temp GE 365 ,nw ) ;second year will also have 365 days
IF (nw GT 0) THEN BEGIN ; ie. 1971, 1975, 1979...
gm(w).year= gm(w).year +1
temp(w)= temp(w) - 365

w= WHERE( temp GE 366 ,nw ) ;but third year will be a leap year,
IF (nw GT 0) THEN BEGIN ; with 366 days
gm(w).year= gm(w).year +1 ; ie. 1972, 1976, 1980...
temp(w)= temp(w) - 366
ENDIF

ENDIF
ENDIF
gm.yday= temp ;number of days since January 1 of the current year (0 to 365)


daysinmonthlist= [31,28,31,30,31,30,31,31,30,31,30,31]
month= 0
daysinmonth= daysinmonthlist(month)
w= WHERE( temp GT daysinmonth ,nw )
WHILE (nw GT 0) DO BEGIN
month= month+1
temp(w)= temp(w)- daysinmonth
gm(w).mon= month
daysinmonth= daysinmonthlist(month)
IF (month EQ 1) THEN daysinmonth= daysinmonth + ((gm.year MOD 4) EQ 0)
w= WHERE( temp GT daysinmonth ,nw )
ENDWHILE
gm.mday= temp+1 ;day of the month (1 to 31)


ASCTIME: IF (asctime EQ 1) THEN BEGIN
IF (siz(0) EQ 0) THEN timestring=' ' ELSE $
timestring= MAKE_ARRAY( DIMENSION= siz( 1:siz(0) ), VALUE=' ' )
days= ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
months= ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct' ,'Nov','Dec']
timestring= STRING(days(gm.wday),FORMAT="(A3)") + $
STRING(months(gm.mon),FORMAT="(X,A3)") + $
STRING(gm.mday,FORMAT="(X,I2.2)") + $
STRING(gm.hour,FORMAT="(X,I2.2,':')") + $
STRING(gm.min,FORMAT="(I2.2,':')")+ $
STRING(gm.sec,FORMAT="(I2.2)")+ $
STRING(gm.year+1900,FORMAT="(X,I4)" )
return,timestring
ENDIF ELSE return,gm


END
Re: number of seconds since 1/1/1970 [message #3759 is a reply to message #3757] Tue, 14 March 1995 07:17 Go to previous message
sterner is currently offline  sterner
Messages: 106
Registered: February 1991
Senior Member
knipp@ipi.uni-hannover.de (K Knipp) writes:

> I'm sure it's in the libraries but I can't find it:
> I need a function to convert any given date (not the actual one)
> to the number of seconds elapsed since Jan 1st, 1970.

My IDL library has a routine called dt_tm_tojs (date and time to
Julian Seconds) which converts a date and time to the number of
seconds after 2000 Jan 1 0:00. A simple difference will give the
value you need. Here is an example use:

js0 = dt_tm_tojs('1970 jan 1 0:00')
sec = dt_tm_tojs('1990 mar 14 10:15')-js0
print,sec
6.3740970e+08

There is also the inverse, dt_tm_fromjs, which allows fairly flexible
formatting of the result. Many other time related routines are
available in this library. These are documented in the following
web page:
ftp://fermi.jhuapl.edu/www/s1r/idl/s1rlib/time/time.html
Information on obtaining the library is also on that page.

If you don't have web access (a very unfortunate circumstance)
you can get the library as follows:

ftp fermi.jhuapl.edu
login: anonymous
password: enter your email address
cd pub/idl
get README
bye

Follow the instructions in the README (~6.6k bytes) file to get the
actual library. You may also
want to get the one line description file cat.one (~30kb).

Ray Sterner sterner@tesla.jhuapl.edu
The Johns Hopkins University North latitude 39.16 degrees.
Applied Physics Laboratory West longitude 76.90 degrees.
Laurel, MD 20723-6099
WWW Home page: ftp://fermi.jhuapl.edu/www/s1r/people/res/res.html
Re: number of seconds since 1/1/1970 [message #3761 is a reply to message #3757] Mon, 13 March 1995 19:03 Go to previous message
chris is currently offline  chris
Messages: 22
Registered: October 1994
Junior Member
K Knipp (knipp@ipi.uni-hannover.de) wrote:

:... to the number of seconds elapsed since Jan 1st, 1970.

Do you have JULDATE in your library? If not, mail me and I'll send it to
you. Example:

secperday = 86400.d0
juldate,[1970,1,1,0,0.],before
juldate,[1995,3,13,18,33.2],now ;18:33.2
print,(now-before) * secperday

The times specified are Universal Times, (which differs from PST by
8 hours) but since you are taking differences, I don't think it
should matter as long as both are specified the same.

-chris
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Calculate convex hull of scattered data?
Next Topic: New User seeks book

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:13:59 PDT 2025

Total time taken to generate the page: 0.00627 seconds