Golang time.Parse() Formatting with Non-Time Number
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?
go time strptime
add a comment |
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?
go time strptime
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 wasIs 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 0: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
add a comment |
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?
go time strptime
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?
go time strptime
go time strptime
edited Nov 28 '18 at 13:33
WXMan
asked Nov 27 '18 at 18:45
WXManWXMan
758
758
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 wasIs 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 0: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
add a comment |
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 wasIs 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 0: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
So: What is the question? Just remove stuff you do not want to parse.
– Volker
Nov 27 '18 at 19:52
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
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
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 0:05
@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 0: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
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506229%2fgolang-time-parse-formatting-with-non-time-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
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
answered Nov 27 '18 at 20:05
chezniccheznic
93
93
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
add a comment |
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
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
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
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506229%2fgolang-time-parse-formatting-with-non-time-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 0: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