Golang parsing date in RFC822Z format without leading zero [duplicate] - date

This question already has answers here:
How to format current time using a yyyyMMddHHmmss format?
(6 answers)
Convert time.Time to string
(6 answers)
Parsing RFC-3339 / ISO-8601 date-time string in Go
(8 answers)
Parse string as a time.Time value [duplicate]
(1 answer)
Parsing date/time strings which are not 'standard' formats
(4 answers)
Closed 2 months ago.
I have a date string I can't control that I'm trying to parse into a Date.
The format most closely resembles RFC822Z.
RFC822Z = "02 Jan 06 15:04 -0700"
Reference: https://yourbasic.org/golang/format-parse-string-time-date-example/
However, it does not have the leading zero.
Example: "5 Dec 2022 20:15:21 +0000"
The way I saw in other posts, is to write a manual format.
parseTime, timeParseError = time.Parse("2 Jan 2006 15:04:21 -0700", stringDate)
However, when I try that, I get a warning:
parsing time "2 Jan 2006 15:04:21 -0700" as "2 Jan 2006 15:04:21 -0700": cannot parse " -0700" as "1" (SA1002)
Running it despite the warning fails to part, unsurprisingly.

Your time format doesn't match - in your example you have "5 Dec 2022", but you are using "2 Jan 06", and in your reference format you hvae "15:04:21" but it should be "15:04:05".
Your reference format should be 2 Jan 2006 15:04:05 -0700 not 2 Jan 06 15:04:21 -0700
https://go.dev/play/p/Shc381WgB6_7

Related

Google Sheet convert time format to another

Whenever I extract raw data from this specific tool I am getting the date format of
Fri Sep 2 10:29:50 2022
Fri Sep 9 10:31:01 2022
Mon Sep 12 10:32:28 2022
and I am having a hard time converting it to this kind of format in Google Sheet
9/2/2022 10:29
9/9/2022 10:31
9/12/2022 10:32
Already tried using different of formating but still can't convert it to the above format.
use:
=ARRAYFORMULA(IF(A1:A5="",,REGEXREPLACE(TRIM(A1:A5),
"(.*) (.*) (.*) (.*) (.*)", "$2/$3/$5 $4")*1))
then do:
Date value
=ArrayFormula(IF(A2:A="",,
DATEVALUE(REGEXEXTRACT(TRIM(A2:A), "(.+\d{1,2}) \d{1,2}:")&" "&REGEXEXTRACT(A2:A, " (....\z)"))+
TIMEVALUE(REGEXEXTRACT(TRIM(A2:A), " (\d{1,2}:\d{1,2}:\d{1,2}) "))))
Custom date and time formats
Custom number formats

cannot parse " +0000 UTC" as "T". Go utc time parsing error [duplicate]

This question already has answers here:
Convert UTC string to time object
(3 answers)
Closed 3 years ago.
I was trying to parse a time time between utc string back to Go time. But I'm getting an error cannot parse " +0000 UTC" as "T".
stringTime := time.Now().UTC().String()
t, e := time.Parse(time.RFC3339, stringTime)
fmt.Println(e)
fmt.Println(t)
Playground
stringTime is not in RFC3339 format: https://golang.org/pkg/time/#Time.String
If you want to parse it, use the format in the link.

looking for spark scala(java) code for date string with spaces in between with specific conditions

need some suggestions on below requirement.
Each response help a lot thanks in advance....
I have a date of type String with timestamp ex: Jan 8 2019 4:44 AM
My requirement is if the date is single digit I want date to be 1 space and digit
(ex: 8) and if the date is 2 digits which is dates from 10 to 31 I want date with no space(ex:10) and also same for hour in timestamp.
to summarize: if the date is 1 to 9 and hour in timestamp is 1 to 9 looking for below string
Jan 8 2019 4:44 AM
if the date is 10 to 31 and hour in timestamp is 10 to 12 looking for below string
Jan 18 2019 12:44 AM
right now I am creating a date in following way:
val sdf = new SimpleDateFormat("MMM d yyyy h:mm a")
but the above format satisfies only one condition which is dates from 1 to 9.
my application is spark with scala so looking for some spark scala code or java.
I appreciate your help...
Thanks..
java.time
Use p as a pad modifier in the format pattern string. In Java syntax (sorry):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"MMM ppd ppppu pph:mm a", Locale.ENGLISH);
System.out.println(LocalDateTime.of(2019, Month.JANUARY, 8, 4, 44)
.format(formatter));
System.out.println(LocalDateTime.of(2019, Month.JANUARY, 18, 0, 44)
.format(formatter));
Jan 8 2019 4:44 AM
Jan 18 2019 12:44 AM
And do yourself the favour: Forget everything about the SimpleDateFormat class. It is notoriously troublesome and fortunately long outdated. Use java.time, the modern Java date and time API.
Link: Oracle tutorial: Date Time explaining how to use java.time.
To quote the DateTimeFormatter class documentation:
Pad modifier: Modifies the pattern that immediately follows to be padded with spaces. The pad width is determined by the number of pattern letters. This is the same as calling DateTimeFormatterBuilder.padNext(int).
For example, 'ppH' outputs the hour-of-day padded on the left with spaces to a width of 2.

Date format toUTCString in Lua

I have a question regarding date formatting in Lua (Luajit). I need to get UTC string, for example, like I would do it in JavaScript:
var date = new Date()
console.log(date.toUTCString()) // "Fri, 06 Dec 2013 14:05:28 GMT"
Unfortunately in Lua I cannot find possibility to format date this way:
print(os.date()) -- Fri Dec 6 16:06:43 2013
From the Lua manual:
If format starts with '!', then the date is formatted in Coordinated Universal Time. [...]
If format is not "*t", then date returns the date as a string, formatted according to the same rules as the ANSI C function strftime.
Based on this and a little documentation referencing, it's quite simple to construct a format string that resembles JavaScript's toUTCString format.
> =os.date('!%a, %d %b %Y %H:%M:%S GMT')
Fri, 06 Dec 2013 14:27:34 GMT

Convert seconds to date from Jan 01 1901 in unix/linux

im trying to convert a time stamp in seconds from Jan 01 1901 to the current date.
for example,
time stamp 3465468225 translate to a date in 2010. does anyone know of a way to do this in unix/linux? thanks.
In R, it is as simple as this:
> as.POSIXct(3465468225, origin="1901-01-01")
[1] "2010-10-25 15:03:45 CDT"
>
This uses appropriate wrappers around C-level calls gmtime() / localtime() plus time formatting via strftime().
On GNU and POSIX systems you can obtain the date string using seconds since Epoch (1970-01-01 00:00:00 UTC) as:
$ date --date=#1289495920
Thu Nov 11 12:18:40 EST 2010
You should handle the offset since Jan 01 1901 yourself.