source: sans/Dev/trunk/NCNR_User_Procedures/Reduction/VSANS/V_ISO8601_Util.ipf

Last change on this file was 1242, checked in by srkline, 3 years ago

updating the IgorVersion? pragma to v7.0 for all files to be consistent.

File size: 2.9 KB
Line 
1#pragma rtGlobals=3             // Use modern global access method and strict wave access.
2#pragma IgorVersion = 7.00
3
4//
5// The general format of the time string is:
6//  1901-01-01T12:00:00-0500
7// (note that the -0500 appears to be incorrect - should be -05:00
8//
9//   Complete date plus hours, minutes and seconds:
10//      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
11//
12
13// What utilities do I need to have to interpret these times?
14
15// possible needs:
16// Convert to "Igor" time? (as a number for computation)
17// Find the elapsed time between two ISO times?
18// Convert "Igor" time to ISO time? (as a string for writing)
19//
20
21// What does Igor have built-in to work with...?
22//
23// Unfortunately, there are functions based on the Julian calendar
24// and the ISO standard uses the Gregorian calendar...
25//
26//¥print time()
27//  10:53:42 AM
28//¥print date()
29//  Wed, Oct 7, 2015
30//¥Print DateTime
31//  3.52706e+09
32//¥Print secs2date(DateTime,-2)
33//  2015-10-07
34//¥Print secs2time(DateTime,2)
35//  10:55
36//¥Print secs2time(DateTime,3)
37//  10:55:50
38
39
40
41// The two utilites are self-consistent, at least --
42// passing in an ISO time returns itself atfer comupting the "Igor" seconds
43//
44// print CurrentTime_to_ISO8601String(ISO8601_to_IgorTime("2015-10-07T11:04:26-05:00"))
45//
46
47//
48// Call w/ DateTime as the argument as V_CurrentTime_to_ISO8601String(DateTime)
49//
50// DONE -- verify that this is correct
51Function/S V_CurrentTime_to_ISO8601String(now)
52        Variable now
53       
54        String s1,s2,s3,ISOstr
55       
56        s3 = "-05:00"           // time zone string
57       
58        // Y-M-D
59        s1 = secs2date(now,-2)
60        //H:M:S
61        s2 = secs2time(now,3)
62
63        ISOstr = s1 + "T" + s2 + s3
64       
65        return(ISOstr)
66End
67
68//
69// takes the ISO time and converts it into "Igor" time
70// as if it was reported from DateTime:
71//
72// "The DateTime function returns number of seconds from 1/1/1904 to the current date and time."
73//
74// DONE -- verify that this is correct, since I'm not actually parsing the string, but rather
75// counting on the string to be EXACTLY the correct format
76//
77Function V_ISO8601_to_IgorTime(ISOstr)
78        String ISOstr
79       
80        Variable secs
81        String s1,s2
82        s1 = ISOstr[0,9]                //taking strictly the first 10 characters YYYY-MM-DD
83        s2 = ISOstr[11,18]              // skip the "T" and take the next 8 chars HH:MM:SS
84        // skip the time zone...
85//      print s1
86//      print s2
87       
88        Variable yr,mo,dy,hh,mm,ss
89        yr = str2num(s1[0,3])
90        mo = str2num(s1[5,6])
91        dy = str2num(s1[8,9])
92        hh = str2num(s2[0,1])
93        mm = str2num(s2[3,4])
94        ss = str2num(s2[6,7])
95
96        secs = date2secs(yr,mo,dy)
97        secs += hh*60*60
98        secs += mm*60
99        secs += ss
100       
101        return(secs)
102End
103
104
105// utility to compare two iso dates
106// returns
107// 1 if iso1 is greater than iso2 (meaning iso1 is more RECENT)
108// 2 if iso2 is greater than iso1 (meaning iso2 is more RECENT)
109// 0 if they are the same time
110//
111//
112Function V_Compare_ISO_Dates(iso1,iso2)
113        String iso1,iso2
114       
115        if(V_ISO8601_to_IgorTime(iso1) == V_ISO8601_to_IgorTime(iso2))
116                return(0)
117        endif
118
119        if(V_ISO8601_to_IgorTime(iso1) > V_ISO8601_to_IgorTime(iso2))
120                return(1)
121        else
122                return(2)
123        endif
124       
125        return(-1)
126End
127
Note: See TracBrowser for help on using the repository browser.