What does the "p" in strptime stand for? - strptime

There is a strptime function in many language libraries (C, Python, Ruby, PHP, PERL, etc.).
It seems to be based on the Open Group's specification for time.h.
I understand 'str' stands for string, and 'time' obviously stands for time, but what does the 'p' stand for? Parse? Pointer? Print?
Every time I reach for the strptime() function, I have a mental blank, and have to look up the name in a manual. I figure if I finally worked out what it stood for, perhaps I would have a chance of remembering it.

I guess it stands for "parse" because its reverse function is called strftime in Python's time module wherein the "f" I can reasonably guess stands for "format".

p = pointer. It returns a pointer to a char.
BTW According to my K&R there is a
char *strpbrk(cs,ct);
This 'p' also refers to the returned pointer.

I have same problem and I'm going with put:
strftime -> 'string from time'
strptime -> 'string, put time'

Most of the places, I found
strftime() -> string format time &
strptime() -> string parsed time

It helps me to remember:
p for produce, str p time -> string produce time
f for format, str f time -> string format time

I think of strftime() as string from time. And the function strptime() is, probably, derived from string parsed time.

Here's a pneumonic I found helpful:
strftime -> string (forward) time : this function converts from a time object
strptime -> string (previous) time : this function converts from a string object

This is yet another way to remember this:
f for from, str f time (strftime)-> string from time
p for produce, str p time (strptime)-> string produces time

strftime: format a datetime into a string
strptime: parse a string into a datetime

Related

How to format the time as following "2018-03-15T23:47:15+01:00"

With java.time , I'm trying to format the time as the following "2018-03-15T23:47:15+01:00" .
With this formatter I'm close to the result in Scala.
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssZ")
ZonedDateTime.now() // 2018-03-14T19:25:23.397+01:00
ZonedDateTime.now().format(formatter) // => 2018-03-14 19:25:23+0100
But I cannot insert the extra character "T" between the day and hour.
What does this "T" mean BTW ?
How to format as "2018-03-15T23:47:15+01:00" ?
Notes:
In case you wonder why LocalDateTime cannot be formatted
Format LocalDateTime with Timezone in Java8
Try this
val ZONED_DATE_TIME_ISO8601_FORMATTER3 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
ZonedDateTime.now().format(ZONED_DATE_TIME_ISO8601_FORMATTER3)
// 2018-03-14T19:35:54.321+01:00
See here
Offset X and x: This formats the offset based on the number of pattern letters. One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'. Two letters outputs the hour and minute, without a colon, such as '+0130'. Three letters outputs the hour and minute, with a colon, such as '+01:30'. Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'. Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'. Six or more letters throws IllegalArgumentException. Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.
Converting the ZonedDateTime to OffsetDateTime - as suggested in the other answers - works, but if you want to use a DateTimeFormatter, there's a built-in constant that does the job:
ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
But it's important to note some differences between all the approaches. Suppose that the ZonedDateTime contains a date/time equivalent to 2018-03-15T23:47+01:00 (the seconds and milliseconds are zero).
All the approaches covered in the answers will give you different results.
toString() omits seconds and milliseconds when they are zero. So this code:
ZonedDateTime zdt = // 2018-03-15T23:47+01:00
zdt.toOffsetDateTime().toString()
prints:
2018-03-15T23:47+01:00
only hour and minute, because seconds and milliseconds are zero
The built-in formatter will omit only the milliseconds if it's zero, but it'll print the seconds, regardless of the value. So this:
zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
prints:
2018-03-15T23:47:00+01:00
seconds printed, even if it's zero; milliseconds ommited
And the formatter that uses an explicit pattern will always print all the fields specified, regardless of their values. So this:
zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx"))
prints:
2018-03-15T23:47:00.000+01:00
seconds and milliseconds are printed, regardless of their values
You'll also find a difference in values such as 2018-03-15T23:47:10.120+01:00 (note the 120 milliseconds). toString() and ofPattern will give you:
2018-03-15T23:47:10.120+01:00
While the built-in DateTimeFormatter.ISO_OFFSET_DATE_TIME will print only the first 2 digits:
2018-03-15T23:47:10.12+01:00
Just be aware of these details when choosing which approach to use.
As your question already shows, you may just rely on ZonedDateTime.toString() for getting a string like 2018-03-14T19:25:23.397+01:00. BTW, that string is in ISO 8601 format, the international standard. Only two minor modifications may be needed:
If you don’t want the fraction of second — well, I don’t see what harm it does, it agrees with ISO 8601, so whoever receives your ISO 8601 string should be happy to have it. But if you don’t want it, you may apply myZonedDateTime.truncatedTo(ChronoUnit.SECONDS) to get rid of it.
ZonedDateTime.toString() often appends a zone name, for example 2018-03-14T19:25:23+01:00[Europe/Paris], which is not part of the ISO 8601 standard. To avoid that, convert to OffsetDateTime before using its toString method: myZonedDateTime.toOffsetDateTime().toString() (or myZonedDateTime.truncatedTo(ChronoUnit.SECONDS).toOffsetDateTime().toString()).
Building your own formatter through a format pattern string is very flexible when this is what you need. However, very often we can get through with less (and then should do for the easier maintainability of our code): toString methods or built-in formatters including both the ISO ones and the localized ones that we can get from DateTimeFormatter.ofLocalizedPattern().
What does this "T" mean BTW ?
The T is part of the ISO 8601 format. It separates the date part from the time-of-day part. You may think of it as T for time since it denotes the start of the time part. If there is only a date (2018-04-25) or only a time-of-day (21:45:00), the T is not used, but when we have both, the T is required. You may think that the format might have been specified without the T, and you are probably right. When it comes to the format for periods/durations it is indispensable, however, and also needed when there are no days: P3M means a period of 3 months, while PT3M means 3 minutes.
Link: Read more in the Wikipedia article on ISO 8601.

