-3

I am porting code from python and have a function that takes a formatting string and equivalent datetime string and creates a datetime object:

import datetime
def retrieve_object(file_name, fmt_string):
    datetime = datetime.strptime(file_name, fmt_string)
    // Do additional datetime calculations here

I tried creating the equivalent function in Go:

import(
    "time"
)

func retrieve_object(file_name string, fmt_string string) {
    time_out, _ := time.Parse(fmt_string, file_name)  
    // Do additional time.Time calculations here

This parses the time.Time correctly in this case:

file_name := "KICT20170307_000422" 
fmt_string := "KICT20060102_150405"
// returns 2017-03-07 00:04:22 +0000 UTC

But fails to correctly parse in this case:

file_name := "KICT20170307_000422_V06.nc" 
fmt_string := "KICT20060102_150405_V06.nc"
// returns 2006-03-07 00:04:22 +0000 UTC

I suspect this is due to the additional non-date number ("06") in the datetime string. Is it possible to create a function that can create a time.Time object given an arbitrary formatting string and datetime string representation using the built-in time.Parse function? If not, are there any third-party solutions that could work?

WXMan
  • 310
  • 1
  • 12
  • So: What is the question? Just remove stuff you do not want to parse. – Volker Nov 27 '18 at 19:52
  • Sorry - I'll edit the post to elaborate a bit more. But the question was ```Is there any way to use time.Parse and indicate a number in the formatting string should not be considered as part of the time?```. Unfortunately stripping the suffix won't work for me, as I need a flexible function that takes an arbitrary formatting string and datetime string and returns the time.Time object. – WXMan Nov 27 '18 at 20:28
  • 1
    @WXMan: Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Asking for an arbitrary formatting string and an arbitrary datetime string to return a time.Time is unreasonable. – peterSO Nov 28 '18 at 00:05
  • I'm porting code from Python to Go, and solving this problem in python is very straight-forward with strptime. I honestly do not think it is an unreasonable question to ask _if it's possible_ to do the equivalent in Go when there are non-time numbers contained in the datetime string. If the answer is no, then I'm happy with that and will move on. I'll update the post to more directly ask this question. – WXMan Nov 28 '18 at 13:10

1 Answers1

1

I suspect this is obvious but here it goes...

just strip it:

func removeSuffix(s string) (string, error) {
    i := strings.LastIndexByte(s, '_')

    if i < 0 {
         return "", fmt.Errorf("invalid input")
    }

    runes := []rune(s)

    result := runes[0:i]

    return string(result), nil
}

Here's the go playground https://play.golang.org/p/JbHt4Png-eT

cheznic
  • 39
  • 3
  • This does answer the question as originally proposed. But I realized I left out an important point; I need a flexible function that takes an arbitrary formatting string and datetime string and returns the time.Time object. So stripping won't work if we don't necessarily know what the incoming format looks like. – WXMan Nov 27 '18 at 20:31