go-hep.org/x/hep@v0.38.1/groot/riofs/utils.go (about)

     1  // Copyright ©2017 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package riofs
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  	"strings"
    11  	"time"
    12  )
    13  
    14  // decodeNameCycle decodes a namecycle "aap;2" into name "aap" and cycle "2"
    15  func decodeNameCycle(namecycle string) (string, int16) {
    16  	var name string
    17  	var cycle int16
    18  
    19  	toks := strings.Split(namecycle, ";")
    20  	switch len(toks) {
    21  	case 1:
    22  		name = toks[0]
    23  		cycle = 9999
    24  	case 2:
    25  		name = toks[0]
    26  		i, err := strconv.Atoi(toks[1])
    27  		if err != nil {
    28  			// not a number
    29  			cycle = 9999
    30  		} else {
    31  			cycle = int16(i)
    32  		}
    33  	default:
    34  		panic(fmt.Errorf("invalid namecycle format [%v]", namecycle))
    35  	}
    36  
    37  	return name, cycle
    38  }
    39  
    40  // datime2time converts a uint32 holding a ROOT's TDatime into a time.Time
    41  func datime2time(d uint32) time.Time {
    42  
    43  	// ROOT's TDatime begins in January 1995...
    44  	var year uint32 = (d >> 26) + 1995
    45  	var month uint32 = (d << 6) >> 28
    46  	var day uint32 = (d << 10) >> 27
    47  	var hour uint32 = (d << 15) >> 27
    48  	var min uint32 = (d << 20) >> 26
    49  	var sec uint32 = (d << 26) >> 26
    50  	nsec := 0
    51  	return time.Date(int(year), time.Month(month), int(day),
    52  		int(hour), int(min), int(sec), nsec, time.UTC)
    53  }
    54  
    55  // time2datime converts a time.Time to a uint32 representing a ROOT's TDatime.
    56  func time2datime(t time.Time) uint32 {
    57  	var (
    58  		year  = uint32(t.Year())
    59  		month = uint32(t.Month())
    60  		day   = uint32(t.Day())
    61  		hour  = uint32(t.Hour())
    62  		min   = uint32(t.Minute())
    63  		sec   = uint32(t.Second())
    64  	)
    65  
    66  	if year < 1995 {
    67  		panic("riofs: TDatime year must be >= 1995")
    68  	}
    69  
    70  	return (year-1995)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec
    71  }