sprintf function's arguments and formats in matlab

I'v read and re-read the help about the function sprintf in matlab but I do not understand everything about this function and the format they talk about.
I was asking myself the logic behind the function formats.
If I run the example
sprintf('%05d%s%02d%s%02d',546,'.',1,'.',3)
I get
00546.01.03
which is logic, since the first number (546) is written as an integer and with 5 digits, the second is a character, and so on... But if now I try this
sprintf('%05d%s%02d%s%02d',546,'.',1,'.',3,4)
I get
00546.01.0300004
the first part is the same as above... But the last part of it (00004) has the format '%05d', that corresponds to the first format I entered in the function's arguments. My question is then Does the first format become the 'default' format ?
By trying this
sprintf('%05d%s%02d%s%02d',546,'.',1,'.',3,4,56)
and getting this
00546.01.03000048
I think the answer is no... But why ? And what is then the logic behind those arguments?
Thanks for your help !
You are providing sprintf more arguments than there are %s in the format string. Therefore, sprintf re-uses the format string from begining:
sprintf('%05d%s%02d%s%02d',546,'.',1,'.',3,4,56)
result:
00546.01.03000048
^
starting fromat anew printing 00004 for %05d with 4
The final '8' character is 56 printed as '%s' (if you want to check it out the ascii code of '8' (the char) is 56!)

perl datetime output explain

perl code:
#!/bin/env perl
use DateTime;
print DateTime->now;
OUTPUT:
2013-01-28T06:02:33
what's mean of 'T' letter in the output string ?
ISO8601 and RFC3339 both use "T" to join the date and the time, and DateTime's default stringifier apparently adopted that convention. If you want another format, you can use one of the DateTime::Format::* modules or ->strftime.
my $now = DateTime->now( time_zone => 'local' );
say $now->strftime('%Y-%m-%d %H:%M:%S');
It stands for "Time". You can read more at:
http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations
It's the separator between date and time in ISO 8601. It's always a T in this format.
The character [T] shall be used as time designator to indicate the start of the representation of the time of day
component in these expressions. The hyphen [-] and the colon [:] shall be used, in accordance with 4.4.4, as
separators within the date and time of day expressions, respectively, when required.
NOTE By mutual agreement of the partners in information interchange, the character [T] may be omitted in
applications where there is no risk of confusing a date and time of day representation with others defined in this
International Standard.
--Data elements and interchange
formats — Information interchange —
Representation of dates and times, ISO 8601:2004(E)

NSDateFormatter Date Format for date/time with colon in Time Zone

I am having a problem converting a date/time string to an NSDate.
The date string I need to convert is: 2002-12-24T00:00:00-07:00
My date format string is currently yyyy-MM-dd'T'HH:mm:ssZZZ
The problem is the colon in the time zone.
ZZZ returns a string like: -0700 (without the colon)
How can I convert a string WITH a colon in the time zone?
Thanks
I suggest doing some string manipulation so it is in a form that dateWithString can more easily accept - how about using stringByReplacingOccurrencesOfString one or more times to get rid of the colon?
dateWithString wants:
YYYY-MM-DD HH:MM:SS ±HHMM
you have:
yyyy-MM-dd'T'HH:mm:ssZZZ
You will probably need to use some combination of componentsSeparatedByString (to get rid of the 'T' part, unless you have a small range of values possible for T, and perhaps write yourself a small function to convert ssZZZ into +HHMM.

Parsing date in Pascal

what's the better way of parsing date in Pascal than manually parsing character after character?
Date should be in mm.dd.yyyy format.
Depends on the compiler used. If you use Delphi have a look at trystrtodate, or (if with a recent Free Pascal) try dateutils.scandatetime
If you're using Turbo Pascal, then the only thing you can do is check the string character by character.
Or, you can create a record:
type
dater = Record
month: byte;
day : byte;
year: integer;
End;
var mydate: dater;
Thus, knowing the format (mm.dd.yyyy) you can easily validate it.
Access the values easily mydate.month, mydate.day, mydate.year.